Skip to content

Commit 845ba29

Browse files
0x084Evweijsters
authored andcommitted
use two different messages for namespaces and aliased types in SA1135
check if using is inside a namespace in SA1135
1 parent 77c3341 commit 845ba29

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ namespace System.Threading
3232

3333
DiagnosticResult[] expected =
3434
{
35-
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("System.IO"),
36-
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("System.IO"),
37-
this.CSharpDiagnostic().WithLocation(5, 5).WithArguments("System.Threading.Tasks"),
38-
this.CSharpDiagnostic().WithLocation(5, 5).WithArguments("System.Threading.Tasks")
35+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(4, 5).WithArguments("System.IO"),
36+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(4, 5).WithArguments("System.IO"),
37+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(5, 5).WithArguments("System.Threading.Tasks"),
38+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(5, 5).WithArguments("System.Threading.Tasks")
3939
};
4040

4141
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
@@ -61,10 +61,10 @@ namespace System.Threading
6161

6262
DiagnosticResult[] expected =
6363
{
64-
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("System.IO"),
65-
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("System.IO"),
66-
this.CSharpDiagnostic().WithLocation(5, 5).WithArguments("System.Threading.Tasks"),
67-
this.CSharpDiagnostic().WithLocation(5, 5).WithArguments("System.Threading.Tasks")
64+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(4, 5).WithArguments("System.IO"),
65+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(4, 5).WithArguments("System.IO"),
66+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(5, 5).WithArguments("System.Threading.Tasks"),
67+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorNamespace).WithLocation(5, 5).WithArguments("System.Threading.Tasks")
6868
};
6969

7070
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
@@ -90,10 +90,10 @@ namespace System.Threading
9090

9191
DiagnosticResult[] expected =
9292
{
93-
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("System.IO.Path"),
94-
this.CSharpDiagnostic().WithLocation(4, 5).WithArguments("System.IO.Path"),
95-
this.CSharpDiagnostic().WithLocation(5, 5).WithArguments("System.Threading.Tasks.Task"),
96-
this.CSharpDiagnostic().WithLocation(5, 5).WithArguments("System.Threading.Tasks.Task")
93+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorType).WithLocation(4, 5).WithArguments("System.IO.Path"),
94+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorType).WithLocation(4, 5).WithArguments("System.IO.Path"),
95+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorType).WithLocation(5, 5).WithArguments("System.Threading.Tasks.Task"),
96+
this.CSharpDiagnostic(SA1135UsingDirectivesMustBeQualified.DescriptorType).WithLocation(5, 5).WithArguments("System.Threading.Tasks.Task")
9797
};
9898

9999
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/ReadabilityResources.Designer.cs

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/ReadabilityResources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,12 @@
528528
<data name="SA1135Description" xml:space="preserve">
529529
<value>All using directives must be qualified.</value>
530530
</data>
531-
<data name="SA1135MessageFormat" xml:space="preserve">
531+
<data name="SA1135MessageFormatNamespace" xml:space="preserve">
532532
<value>Using directive for namespace '{0}' must be qualified</value>
533533
</data>
534+
<data name="SA1135MessageFormatType" xml:space="preserve">
535+
<value>Using directive for type '{0}' must be qualified</value>
536+
</data>
534537
<data name="SA1135Title" xml:space="preserve">
535538
<value>Using directives must be qualified</value>
536539
</data>

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@ namespace StyleCop.Analyzers.ReadabilityRules
1919
/// </para>
2020
/// </remarks>
2121
[DiagnosticAnalyzer(LanguageNames.CSharp)]
22-
public class SA1135UsingDirectivesMustBeQualified : DiagnosticAnalyzer
22+
internal class SA1135UsingDirectivesMustBeQualified : DiagnosticAnalyzer
2323
{
2424
/// <summary>
2525
/// The ID for diagnostics produced by the <see cref="SA1135UsingDirectivesMustBeQualified"/> analyzer.
2626
/// </summary>
2727
public const string DiagnosticId = "SA1135";
2828
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(ReadabilityResources.SA1135Title), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
29-
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(ReadabilityResources.SA1135MessageFormat), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
29+
private static readonly LocalizableString MessageFormatNamespace = new LocalizableResourceString(nameof(ReadabilityResources.SA1135MessageFormatNamespace), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
30+
private static readonly LocalizableString MessageFormatType = new LocalizableResourceString(nameof(ReadabilityResources.SA1135MessageFormatType), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
3031
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(ReadabilityResources.SA1135Description), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
3132
private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1135.md";
3233

33-
private static readonly DiagnosticDescriptor Descriptor =
34-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
34+
public static DiagnosticDescriptor DescriptorNamespace { get; } =
35+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatNamespace, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
3536

36-
private static readonly ImmutableArray<DiagnosticDescriptor> SupportedDiagnosticsValue =
37-
ImmutableArray.Create(Descriptor);
37+
public static DiagnosticDescriptor DescriptorType { get; } =
38+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatType, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
3839

3940
/// <inheritdoc/>
40-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => SupportedDiagnosticsValue;
41+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
42+
ImmutableArray.Create(DescriptorNamespace);
4143

4244
/// <inheritdoc/>
4345
public override void Initialize(AnalysisContext context)
@@ -58,7 +60,8 @@ private static void HandleUsingDeclaration(SyntaxNodeAnalysisContext context)
5860

5961
private static void CheckUsingDeclaration(SyntaxNodeAnalysisContext context, UsingDirectiveSyntax usingDirective)
6062
{
61-
if (usingDirective.StaticKeyword.IsKind(SyntaxKind.None))
63+
// Usings outside of a namepsace are always qualified.
64+
if (usingDirective.Parent is NamespaceDeclarationSyntax && usingDirective.StaticKeyword.IsKind(SyntaxKind.None))
6265
{
6366
string usingString = usingDirective.Name.ToString();
6467

@@ -71,7 +74,14 @@ private static void CheckUsingDeclaration(SyntaxNodeAnalysisContext context, Usi
7174
string symbolString = symbolInfo.Symbol.ToString();
7275
if (symbolString != usingString)
7376
{
74-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, usingDirective.GetLocation(), symbolString));
77+
if (symbolInfo.Symbol.Kind == SymbolKind.NamedType)
78+
{
79+
context.ReportDiagnostic(Diagnostic.Create(DescriptorType, usingDirective.GetLocation(), symbolString));
80+
}
81+
else
82+
{
83+
context.ReportDiagnostic(Diagnostic.Create(DescriptorNamespace, usingDirective.GetLocation(), symbolString));
84+
}
7585
}
7686
}
7787
}

0 commit comments

Comments
 (0)