Skip to content

Commit

Permalink
Update SA1404 to not crash when attribute uses a namespace alias
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed Apr 23, 2024
1 parent 045aba1 commit 1f7ac37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,23 @@ public void Bar()

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[InlineData("global::System.Obsolete")]
[InlineData("global::My")]
[WorkItem(3829, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3829")]
public async Task TestGlobalOtherAttributeAsync(string name)
{
var testCode = $@"public class MyAttribute : System.Attribute
{{
}}
[{name}]
public class Foo
{{
}}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ public void HandleAttributeNode(SyntaxNodeAnalysisContext context)
{
if (!(attribute.Name is SimpleNameSyntax simpleNameSyntax))
{
QualifiedNameSyntax qualifiedNameSyntax = attribute.Name as QualifiedNameSyntax;
simpleNameSyntax = qualifiedNameSyntax.Right;
if (attribute.Name is AliasQualifiedNameSyntax aliasQualifiedNameSyntax)
{
simpleNameSyntax = aliasQualifiedNameSyntax.Name;
}
else
{
QualifiedNameSyntax qualifiedNameSyntax = attribute.Name as QualifiedNameSyntax;
simpleNameSyntax = qualifiedNameSyntax.Right;
}
}

if (simpleNameSyntax.Identifier.ValueText != nameof(SuppressMessageAttribute)
Expand Down

0 comments on commit 1f7ac37

Please sign in to comment.