Skip to content

Commit 17b613d

Browse files
authored
Merge pull request #3830 from bjornhellander/feature/sa1404-global-3829
Update SA1404 to not crash when attribute uses a namespace alias
2 parents d855e3a + 1f7ac37 commit 17b613d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,5 +426,23 @@ public void Bar()
426426

427427
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
428428
}
429+
430+
[Theory]
431+
[InlineData("global::System.Obsolete")]
432+
[InlineData("global::My")]
433+
[WorkItem(3829, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3829")]
434+
public async Task TestGlobalOtherAttributeAsync(string name)
435+
{
436+
var testCode = $@"public class MyAttribute : System.Attribute
437+
{{
438+
}}
439+
440+
[{name}]
441+
public class Foo
442+
{{
443+
}}";
444+
445+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
446+
}
429447
}
430448
}

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1404CodeAnalysisSuppressionMustHaveJustification.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,15 @@ public void HandleAttributeNode(SyntaxNodeAnalysisContext context)
100100
{
101101
if (!(attribute.Name is SimpleNameSyntax simpleNameSyntax))
102102
{
103-
QualifiedNameSyntax qualifiedNameSyntax = attribute.Name as QualifiedNameSyntax;
104-
simpleNameSyntax = qualifiedNameSyntax.Right;
103+
if (attribute.Name is AliasQualifiedNameSyntax aliasQualifiedNameSyntax)
104+
{
105+
simpleNameSyntax = aliasQualifiedNameSyntax.Name;
106+
}
107+
else
108+
{
109+
QualifiedNameSyntax qualifiedNameSyntax = attribute.Name as QualifiedNameSyntax;
110+
simpleNameSyntax = qualifiedNameSyntax.Right;
111+
}
105112
}
106113

107114
if (simpleNameSyntax.Identifier.ValueText != nameof(SuppressMessageAttribute)

0 commit comments

Comments
 (0)