diff --git a/src/Compilers/CSharp/Test/Semantic/Diagnostics/DiagnosticAnalyzerTests.cs b/src/Compilers/CSharp/Test/Semantic/Diagnostics/DiagnosticAnalyzerTests.cs index 885014797626cb924f5fe4cd8fa3d9d3e730e07b..4338a5e44603a3d891ffa751cfd1fc4213f96885 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 e5a359e88712e40e8cce29c36a94990120dc273e..90c5fe44f39af09531007e0b9e31bb81b3cb7a21 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 89c2c956fdf750f7c5a2ac0557c47f19f8df1f1b..247f3d5e66873da0b302f6918530757c34a84d4d 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 9bdeda8dd3a84f132fe74a43b8b0d7b0b69c7e2e..6c6824ffafed59166dab32ec13edd3715bd73b63 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 d899680f5a4befaf4821cc33c691b09a35e0212b..db8c7d51bfc36d4cd1aa7905e27065b21cf14e71 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 57008a5683abcd7f7e6b20b58ed99ebbf2cefe9c..9236ff22000455ac36499701156105d4ea0d1d3d 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 e025854c2b5c7c3ea850bb84fa587cb86acbc613..e0ccbba0642d7655000560c6dffa8e3a624fc42a 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 3bcaa9674177b749045af783a9ae5dc2f6193155..fa607ce2dca585c1dbb234d1a4e9d7e614e57597 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 1a88860834e823c27c3139136c851a62607de24c..a9aa85f9ef283297d1786c0e4890064054fd033c 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 7da82f982705fa206d5e2c01a0c34cdbe99320e5..9739b55c7e8e2a1caa17f95ab9e56ababc2d343a 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 b855092e62799acc1f0a2b3b30bcb8e152941d5c..87da088509d0efb888155362a11d83fdf02a1767 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 ca6b2fb555b3f29c1759cd7946abdabda881c162..9590aaab85edcc9d860f5e8e6b85ac3c23505658 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 69c27ea146e5ec38027e49dfc786b716185a7143..964babfdc28ae1c09b3929e9a363fe23f96bd5bd 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 071a87e4e98b0511c080631aee4eb2419bccb6d5..02afd9076f1a6d59e6aa072ee80dd2abca7802f0 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 aad2444188568840d241f2aa0c037c282a4eaddb..805f847874f8ba90fec5107d6df19dc1e62556ec 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 4a8afc829bf5ba7b76c77d3f60997addd7a99d4d..2b31a581b1b1f12051846ff82173f1ee367fd755 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 a1176f648b93db7a71955bf146cda7a30fe894b5..8bab76bd8dcf3238905e4c4e513bd4c6f30e0ac6 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 78637b56851b400e8ad355d71157aaa261e1f996..2c35c03856eda3f9ff0ee352138c61c34a7038dd 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 352a6360246c2fe7cca6f8470476dc844920c43a..791aa7deeb9ba1864ce229bd917aaf650b20bc1c 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 65cf62e645fdbdf81d9938a5d118032de28bca87..40cff496eb63bb56a10fa52d1a6576d66ebb6637 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 3fd2cee943df3d6354f99f67bbb4354ef1e3cca6..e8f17b3784b4b6c08068580caa45e0bd4860f010 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 ecde2465ac2a45688eb60882a9625366b1d06b0c..ea552767c49ddeeacfea1991c5b0524d856492ee 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 46c34bbd85fa10fb318b6453bf8f5b5bac48442c..a659fec4dab3db6d8456e411e5c0455b10cb7bdd 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 6fde97829755af73c2165c5652252575dc8a5e02..1c434aec534aad948df4e06751428280dff19b28 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 4c957d378d5e333b1f1f06ffda5efed6ead35f98..00188eda79a3c332c572f24c5ddb7726d0e98f73 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 bbe99c47cd0efbade70bfbe5583d9bd5fdc87945..4da4cacbf27493fd427919625fe9f169eacac3e1 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 ba2620f5624be5082eeda2fbd906d85a1f853e93..871ec8c6ffafe042e46c7fcdaffcd5cf550a5d9d 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 4cd748e74f2e7474f4a9f79f7d7958d0fbc740ee..3717e49743f44343671ac29918304c908d3f2f4d 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 33b199bd13c54fd810bcc8516365c8acc099d951..e732b6bbfee0092b865224e2b08ad49abe16af31 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 df49a6a0890eeb120fcb9557ba5ceecc320acbf6..727ac5686aa94632fefe3353224045984c54014b 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 6662ed5771f0c5ded5a18e347c3047edf7cfbdc9..ddbf62f6678c34b414fc4405b7f8a68a820baf64 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 {