From 850f809c4a0f796f4cd200400207543b523f49d3 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 14 Dec 2017 13:56:40 -0800 Subject: [PATCH] Only the compiler analyzer can use CS or BC prefix for diagnostics (#23776) --- .../Diagnostics/DiagnosticAnalyzerTests.cs | 30 +++++++++++++++ .../CodeAnalysisResources.Designer.cs | 9 +++++ .../Core/Portable/CodeAnalysisResources.resx | 3 ++ .../DiagnosticAnalyzer/AnalyzerManager.cs | 17 +++++++++ .../Portable/xlf/CodeAnalysisResources.cs.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.de.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.es.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.fr.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.it.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.ja.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.ko.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.pl.xlf | 5 +++ .../xlf/CodeAnalysisResources.pt-BR.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.ru.xlf | 5 +++ .../Portable/xlf/CodeAnalysisResources.tr.xlf | 5 +++ .../xlf/CodeAnalysisResources.zh-Hans.xlf | 5 +++ .../xlf/CodeAnalysisResources.zh-Hant.xlf | 5 +++ .../CSharpAddImportCodeFixProvider.cs | 4 +- .../CSharpAddImportFeatureService.cs | 2 + .../GenerateTypeCodeFixProvider.cs | 3 +- ...arpUnboundIdentifiersDiagnosticAnalyzer.cs | 12 +++--- .../GenerateConstructorCodeFixProvider.cs | 3 +- .../Diagnostics/Analyzers/IDEDiagnosticIds.cs | 2 + .../VisualBasicAddImportCodeFixProvider.vb | 4 +- .../VisualBasicAddImportFeatureService.vb | 3 ++ .../GenerateTypeCodeFixProvider.vb | 3 +- .../VisualBasicSpellCheckCodeFixProvider.vb | 3 +- ...sicUnboundIdentifiersDiagnosticAnalyzer.vb | 6 +-- .../VisualBasicFullyQualifyCodeFixProvider.vb | 3 +- .../GenerateConstructorCodeFixProvider.vb | 5 ++- .../Desktop/CommonDiagnosticAnalyzers.cs | 38 +++++++++++++++++++ 31 files changed, 196 insertions(+), 19 deletions(-) diff --git a/src/Compilers/CSharp/Test/Semantic/Diagnostics/DiagnosticAnalyzerTests.cs b/src/Compilers/CSharp/Test/Semantic/Diagnostics/DiagnosticAnalyzerTests.cs index 88501479762..4338a5e4460 100644 --- a/src/Compilers/CSharp/Test/Semantic/Diagnostics/DiagnosticAnalyzerTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Diagnostics/DiagnosticAnalyzerTests.cs @@ -888,6 +888,36 @@ public void TestReportingDiagnosticWithInvalidId() .WithLocation(1, 1)); } + [Fact, WorkItem(23667, "https://github.com/dotnet/roslyn/issues/23667")] + public void TestReportingDiagnosticWithCSharpCompilerId() + { + string source = @""; + var analyzers = new DiagnosticAnalyzer[] { new AnalyzerWithCSharpCompilerDiagnosticId() }; + string message = new ArgumentException(string.Format(CodeAnalysisResources.CompilerDiagnosticIdReported, AnalyzerWithCSharpCompilerDiagnosticId.Descriptor.Id), "diagnostic").Message; + + CreateCompilationWithMscorlib45(source) + .VerifyDiagnostics() + .VerifyAnalyzerDiagnostics(analyzers, null, null, logAnalyzerExceptionAsDiagnostics: true, + expected: Diagnostic("AD0001") + .WithArguments("Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers+AnalyzerWithCSharpCompilerDiagnosticId", "System.ArgumentException", message) + .WithLocation(1, 1)); + } + + [Fact, WorkItem(23667, "https://github.com/dotnet/roslyn/issues/23667")] + public void TestReportingDiagnosticWithBasicCompilerId() + { + string source = @""; + var analyzers = new DiagnosticAnalyzer[] { new AnalyzerWithBasicCompilerDiagnosticId() }; + string message = new ArgumentException(string.Format(CodeAnalysisResources.CompilerDiagnosticIdReported, AnalyzerWithBasicCompilerDiagnosticId.Descriptor.Id), "diagnostic").Message; + + CreateCompilationWithMscorlib45(source) + .VerifyDiagnostics() + .VerifyAnalyzerDiagnostics(analyzers, null, null, logAnalyzerExceptionAsDiagnostics: true, + expected: Diagnostic("AD0001") + .WithArguments("Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers+AnalyzerWithBasicCompilerDiagnosticId", "System.ArgumentException", message) + .WithLocation(1, 1)); + } + [Fact, WorkItem(7173, "https://github.com/dotnet/roslyn/issues/7173")] public void TestReportingDiagnosticWithInvalidLocation() { diff --git a/src/Compilers/Core/Portable/CodeAnalysisResources.Designer.cs b/src/Compilers/Core/Portable/CodeAnalysisResources.Designer.cs index e5a359e8871..90c5fe44f39 100644 --- a/src/Compilers/Core/Portable/CodeAnalysisResources.Designer.cs +++ b/src/Compilers/Core/Portable/CodeAnalysisResources.Designer.cs @@ -405,6 +405,15 @@ internal class CodeAnalysisResources { } } + /// + /// Looks up a localized string similar to Reported diagnostic has an ID '{0}', which only a compiler should be reporting.. + /// + internal static string CompilerDiagnosticIdReported { + get { + return ResourceManager.GetString("CompilerDiagnosticIdReported", resourceCulture); + } + } + /// /// Looks up a localized string similar to constructor. /// diff --git a/src/Compilers/Core/Portable/CodeAnalysisResources.resx b/src/Compilers/Core/Portable/CodeAnalysisResources.resx index 89c2c956fdf..247f3d5e668 100644 --- a/src/Compilers/Core/Portable/CodeAnalysisResources.resx +++ b/src/Compilers/Core/Portable/CodeAnalysisResources.resx @@ -446,6 +446,9 @@ Reported diagnostic has an ID '{0}', which is not a valid identifier. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic '{0}' has a source location in file '{1}', which is not part of the compilation being analyzed. diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.cs index 9bdeda8dd3a..6c6824ffafe 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerManager.cs @@ -168,6 +168,11 @@ internal bool IsSupportedDiagnostic(DiagnosticAnalyzer analyzer, Diagnostic diag return true; } + if (IsCompilerReservedDiagnostic(diagnostic.Id)) + { + throw new ArgumentException(string.Format(CodeAnalysisResources.CompilerDiagnosticIdReported, diagnostic.Id), nameof(diagnostic)); + } + // Get all the supported diagnostics and scan them linearly to see if the reported diagnostic is supported by the analyzer. // The linear scan is okay, given that this runs only if a diagnostic is being reported and a given analyzer is quite unlikely to have hundreds of thousands of supported diagnostics. var supportedDescriptors = GetSupportedDiagnosticDescriptors(analyzer, analyzerExecutor); @@ -182,6 +187,18 @@ internal bool IsSupportedDiagnostic(DiagnosticAnalyzer analyzer, Diagnostic diag return false; } + private static bool IsCompilerReservedDiagnostic(string id) + { + // Only the compiler analyzer should produce diagnostics with CS or BC prefixes (followed by digit) + if (id.Length >= 3 && (id.StartsWith("CS", StringComparison.Ordinal) || id.StartsWith("BC", StringComparison.Ordinal))) + { + char thirdChar = id[2]; + return thirdChar >= '0' && thirdChar <= '9'; + } + + return false; + } + /// /// Returns true if all the diagnostics that can be produced by this analyzer are suppressed through options. /// diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.cs.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.cs.xlf index d899680f5a4..db8c7d51bfc 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.cs.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.cs.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.de.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.de.xlf index 57008a5683a..9236ff22000 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.de.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.de.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.es.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.es.xlf index e025854c2b5..e0ccbba0642 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.es.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.es.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.fr.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.fr.xlf index 3bcaa967417..fa607ce2dca 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.fr.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.fr.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.it.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.it.xlf index 1a88860834e..a9aa85f9ef2 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.it.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.it.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ja.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ja.xlf index 7da82f98270..9739b55c7e8 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ja.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ja.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ko.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ko.xlf index b855092e627..87da088509d 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ko.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ko.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pl.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pl.xlf index ca6b2fb555b..9590aaab85e 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pl.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pl.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pt-BR.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pt-BR.xlf index 69c27ea146e..964babfdc28 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pt-BR.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.pt-BR.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ru.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ru.xlf index 071a87e4e98..02afd9076f1 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ru.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.ru.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.tr.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.tr.xlf index aad24441885..805f847874f 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.tr.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.tr.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hans.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hans.xlf index 4a8afc829bf..2b31a581b1b 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hans.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hans.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hant.xlf b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hant.xlf index a1176f648b9..8bab76bd8dc 100644 --- a/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hant.xlf +++ b/src/Compilers/Core/Portable/xlf/CodeAnalysisResources.zh-Hant.xlf @@ -828,6 +828,11 @@ Warning: Could not enable multicore JIT due to exception: {0}. + + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + Reported diagnostic has an ID '{0}', which only a compiler should be reporting. + + \ No newline at end of file diff --git a/src/Features/CSharp/Portable/AddImport/CSharpAddImportCodeFixProvider.cs b/src/Features/CSharp/Portable/AddImport/CSharpAddImportCodeFixProvider.cs index 78637b56851..2c35c03856e 100644 --- a/src/Features/CSharp/Portable/AddImport/CSharpAddImportCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/AddImport/CSharpAddImportCodeFixProvider.cs @@ -4,6 +4,7 @@ using System.Composition; using Microsoft.CodeAnalysis.AddImport; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Packaging; using Microsoft.CodeAnalysis.SymbolSearch; @@ -122,7 +123,8 @@ internal static class AddImportDiagnosticIds CS0616, CS1580, CS1581, - CS8129); + CS8129, + IDEDiagnosticIds.UnboundIdentifierId); public static ImmutableArray FixableDiagnosticIds = FixableTypeIds.Concat(ImmutableArray.Create( diff --git a/src/Features/CSharp/Portable/AddImport/CSharpAddImportFeatureService.cs b/src/Features/CSharp/Portable/AddImport/CSharpAddImportFeatureService.cs index 352a6360246..791aa7deeb9 100644 --- a/src/Features/CSharp/Portable/AddImport/CSharpAddImportFeatureService.cs +++ b/src/Features/CSharp/Portable/AddImport/CSharpAddImportFeatureService.cs @@ -12,6 +12,7 @@ using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.LanguageServices; @@ -153,6 +154,7 @@ protected override bool CanAddImportForType(string diagnosticId, SyntaxNode node switch (diagnosticId) { case CS0103: + case IDEDiagnosticIds.UnboundIdentifierId: case CS0246: case CS0305: case CS0308: diff --git a/src/Features/CSharp/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.cs b/src/Features/CSharp/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.cs index 65cf62e645f..40cff496eb6 100644 --- a/src/Features/CSharp/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.CodeFixes.GenerateMember; using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.GenerateType; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -29,7 +30,7 @@ internal class GenerateTypeCodeFixProvider : AbstractGenerateMemberCodeFixProvid public override ImmutableArray FixableDiagnosticIds { - get { return ImmutableArray.Create(CS0103, CS0117, CS0234, CS0246, CS0305, CS0308, CS0426, CS0616); } + get { return ImmutableArray.Create(CS0103, CS0117, CS0234, CS0246, CS0305, CS0308, CS0426, CS0616, IDEDiagnosticIds.UnboundIdentifierId); } } protected override bool IsCandidate(SyntaxNode node, SyntaxToken token, Diagnostic diagnostic) diff --git a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUnboundIdentifiersDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUnboundIdentifiersDiagnosticAnalyzer.cs index 3fd2cee943d..e8f17b3784b 100644 --- a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUnboundIdentifiersDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUnboundIdentifiersDiagnosticAnalyzer.cs @@ -10,19 +10,19 @@ namespace Microsoft.CodeAnalysis.CSharp.Diagnostics [DiagnosticAnalyzer(LanguageNames.CSharp)] internal sealed class CSharpUnboundIdentifiersDiagnosticAnalyzer : UnboundIdentifiersDiagnosticAnalyzerBase { - private const string NameNotInContext = "CS0103"; - private readonly LocalizableString _nameNotInContextMessageFormat = new LocalizableResourceString(nameof(CSharpFeaturesResources.The_name_0_does_not_exist_in_the_current_context), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); + private readonly LocalizableString _nameNotInContextMessageFormat = + new LocalizableResourceString(nameof(CSharpFeaturesResources.The_name_0_does_not_exist_in_the_current_context), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); - private const string ConstructorOverloadResolutionFailure = "CS1729"; - private readonly LocalizableString _constructorOverloadResolutionFailureMessageFormat = new LocalizableResourceString(nameof(CSharpFeaturesResources._0_does_not_contain_a_constructor_that_takes_that_many_arguments), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); + private readonly LocalizableString _constructorOverloadResolutionFailureMessageFormat = + new LocalizableResourceString(nameof(CSharpFeaturesResources._0_does_not_contain_a_constructor_that_takes_that_many_arguments), CSharpFeaturesResources.ResourceManager, typeof(CSharpFeaturesResources)); private static readonly ImmutableArray s_kindsOfInterest = ImmutableArray.Create(SyntaxKind.IncompleteMember, SyntaxKind.ParenthesizedLambdaExpression, SyntaxKind.SimpleLambdaExpression); protected override ImmutableArray SyntaxKindsOfInterest => s_kindsOfInterest; - protected override DiagnosticDescriptor DiagnosticDescriptor => GetDiagnosticDescriptor(NameNotInContext, _nameNotInContextMessageFormat); + protected override DiagnosticDescriptor DiagnosticDescriptor => GetDiagnosticDescriptor(IDEDiagnosticIds.UnboundIdentifierId, _nameNotInContextMessageFormat); - protected override DiagnosticDescriptor DiagnosticDescriptor2 => GetDiagnosticDescriptor(ConstructorOverloadResolutionFailure, _constructorOverloadResolutionFailureMessageFormat); + protected override DiagnosticDescriptor DiagnosticDescriptor2 => GetDiagnosticDescriptor(IDEDiagnosticIds.UnboundConstructorId, _constructorOverloadResolutionFailureMessageFormat); protected override bool ConstructorDoesNotExist(SyntaxNode node, SymbolInfo info, SemanticModel model) { diff --git a/src/Features/CSharp/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.cs b/src/Features/CSharp/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.cs index ecde2465ac2..ea552767c49 100644 --- a/src/Features/CSharp/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.CodeFixes.GenerateMember; using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -23,7 +24,7 @@ internal static class GenerateConstructorDiagnosticIds public const string CS7036 = nameof(CS7036); // CS7036: There is no argument given that corresponds to the required formal parameter 'v' of 'C.C(int)' public static readonly ImmutableArray AllDiagnosticIds = - ImmutableArray.Create(CS0122, CS1729, CS1739, CS1503, CS7036); + ImmutableArray.Create(CS0122, CS1729, CS1739, CS1503, CS7036, IDEDiagnosticIds.UnboundConstructorId); public static readonly ImmutableArray TooManyArgumentsDiagnosticIds = ImmutableArray.Create(CS1729); diff --git a/src/Features/Core/Portable/Diagnostics/Analyzers/IDEDiagnosticIds.cs b/src/Features/Core/Portable/Diagnostics/Analyzers/IDEDiagnosticIds.cs index 46c34bbd85f..a659fec4dab 100644 --- a/src/Features/Core/Portable/Diagnostics/Analyzers/IDEDiagnosticIds.cs +++ b/src/Features/Core/Portable/Diagnostics/Analyzers/IDEDiagnosticIds.cs @@ -70,5 +70,7 @@ internal static class IDEDiagnosticIds public const string ErrorReadingRulesetId = "IDE1004"; public const string InvokeDelegateWithConditionalAccessId = "IDE1005"; public const string NamingRuleId = "IDE1006"; + public const string UnboundIdentifierId = "IDE1007"; + public const string UnboundConstructorId = "IDE1008"; } } diff --git a/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportCodeFixProvider.vb b/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportCodeFixProvider.vb index 6fde9782975..1c434aec534 100644 --- a/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportCodeFixProvider.vb +++ b/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportCodeFixProvider.vb @@ -4,6 +4,7 @@ Imports System.Collections.Immutable Imports System.Composition Imports Microsoft.CodeAnalysis.AddImport Imports Microsoft.CodeAnalysis.CodeFixes +Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.Packaging Imports Microsoft.CodeAnalysis.SymbolSearch @@ -100,7 +101,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddImport Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String) Get - Return ImmutableArray.Create(BC30002, BC30451, BC30456, BC32042, BC36593, BC32045, BC30389, BC31504, BC32016, BC36610, BC36719, BC30512, BC30390, BC42309, BC30182) + Return ImmutableArray.Create(BC30002, BC30451, BC30456, BC32042, BC36593, BC32045, BC30389, BC31504, BC32016, BC36610, + BC36719, BC30512, BC30390, BC42309, BC30182, IDEDiagnosticIds.UnboundIdentifierId) End Get End Property End Class diff --git a/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportFeatureService.vb b/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportFeatureService.vb index 4c957d378d5..00188eda79a 100644 --- a/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportFeatureService.vb +++ b/src/Features/VisualBasic/Portable/AddImport/VisualBasicAddImportFeatureService.vb @@ -5,6 +5,7 @@ Imports System.Threading Imports Microsoft.CodeAnalysis.AddImport Imports Microsoft.CodeAnalysis.AddImports Imports Microsoft.CodeAnalysis.CaseCorrection +Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.Formatting Imports Microsoft.CodeAnalysis.Host.Mef Imports Microsoft.CodeAnalysis.LanguageServices @@ -88,6 +89,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddImport Protected Overrides Function CanAddImportForNamespace(diagnosticId As String, node As SyntaxNode, ByRef nameNode As SimpleNameSyntax) As Boolean Select Case diagnosticId Case VisualBasicAddImportCodeFixProvider.BC30002, + IDEDiagnosticIds.UnboundIdentifierId, VisualBasicAddImportCodeFixProvider.BC30451 Exit Select Case Else @@ -120,6 +122,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddImport diagnosticId As String, node As SyntaxNode, ByRef nameNode As SimpleNameSyntax) As Boolean Select Case diagnosticId Case VisualBasicAddImportCodeFixProvider.BC30002, + IDEDiagnosticIds.UnboundIdentifierId, VisualBasicAddImportCodeFixProvider.BC30451, VisualBasicAddImportCodeFixProvider.BC32042, VisualBasicAddImportCodeFixProvider.BC32045, diff --git a/src/Features/VisualBasic/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.vb b/src/Features/VisualBasic/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.vb index bbe99c47cd0..4da4cacbf27 100644 --- a/src/Features/VisualBasic/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.vb +++ b/src/Features/VisualBasic/Portable/CodeFixes/GenerateType/GenerateTypeCodeFixProvider.vb @@ -6,6 +6,7 @@ Imports System.Threading Imports Microsoft.CodeAnalysis.CodeActions Imports Microsoft.CodeAnalysis.CodeFixes Imports Microsoft.CodeAnalysis.CodeFixes.GenerateMember +Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.GenerateType Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -26,7 +27,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.GenerateType Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String) Get - Return ImmutableArray.Create(BC30002, BC30182, BC30451, BC30456, BC32042, BC32043, BC32045, BC40056) + Return ImmutableArray.Create(BC30002, IDEDiagnosticIds.UnboundIdentifierId, BC30182, BC30451, BC30456, BC32042, BC32043, BC32045, BC40056) End Get End Property diff --git a/src/Features/VisualBasic/Portable/CodeFixes/Spellcheck/VisualBasicSpellCheckCodeFixProvider.vb b/src/Features/VisualBasic/Portable/CodeFixes/Spellcheck/VisualBasicSpellCheckCodeFixProvider.vb index ba2620f5624..871ec8c6ffa 100644 --- a/src/Features/VisualBasic/Portable/CodeFixes/Spellcheck/VisualBasicSpellCheckCodeFixProvider.vb +++ b/src/Features/VisualBasic/Portable/CodeFixes/Spellcheck/VisualBasicSpellCheckCodeFixProvider.vb @@ -4,6 +4,7 @@ Imports System.Collections.Immutable Imports System.Composition Imports Microsoft.CodeAnalysis.CodeFixes Imports Microsoft.CodeAnalysis.Completion +Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.SpellCheck Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -36,7 +37,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Spellcheck Public NotOverridable Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String) Get - Return ImmutableArray.Create(BC30002, BC30451, BC30456, BC32045) + Return ImmutableArray.Create(BC30002, IDEDiagnosticIds.UnboundIdentifierId, BC30451, BC30456, BC32045) End Get End Property diff --git a/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/VisualBasicUnboundIdentifiersDiagnosticAnalyzer.vb b/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/VisualBasicUnboundIdentifiersDiagnosticAnalyzer.vb index 4cd748e74f2..3717e49743f 100644 --- a/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/VisualBasicUnboundIdentifiersDiagnosticAnalyzer.vb +++ b/src/Features/VisualBasic/Portable/Diagnostics/Analyzers/VisualBasicUnboundIdentifiersDiagnosticAnalyzer.vb @@ -10,9 +10,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Diagnostics Friend NotInheritable Class VisualBasicUnboundIdentifiersDiagnosticAnalyzer Inherits UnboundIdentifiersDiagnosticAnalyzerBase(Of SyntaxKind, SimpleNameSyntax, QualifiedNameSyntax, IncompleteMemberSyntax, LambdaExpressionSyntax) - Private Const s_undefinedType1 As String = "BC30002" Private ReadOnly _messageFormat As LocalizableString = New LocalizableResourceString(NameOf(VBFeaturesResources.Type_0_is_not_defined), VBFeaturesResources.ResourceManager, GetType(VBFeaturesResources.VBFeaturesResources)) - Private Const s_undefinedType2 As String = "BC30057" Private ReadOnly _messageFormat2 As LocalizableString = New LocalizableResourceString(NameOf(VBFeaturesResources.Too_many_arguments_to_0), VBFeaturesResources.ResourceManager, GetType(VBFeaturesResources.VBFeaturesResources)) @@ -31,13 +29,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Diagnostics Protected Overrides ReadOnly Property DiagnosticDescriptor As DiagnosticDescriptor Get - Return GetDiagnosticDescriptor(s_undefinedType1, _messageFormat) + Return GetDiagnosticDescriptor(IDEDiagnosticIds.UnboundIdentifierId, _messageFormat) End Get End Property Protected Overrides ReadOnly Property DiagnosticDescriptor2 As DiagnosticDescriptor Get - Return GetDiagnosticDescriptor(s_undefinedType2, _messageFormat2) + Return GetDiagnosticDescriptor(IDEDiagnosticIds.UnboundConstructorId, _messageFormat2) End Get End Property diff --git a/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyCodeFixProvider.vb b/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyCodeFixProvider.vb index 33b199bd13c..e732b6bbfee 100644 --- a/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyCodeFixProvider.vb +++ b/src/Features/VisualBasic/Portable/FullyQualify/VisualBasicFullyQualifyCodeFixProvider.vb @@ -7,6 +7,7 @@ Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.CaseCorrection Imports Microsoft.CodeAnalysis.CodeFixes Imports Microsoft.CodeAnalysis.CodeFixes.FullyQualify +Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.Formatting Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -43,7 +44,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.FullyQualify Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String) Get - Return ImmutableArray.Create(BC30002, BC30451, BC30561, BC40056, BC32045) + Return ImmutableArray.Create(BC30002, IDEDiagnosticIds.UnboundIdentifierId, BC30451, BC30561, BC40056, BC32045) End Get End Property diff --git a/src/Features/VisualBasic/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.vb b/src/Features/VisualBasic/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.vb index df49a6a0890..727ac5686aa 100644 --- a/src/Features/VisualBasic/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.vb +++ b/src/Features/VisualBasic/Portable/GenerateConstructor/GenerateConstructorCodeFixProvider.vb @@ -6,6 +6,7 @@ Imports System.Threading Imports Microsoft.CodeAnalysis.CodeActions Imports Microsoft.CodeAnalysis.CodeFixes Imports Microsoft.CodeAnalysis.CodeFixes.GenerateMember +Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -21,8 +22,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateConstructor Friend Const BC30387 As String = "BC30387" ' error BC32006: Class 'Derived' must declare a 'Sub New' because its base class 'Base' does not have an accessible 'Sub New' that can be called with no arguments. Friend Const BC30516 As String = "BC30516" ' error BC30516: Overload resolution failed because no accessible 'Blah' accepts this number of arguments. - Friend Shared ReadOnly AllDiagnosticIds As ImmutableArray(Of String) = ImmutableArray.Create(BC30057, BC30272, BC30274, BC30389, BC30455, BC32006, BC30512, BC30387, BC30516) - Friend Shared ReadOnly TooManyArgumentsDiagnosticIds As ImmutableArray(Of String) = ImmutableArray.Create(BC30057) + Friend Shared ReadOnly AllDiagnosticIds As ImmutableArray(Of String) = ImmutableArray.Create(BC30057, IDEDiagnosticIds.UnboundConstructorId, BC30272, BC30274, BC30389, BC30455, BC32006, BC30512, BC30387, BC30516) + Friend Shared ReadOnly TooManyArgumentsDiagnosticIds As ImmutableArray(Of String) = ImmutableArray.Create(BC30057, IDEDiagnosticIds.UnboundConstructorId) End Class diff --git a/src/Test/Utilities/Desktop/CommonDiagnosticAnalyzers.cs b/src/Test/Utilities/Desktop/CommonDiagnosticAnalyzers.cs index 6662ed5771f..ddbf62f6678 100644 --- a/src/Test/Utilities/Desktop/CommonDiagnosticAnalyzers.cs +++ b/src/Test/Utilities/Desktop/CommonDiagnosticAnalyzers.cs @@ -461,6 +461,44 @@ public override void Initialize(AnalysisContext context) } } + [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] + public sealed class AnalyzerWithCSharpCompilerDiagnosticId : DiagnosticAnalyzer + { + public static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor( + "CS101", + "Title1", + "Message1", + "Category1", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true); + + public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Descriptor); + public override void Initialize(AnalysisContext context) + { + context.RegisterCompilationAction(compilationContext => + compilationContext.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.None))); + } + } + + [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] + public sealed class AnalyzerWithBasicCompilerDiagnosticId : DiagnosticAnalyzer + { + public static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor( + "BC101", + "Title1", + "Message1", + "Category1", + defaultSeverity: DiagnosticSeverity.Warning, + isEnabledByDefault: true); + + public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Descriptor); + public override void Initialize(AnalysisContext context) + { + context.RegisterCompilationAction(compilationContext => + compilationContext.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.None))); + } + } + [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] public sealed class AnalyzerWithInvalidDiagnosticSpan : DiagnosticAnalyzer { -- GitLab