private void ReportDiagnosticsIfNeeded(NameEqualsSyntax nameEquals, SyntaxNodeAnalysisContext context, OptionSet optionSet, SyntaxTree syntaxTree) { if (!nameEquals.IsParentKind(SyntaxKind.AnonymousObjectMemberDeclarator)) { return; } var anonCtor = (AnonymousObjectMemberDeclaratorSyntax)nameEquals.Parent; if (!optionSet.GetOption(CSharpCodeStyleOptions.PreferInferredAnonymousTypeMemberNames).Value || !CSharpInferredMemberNameReducer.CanSimplifyAnonymousTypeMemberName(anonCtor)) { return; } // Create a normal diagnostic context.ReportDiagnostic( Diagnostic.Create(GetDescriptorWithSeverity( optionSet.GetOption(CSharpCodeStyleOptions.PreferInferredAnonymousTypeMemberNames).Notification.Value), nameEquals.GetLocation())); // Also fade out the part of the name-equals syntax var fadeSpan = TextSpan.FromBounds(nameEquals.Name.SpanStart, nameEquals.EqualsToken.Span.End); context.ReportDiagnostic( Diagnostic.Create( UnnecessaryWithoutSuggestionDescriptor, syntaxTree.GetLocation(fadeSpan))); }
private void ReportDiagnosticsIfNeeded(NameEqualsSyntax nameEquals, SyntaxNodeAnalysisContext context, AnalyzerOptions options, SyntaxTree syntaxTree, CancellationToken cancellationToken) { if (!nameEquals.Parent.IsKind(SyntaxKind.AnonymousObjectMemberDeclarator, out AnonymousObjectMemberDeclaratorSyntax? anonCtor)) { return; } var preference = options.GetOption( CodeStyleOptions.PreferInferredAnonymousTypeMemberNames, context.Compilation.Language, syntaxTree, cancellationToken); if (!preference.Value || !CSharpInferredMemberNameReducer.CanSimplifyAnonymousTypeMemberName(anonCtor)) { return; } // Create a normal diagnostic context.ReportDiagnostic( DiagnosticHelper.Create( Descriptor, nameEquals.GetLocation(), preference.Notification.Severity, additionalLocations: null, properties: null)); // Also fade out the part of the name-equals syntax RoslynDebug.AssertNotNull(UnnecessaryWithoutSuggestionDescriptor); var fadeSpan = TextSpan.FromBounds(nameEquals.Name.SpanStart, nameEquals.EqualsToken.Span.End); context.ReportDiagnostic( Diagnostic.Create( UnnecessaryWithoutSuggestionDescriptor, syntaxTree.GetLocation(fadeSpan))); }