diff --git a/src/Features/CSharp/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.cs b/src/Features/CSharp/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.cs index be387e0777556d48ec126270c5fc6463866eb8ed..8eea98b43ebdcd3109c155f6700876bc3d3640b6 100644 --- a/src/Features/CSharp/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.cs +++ b/src/Features/CSharp/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.cs @@ -2,8 +2,10 @@ #nullable enable +using System; using System.Collections.Generic; using System.Threading; +using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Options; @@ -65,7 +67,14 @@ public override void VisitGenericName(GenericNameSyntax node) public override void VisitIdentifierName(IdentifierNameSyntax node) { - if (node.IsKind(SyntaxKind.IdentifierName) && TrySimplify(node)) + // Always try to simplify identifiers with an 'Attribute' suffix. + // + // In other cases, don't bother looking at the right side of A.B or A::B. We will process those in + // one of our other top level Visit methods (like VisitQualifiedName). + var canTrySimplify = node.Identifier.ValueText!.EndsWith("Attribute", StringComparison.Ordinal) + || !node.IsRightSideOfDotOrArrowOrColonColon(); + + if (canTrySimplify && TrySimplify(node)) { // found a match. report it and stop processing. return; diff --git a/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.vb b/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.vb index 85d23be55e76b9cca383662532304cd8e02abd4d..8f88c41e2b966ca1830c18082c28149dd7dc82d4 100644 --- a/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.vb +++ b/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/TypeSyntaxSimplifierWalker.vb @@ -41,7 +41,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames End Sub Public Overrides Sub VisitIdentifierName(node As IdentifierNameSyntax) - If node.IsKind(SyntaxKind.IdentifierName) AndAlso TrySimplify(node) Then + ' Always try to simplify identifiers with an 'Attribute' suffix. + ' + ' In other cases, don't bother looking at the right side of A.B or A!B. We will process those in + ' one of our other top level Visit methods (Like VisitQualifiedName). + Dim canTrySimplify = CaseInsensitiveComparison.EndsWith(node.Identifier.ValueText, "Attribute") _ + OrElse Not node.IsRightSideOfDotOrBang() + + If canTrySimplify AndAlso TrySimplify(node) Then Return End If