-
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
Suppress 'IDE0041 Null check can be simplified' for unconstraint generic parameters. #24173
Suppress 'IDE0041 Null check can be simplified' for unconstraint generic parameters. #24173
Conversation
Another option might be to generate |
This seems to be the better option (I just discovered this option after I finished the PR). Are there any downsides? |
Not that i can tell. Make it so! |
Oh my gosh! I just read dotnet/csharplang#505 and discovered that public void TheFunction<T>(T a) where T : struct {
var c = (a != null); // CS0019: Operator '!=' cannot be applied to operand of type 'T' and '<null>'
} Public Sub TheFunction(Of T As Structure)(a As T)
If a Is Nothing Then 'BC30020: 'Is' operator does not accept operands of type 'T'. Operands must be reference or nullable types.
End If
End Sub I will update the PR to suppress on value types now. |
@@ -36,7 +36,7 @@ class C | |||
{ | |||
void M(string s) | |||
{ | |||
if (s is null) | |||
if (s == null) |
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.
this seems like a regression.
@@ -17,12 +17,16 @@ protected override string GetIsNullTitle() | |||
protected override string GetIsNotNullTitle() | |||
=> GetIsNullTitle(); |
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.
the feature is literall the "Use 'is null'" feature :) You're now using "== null".
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.
Yes. But I thought we agreed on that. Did I got you wrong?
Yes (my fault). I meant specifically using '==' only for the cases where 'is' wouldn't work. |
basically:
|
That's fine to me (I'm on the road tomorrow so this will not be done until Wednesday. Sorry.) |
No worries :) |
I changed the logic as requested by @CyrusNajmabadi. I was wondering if it is worth to document somewhere that this fixer can be simplified once this is done:
There seems to be no issue in the language repo yet to link to. |
you could make one :) Then link to that in the code with a note. |
=> ((PrefixUnaryExpressionSyntax)notExpression).WithOperand((ExpressionSyntax)CreateIsNullCheck(argument)); | ||
|
||
protected override SyntaxNode CreateNullCheck(SyntaxNode argument, bool isUnconstraintGeneric) |
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.
Unconstrained (though Unconstraint is cute :))
@@ -108,9 +119,18 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, IMethodSymbol refe | |||
return; | |||
} | |||
|
|||
var properties = ImmutableDictionary<string, string>.Empty; | |||
|
|||
switch (GetGenericParameterConstraintType(syntaxFacts, semanticModel, arguments[0], arguments[1], 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.
a simpler approach seems like it would be if you just had this method return the type parameter if there is one.
then you could do:
if (typeParameter?.HasValueConstraint == true)
{
return;
}
else if (...?.HasReferenceConstraint == true)
{
...
}
Then there's no need for the enum to shuttle data across that's already in the ITypeParameter.
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 logic LGTM. I think there are some things that can be simplified though.
@Pilchie Requesting approval for 15.7 before we do the work to rebase this 😄 |
I'm in favor of fixing false positive diagnostic results as early as possible, so I would be supportive of this for 15.7. |
@MaStr11 This pull request needs to be rebased onto the dev15.7.x branch so we can get it to users earlier. Let me know if you'd like to do this yourself, or if you'd like me to go ahead and do it for you. 👍 |
@sharwell I changed the base branch but it seems there are some unrelated commits are also part of the PR. I'm not able to fix (because I don't know how to do it and because I'm currently on vacation and don't have my laptop with me). If there is anything that needs to be done it would be great if you could do it. |
@MaStr11 Thanks I'm on it! |
…ll) to (v == null).
case 'type constraint': no fix offered case 'reference constraint': C# use '==null' instead of 'is null' default: C# 'is null', VB 'IsNothing'
1f1a266
to
bffe221
Compare
@Pilchie for approval |
I know I'm late to the game here, but this needs to be documented: do not do this! It's is not the same thing. A |
Customer scenario
IDE0041 Null check can be simplified
is shown for theReferenceEquals
method and a fix is offered. The fix generates code which might have the compiler error 'Can not convert null to type parameter T because it could be a non-nullable value type.'. This fix suppresses IDE0041 if the parameter passed to ReferenceEquals isan unconstraint generic type parameterUpdate 01/17/2018: a value type constraint generic type parameter.Bugs this fixes
#23581
Workarounds, if any
Suppress IDE0041 with a pragma or change the generated code from
(value is null)
to(value == null)
.Risk
Low. Small additional check.
Performance impact
Low. Syntax and semantic analysis has already been done by other checks on the code path before. This adds some more checks.
Is this a regression from a previous update?
/
Root cause analysis
/
How was the bug found?
Customer reported #23581
Test documentation updated?
/