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

Gray out unnecessary non-null suppression operator #26736

Closed
sharwell opened this issue May 9, 2018 · 7 comments
Closed

Gray out unnecessary non-null suppression operator #26736

sharwell opened this issue May 9, 2018 · 7 comments

Comments

@sharwell
Copy link
Member

sharwell commented May 9, 2018

Version Used: Nullable Reference Types Preview (7 May 2018)

Steps to Reproduce:

object? obj = new object();
var str = obj!.ToString();

Expected Behavior:

A warning is provided for the use of ! where flow analysis already determines the expression cannot be null.

Actual Behavior:

No warning is provided.

@jcouv
Copy link
Member

jcouv commented Nov 2, 2018

Last LDM discussion on this landed on:

These constructs are meaningless, but unlikely to do something the user didn't want. No compiler warnings for !!... or nonNull!. Maybe an IDE feature.

https://github.com/dotnet/csharplang/blob/master/meetings/2018/LDM-2018-08-20.md#12-meaningless--operators

@jcouv jcouv changed the title Warn if non-null assertion operator is used for non-null expression Gray out unnecessary non-null suppression operator Nov 2, 2018
@jcouv
Copy link
Member

jcouv commented Nov 2, 2018

Updated area to IDE and title to "gray out unnecessary suppression".

@jasonmalinowski
Copy link
Member

@jcouv @333fred If the IDE did want to implement this, what would be the recommended way to do so? We'd just call GetTypeInfo on the thing to the left and if it's NullableFlowState.NotNull then flag it? Or is there more subtlety needed here?

(We're keeing this in backlog for now but just curious if this is something we even could do or if I should move it to 'blocked by compiler API')

@333fred
Copy link
Member

333fred commented May 14, 2019

More subtlety will be needed here. The suppression operator is going to change what we report for GetTypeInfo, so NotNull will be reported for this. I think this will likely be blocked by #35412.

@jcouv
Copy link
Member

jcouv commented May 14, 2019

Definitely more subtle. The suppression operator also conversions and nested nullability. For example, List<string> x = new List<string?>()!; // warning is suppressed by ! so is "necessary".

@sharwell
Copy link
Member Author

This is blocked on #25372

@sharwell
Copy link
Member Author

Duplicate of #34714

@sharwell sharwell marked this as a duplicate of #34714 Oct 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants