-
Notifications
You must be signed in to change notification settings - Fork 468
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
RS0046: Avoid Opt suffix in nullable-enabled code #3352
Conversation
src/Roslyn.Diagnostics.Analyzers/UnitTests/AvoidOptSuffixForNullableEnableCodeTests.cs
Outdated
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/UnitTests/AvoidOptSuffixForNullableEnableCodeTests.cs
Outdated
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Outdated
Show resolved
Hide resolved
@Evangelink |
FYI: You will need to fix a new RS diagnostic once #3444 goes in. |
src/Roslyn.Diagnostics.Analyzers/Core/AvoidOptSuffixForNullableEnableCode.cs
Outdated
Show resolved
Hide resolved
foreach (var variable in fieldDeclaration.Declaration.Variables) | ||
{ | ||
if (variable.Identifier.Text.EndsWith("Opt", System.StringComparison.Ordinal) && | ||
context.SemanticModel.GetNullableContext(variable.SpanStart).AnnotationsEnabled()) |
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.
In theory it's possible to have some parts of a field declaration after the #nullable enable
but I don't think that would happen in Roslyn so we might want to check the context at the field declaration level rather than each variable.
Codecov Report
@@ Coverage Diff @@
## master #3352 +/- ##
==========================================
- Coverage 94.94% 94.93% -0.02%
==========================================
Files 1075 1078 +3
Lines 246518 246817 +299
Branches 14903 14918 +15
==========================================
+ Hits 234052 234310 +258
- Misses 10426 10456 +30
- Partials 2040 2051 +11 |
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Outdated
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Outdated
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Outdated
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
} | ||
|
||
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); | ||
var variableSymbol = semanticModel.GetDeclaredSymbol(variable, context.CancellationToken); |
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.
Can you try to write some test with error code where this can return null? Honestly, I am not sure but worth trying out few code snippets.
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.
I have tried a couple of tests and they all return a non-null symbol. If you have any suggestion I am happy to add a test. @sharwell Any idea?
src/Roslyn.Diagnostics.Analyzers/UnitTests/CSharpAvoidOptSuffixForNullableEnableCodeTests.cs
Outdated
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/UnitTests/CSharpAvoidOptSuffixForNullableEnableCodeTests.cs
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Outdated
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Outdated
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Show resolved
Hide resolved
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Show resolved
Hide resolved
- Fix resource names - Skip broken test - Avoid error in case the name has changed and is too short
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCode.cs
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
...lyn.Diagnostics.Analyzers/CSharp/CSharpAvoidOptSuffixForNullableEnableCodeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
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.
Overall, LGTM. I can merge once you address the minor suggestions.
I really like this analyzer/code fix and this would be good as a CA rule in future. Let us try to get this in as an RS rule, dogfood it in our repos, and if all looks good, we can promote it to a CA rule.
Thanks @Evangelink!
Fix #2920