Skip to content

Commit

Permalink
Update SA1135UsingDirectivesMustBeQualified to not crash on a UsingDi…
Browse files Browse the repository at this point in the history
…rectiveSyntax without a Name (c# 12's "alias any type")

DotNetAnalyzers#3882
  • Loading branch information
bjornhellander committed Aug 6, 2024
1 parent ff5c432 commit ea5e0d9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp10.ReadabilityRules
{
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,55 @@

namespace StyleCop.Analyzers.Test.CSharp12.ReadabilityRules
{
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp11.ReadabilityRules;
using Xunit;

using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1135UsingDirectivesMustBeQualified,
StyleCop.Analyzers.ReadabilityRules.SA1135CodeFixProvider>;

public partial class SA1135CSharp12UnitTests : SA1135CSharp11UnitTests
{
public static IEnumerable<object[]> CorrectAliasableTypes => new[]
{
new[] { "string" },
new[] { "(string, int)" },
new[] { "(System.String, System.Int32)" },
new[] { "bool[]" },
new[] { "System.Boolean[]" },
};

[Theory]
[MemberData(nameof(CorrectAliasableTypes))]
[WorkItem(3882, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3882")]
public async Task TestAliasAnyTypeOutsideNamespaceAsync(string type)
{
var testCode = $@"
using MyType = {type};
namespace TestNamespace
{{
}}";

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

[Theory]
[MemberData(nameof(CorrectAliasableTypes))]
[WorkItem(3882, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3882")]
public async Task TestAliasAnyTypeInsideNamespaceAsync(string type)
{
var testCode = $@"
namespace TestNamespace
{{
using MyType = {type};
}}";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
{
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
{
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.ReadabilityRules
{
using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.ReadabilityRules
{
using System.Collections.Immutable;
Expand Down Expand Up @@ -82,6 +80,12 @@ private static void CheckUsingDeclaration(SyntaxNodeAnalysisContext context, Usi
return;
}

if (usingDirective.Name == null)
{
// This happens for e.g. "using X = string;" or "using T = (X, Y);"
return;
}

var symbol = context.SemanticModel.GetSymbolInfo(usingDirective.Name, context.CancellationToken).Symbol;
if (symbol == null)
{
Expand Down

0 comments on commit ea5e0d9

Please sign in to comment.