-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
IDE0041 Null check can be simplified is shown for Reference Equals of all generic parameters #23581
Comments
My apologies for asking a question on an old issue, but I have recently run into this on VS Professional 15.8.5. I see that it should have been addressed in 15.7. Is that accurate? If so, there may have been a regression. The following code still has suggestions for IDE0041 in all places (including the original repro above):
|
I also see this in 15.8.3, with a non-generic reference type. |
@sharwell I see it too. Can you reopen this? |
cc @MaStr11 |
For unconstrained generic parameters, we should offer to simplify to roslyn/src/EditorFeatures/CSharpTest/UseIsNullCheck/UseIsNullCheckForReferenceEqualsTests.cs Lines 223 to 248 in 9892220
This behavior was discussed on the PR that fixed this issue: It seems that IDE0041 is properly reported, but the CodeAction to do the fix does nothing: This might already be fixed by @CyrusNajmabadi in commit Fix conflict between fixers that was preventing one from showing up. 856932a#diff-f2c45bae9d7dfdbb5253223b6ecfdb48 because if I add the code from above to a test in master MaStr11@77c29cd the test is green (it does the right thing, without any other changes). @CyrusNajmabadi Can you confirm that the current behavior in 15.8.5 is already fixed in master? PS: It seems the special caseing ( |
You're right, I just checked and this is fixed in master. Although it's a little weird that the code fix title says "Use 'is null' check". In my opinion it should say "Use '== null' check" here.
I'm assuming that the language feature would only be enabled for C# 8. If that's the case, this logic cannot be removed. It still has to work this way if somebody has LangVersion set to 7.3 or below. |
Thanks for checking, that it is fixed in master. I couldn't check it myself because I have trouble getting my Roslyn experimental hive working (once more).
That's right. I think we did so because this is temporary until C# 8 is released and it's an edge case that doesn't come up that often. The title already distinguishes between Line 40 in 9892220
Right. |
It is "sort of" temporary. C# 7 will not change and many users will (as always) continue using projects that have to support building in VS 2017, or even 2015 and avoid using C# 8 for a few years. It would be nice if the title was fixed, because this special case is not going to get removed any time soon. |
Closing this as the original bug was still fixed as described. Regressions can be opened as a new issue. Otherwise it makes a mess of the history and links between PRs and issues. |
I believe this would need a check for the
Based on this, I don't think we need to create a new issue, but we can if people want. |
I'm fine with this case having a slightly incorrect title. :) |
It works for me in master. So things seem fine :) |
Still not fixed in 15.8.7: Several samples: public T Value
{
get
{
var some = this as Some;
Code.AssertState(!ReferenceEquals(some, null), "Option has no value."); // IDE0041
return some.Value;
}
} public bool Equals(Option<T> other)
{
if (other == null)
return false;
var thisSome = this as Some;
var otherSome = other as Some;
if (ReferenceEquals(thisSome, null)) // IDE0041
return ReferenceEquals(otherSome, null); // IDE0041
if (ReferenceEquals(otherSome, null)) // IDE0041
return false;
return EqualityComparer<T>.Default.Equals(Value, other.Value);
} |
Yep I'm getting the same Looks like we need a new issue and a proper explanation of the situation |
What is |
You can open CodeJam.sln in this project and see all these warnings . |
I believe this is fixed in 15.9 |
Nope, just tested 15.9.0 and it is still the same. Also, the auto-correction of the suggested fix does nothing, it leaves the code untouched, even for the simplest cases. It currently says "is null" and not "== null". Edit: it seems that negation of the message doesn't work either, it doesn't say "is not null", nor "!= null": And considering the discussion above, I am not sure whether replacing it with |
@abelbraaksma the correct thing is to do nothing here. At least we do not apply the codefix. We should update the bug to say that the problem is the codefix being offered at all. Looks like someone added some logic so it wasn't applied anymore |
@CyrusNajmabadi looks like this was regressed in 15.9 |
Works for me on the latest bits: I can see the fix isn't in 15.9: https://github.com/dotnet/roslyn/blob/dev15.9.x/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullForReferenceEqualsCodeFixProvider.cs Specifically, it's missing these lines: Lines 31 to 32 in 76c02fe
So this is simply a case of #29458 not being included in 15.9 for some reason. @jmarolf I don't know how i can help here. The bug has been fixed. Roslyn has just chosen to not release it yet. Let me know if you need any more info. Thanks! |
Closing out. Have validated this is working in the 16.x line. |
IDE0041 should only be shown if the type is nullable or the generic parameter is constrained to a nullable type.
Executing the codefix will create invalid code.
Version Used:
Visual Studio Enterprise 2017 15.5 RTM
Steps to Reproduce:
Expected Behavior:
Not shown
Actual Behavior:
Message: IDE0041 Null check can be simplified is shown for the Reference Equals
Repro:
The text was updated successfully, but these errors were encountered: