-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Validate diagnostic ID in Experimental attribute #70397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a7ff4a2
c4534ef
75e7b28
3bdd89b
6ad3134
7a9753e
a520b58
5d1c56d
c4f5f8f
caaf149
43ea3e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,6 +226,15 @@ protected void DecodeWellKnownAttribute(ref DecodeWellKnownAttributeArguments<At | |
| return; | ||
| } | ||
|
|
||
| if (arguments.Attribute.IsTargetAttribute(this, AttributeDescription.ExperimentalAttribute) | ||
| && !SyntaxFacts.IsValidIdentifier((string?)arguments.Attribute.CommonConstructorArguments[0].ValueInternal)) | ||
| { | ||
| Debug.Assert(arguments.AttributeSyntaxOpt is not null); | ||
|
||
| var attrArgumentLocation = arguments.Attribute.GetAttributeArgumentSyntaxLocation(parameterIndex: 0, arguments.AttributeSyntaxOpt); | ||
| arguments.Diagnostics.DiagnosticBag.Add(ErrorCode.ERR_InvalidExperimentalDiagID, attrArgumentLocation); | ||
| return; | ||
|
||
| } | ||
|
|
||
| DecodeWellKnownAttributeImpl(ref arguments); | ||
| } | ||
| #nullable disable | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14875,6 +14875,88 @@ public static void M() { } | |
| var comp = CreateCompilation(src, options: TestOptions.DebugExe.WithGeneralDiagnosticOption(ReportDiagnostic.Suppress)); | ||
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ExperimentalWithWhitespaceDiagnosticID() | ||
| { | ||
| var dir = Temp.CreateDirectory(); | ||
| var src = dir.CreateFile("test.cs").WriteAllText(""" | ||
| C.M(); | ||
|
|
||
| [System.Diagnostics.CodeAnalysis.Experimental(" ")] | ||
| public class C | ||
| { | ||
| public static void M() { } | ||
| } | ||
|
|
||
| namespace System.Diagnostics.CodeAnalysis | ||
| { | ||
| public sealed class ExperimentalAttribute : Attribute | ||
| { | ||
| public ExperimentalAttribute(string diagnosticId) { } | ||
| } | ||
| } | ||
| """); | ||
| var analyzerConfig = dir.CreateFile(".editorconfig").WriteAllText(""" | ||
| [*.cs] | ||
| dotnet_diagnostic.CS9208.severity = warning | ||
|
||
| """); | ||
| var cmd = CreateCSharpCompiler(null, dir.Path, new[] { | ||
| "/nologo", | ||
| "/t:exe", | ||
| "/preferreduilang:en", | ||
| "/analyzerconfig:" + analyzerConfig.Path, | ||
| src.Path }); | ||
|
|
||
| Assert.Equal(analyzerConfig.Path, Assert.Single(cmd.Arguments.AnalyzerConfigPaths)); | ||
|
|
||
| var outWriter = new StringWriter(CultureInfo.InvariantCulture); | ||
| var exitCode = cmd.Run(outWriter); | ||
| Assert.Equal(1, exitCode); | ||
| Assert.StartsWith("test.cs(3,2): error CS9208: The diagnosticId argument to the 'Experimental' attribute must be a valid identifier", | ||
| outWriter.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ExperimentalWithValidDiagnosticID() | ||
| { | ||
| var dir = Temp.CreateDirectory(); | ||
| var src = dir.CreateFile("test.cs").WriteAllText(""" | ||
| C.M(); | ||
|
|
||
| [System.Diagnostics.CodeAnalysis.Experimental("DiagID")] | ||
| public class C | ||
| { | ||
| public static void M() { } | ||
| } | ||
|
|
||
| namespace System.Diagnostics.CodeAnalysis | ||
| { | ||
| public sealed class ExperimentalAttribute : Attribute | ||
| { | ||
| public ExperimentalAttribute(string diagnosticId) { } | ||
| } | ||
| } | ||
| """); | ||
| var analyzerConfig = dir.CreateFile(".editorconfig").WriteAllText(""" | ||
| [*.cs] | ||
| dotnet_diagnostic.DiagID.severity = warning | ||
| """); | ||
| var cmd = CreateCSharpCompiler(null, dir.Path, new[] { | ||
| "/nologo", | ||
| "/t:exe", | ||
| "/preferreduilang:en", | ||
| "/analyzerconfig:" + analyzerConfig.Path, | ||
| src.Path }); | ||
|
|
||
| Assert.Equal(analyzerConfig.Path, Assert.Single(cmd.Arguments.AnalyzerConfigPaths)); | ||
|
|
||
| var outWriter = new StringWriter(CultureInfo.InvariantCulture); | ||
| var exitCode = cmd.Run(outWriter); | ||
| Assert.Equal(0, exitCode); | ||
| Assert.StartsWith("test.cs(1,1): warning DiagID: 'C' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.", | ||
| outWriter.ToString()); | ||
| } | ||
| } | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar refactoring suggestion as for VB #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the second thought, never mind. It looks like we do want to call
DecodeWellKnownAttributeImplbelow.