Skip to content
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

Merged
merged 20 commits into from
Jun 6, 2020

Conversation

Evangelink
Copy link
Member

Fix #2920

@Evangelink Evangelink requested a review from a team as a code owner March 6, 2020 14:22
@Evangelink Evangelink changed the title RS0037: Avoid Opt suffix in nullable-enabled code RS0042: Avoid Opt suffix in nullable-enabled code Mar 20, 2020
@mavasani
Copy link
Contributor

@Evangelink
@sharwell is the expert in this space. I will let him review/help you out here.

@mavasani
Copy link
Contributor

FYI: You will need to fix a new RS diagnostic once #3444 goes in.

foreach (var variable in fieldDeclaration.Declaration.Variables)
{
if (variable.Identifier.Text.EndsWith("Opt", System.StringComparison.Ordinal) &&
context.SemanticModel.GetNullableContext(variable.SpanStart).AnnotationsEnabled())
Copy link
Member Author

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
Copy link

codecov bot commented Apr 6, 2020

Codecov Report

Merging #3352 into master will decrease coverage by 0.01%.
The diff coverage is 87.62%.

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

@Evangelink Evangelink requested review from sharwell and removed request for a team April 6, 2020 19:42
@Evangelink Evangelink changed the title RS0042: Avoid Opt suffix in nullable-enabled code RS0046: Avoid Opt suffix in nullable-enabled code Apr 27, 2020
@Evangelink
Copy link
Member Author

@sharwell, @mavasani PR ready for review.

@Evangelink Evangelink mentioned this pull request Apr 28, 2020
@Evangelink Evangelink requested a review from mavasani May 27, 2020 16:00
}

var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var variableSymbol = semanticModel.GetDeclaredSymbol(variable, context.CancellationToken);
Copy link
Contributor

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.

Copy link
Member Author

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?

@Evangelink Evangelink requested a review from mavasani May 28, 2020 09:52
- Fix resource names
- Skip broken test
- Avoid error in case the name has changed and is too short
Copy link
Contributor

@mavasani mavasani left a 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!

@mavasani mavasani merged commit 596f8f4 into dotnet:master Jun 6, 2020
@Evangelink Evangelink deleted the RS0037 branch June 6, 2020 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid 'Opt' suffix in nullable-enabled code
4 participants