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

Prefer Null-conditional operators over ternaries #57

Open
amanda-mitchell opened this issue Mar 5, 2019 · 3 comments · May be fixed by #75
Open

Prefer Null-conditional operators over ternaries #57

amanda-mitchell opened this issue Mar 5, 2019 · 3 comments · May be fixed by #75
Labels
new analyzer Suggestion for a new analyzer to add

Comments

@amanda-mitchell
Copy link
Contributor

The patterns

foo != null ? foo.Bar.OtherStuff : null

and

foo.HasValue ? foo.Value.Bar.OtherStuff : null // foo is of type Nullable<T>

Should be simplified to

foo?.Bar.OtherStuff
@EthanRutherford
Copy link

EthanRutherford commented Mar 8, 2019

VS is constantly listing this as an "info" level message for me. I'm unclear on the details, but perhaps this could be done by simply upping the log level on the built in analyzer?

@bagley2014 bagley2014 added the new analyzer Suggestion for a new analyzer to add label Oct 22, 2021
@jeffcorcoran jeffcorcoran linked a pull request Nov 12, 2021 that will close this issue
@bgrainger
Copy link
Member

As @EthanRutherford has commented, this is already available as IDE0031: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0031. No need to build our own version.

@bgrainger
Copy link
Member

I don't think IDE0031 catches Nullable<T>, so this analyser could handle that case:

foo.HasValue ? foo.Value.Bar.OtherStuff : null // foo is of type Nullable<T>

@bgrainger bgrainger reopened this Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new analyzer Suggestion for a new analyzer to add
Development

Successfully merging a pull request may close this issue.

4 participants