Skip to content

Commit ea5e0d9

Browse files
Update SA1135UsingDirectivesMustBeQualified to not crash on a UsingDirectiveSyntax without a Name (c# 12's "alias any type")
#3882
1 parent ff5c432 commit ea5e0d9

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/ReadabilityRules/SA1135CSharp10UnitTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp10.ReadabilityRules
75
{
86
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/ReadabilityRules/SA1135CSharp12UnitTests.cs

+46
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,55 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp12.ReadabilityRules
55
{
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp11.ReadabilityRules;
11+
using Xunit;
12+
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.ReadabilityRules.SA1135UsingDirectivesMustBeQualified,
15+
StyleCop.Analyzers.ReadabilityRules.SA1135CodeFixProvider>;
716

817
public partial class SA1135CSharp12UnitTests : SA1135CSharp11UnitTests
918
{
19+
public static IEnumerable<object[]> CorrectAliasableTypes => new[]
20+
{
21+
new[] { "string" },
22+
new[] { "(string, int)" },
23+
new[] { "(System.String, System.Int32)" },
24+
new[] { "bool[]" },
25+
new[] { "System.Boolean[]" },
26+
};
27+
28+
[Theory]
29+
[MemberData(nameof(CorrectAliasableTypes))]
30+
[WorkItem(3882, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3882")]
31+
public async Task TestAliasAnyTypeOutsideNamespaceAsync(string type)
32+
{
33+
var testCode = $@"
34+
using MyType = {type};
35+
36+
namespace TestNamespace
37+
{{
38+
}}";
39+
40+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
41+
}
42+
43+
[Theory]
44+
[MemberData(nameof(CorrectAliasableTypes))]
45+
[WorkItem(3882, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3882")]
46+
public async Task TestAliasAnyTypeInsideNamespaceAsync(string type)
47+
{
48+
var testCode = $@"
49+
namespace TestNamespace
50+
{{
51+
using MyType = {type};
52+
}}";
53+
54+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
55+
}
1056
}
1157
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1135CSharp7UnitTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
75
{
86
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/ReadabilityRules/SA1135CSharp8UnitTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
75
{
86
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1135UnitTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.ReadabilityRules
75
{
86
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1135UsingDirectivesMustBeQualified.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.ReadabilityRules
75
{
86
using System.Collections.Immutable;
@@ -82,6 +80,12 @@ private static void CheckUsingDeclaration(SyntaxNodeAnalysisContext context, Usi
8280
return;
8381
}
8482

83+
if (usingDirective.Name == null)
84+
{
85+
// This happens for e.g. "using X = string;" or "using T = (X, Y);"
86+
return;
87+
}
88+
8589
var symbol = context.SemanticModel.GetSymbolInfo(usingDirective.Name, context.CancellationToken).Symbol;
8690
if (symbol == null)
8791
{

0 commit comments

Comments
 (0)