-
Notifications
You must be signed in to change notification settings - Fork 231
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
Fix S2583 FP: Property pattern match in else-if condition #5002
Comments
Hi @a10r, Thanks for reporting these. I confirm them as False Positive. |
Just to make sure this will also be covered: It also triggers when the patterns are not about type matching, but just comparing a member value, like in the following fragment (where the
|
It also hits the false positive if the patterns are not connected in an if-else if-else chain. E.g. the following is enough: public class Test
{
public int First { get; set; }
public int Second { get; set; }
public static int ShowBug(Test testee)
{
if (testee is { First : 0 })
return 1;
if (testee is { Second : 0 }) // <-- raises S2583
return 2;
return 0;
}
} This bug is live in the recent release of SonarLint for Visual Studio and in case of my own projects, it's making a thorough mess of it in some methods heavy on pattern matching. If a fix can't be made on short term, then a rollback to whatever the previous implementation of this rule was, would be nice instead. Don't really want to roll back SonarLint since the recent version also fixes problems connecting to older instances of SonarQube. |
Fixed by #7750 |
We found some false positive S2583 warnings in our code. ("Change this condition so that it does not always evaluate to 'false'; some subsequent code is never executed.")
Consider the following example:
S2583 is generated for the conditions in the lines marked [2], [3] and [4].
S2583 is not generated for the first condition in the if-else chain (line [1]).
It appears that using a property pattern causes all following property patterns in the same if-else chain to be evaluated incorrectly. For example, changing the condition in line [2] to
demo is B b
and then accessing the property viab.Inner
does not generate a warning for [2], even though it is functionally the same. Also, changing the condition in [1] todemo is A a
makes the warning in line [2] disappear, but not the warnings in lines [3] and [4].The type of property patterns does not seem to matter, as shown in the example. The conditions [2] and [3] are slightly different: [2] only unwraps the property, [3] also does a null-check.
Expected behavior
S2583 should not be raised since all conditions in the example can evaluate to true given a matching object.
Known workarounds
No known workarounds.
Related information
The text was updated successfully, but these errors were encountered: