@@ -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