Open mgirolet-gl opened 6 months ago
Hi @mgirolet-gl,
Thank you for reporting this case, it's a really interesting one. I confirm it as a False Positive.
The reason is, that using dynamic anywhere, like DoSomething(anything)
, doesn't just use dynamic argument. The whole statement becomes dynamic by nature, because the compiler doesn't really know what will really be invoked and what are the usual consequences.
So anything is string && DateTime.DoSomething-well-known
is in reality interpreted
as anything is string && There's-some-other-uknown-dynamic-statement-here,-have-fun
Using dynamic type anywhere in the statement should prevent the rule from raising, so also IsTrue(anything) && ...
should not raise
Hey there, thanks for your reply!
Thanks for the explanation, I hadn't thought of it this way.
I had (wrongly) assumed it was due to a misinterpretation of the is
and out
keywords being combined in the if
, but it indeed seems to be a quirk of how the compiler interprets dynamic
variables in conditions as you explained since there's no compiling error when the input type isn't dynamic
.
Looking forward to see it fixed.
Thanks for all your work, you guys are the best!
Description
Whenever SonarQube detects an
if
statement alone in anotherif
statements, it flagscsharpsquid:S1066
to indicate to the developers that the twoif
statements can (and should) be merged into one.However, C# has a very interesting type resolving feature that allows to declare variables within
if
s:This is also possible with 'out' variables, although it behaves differently:
Now here's the glitch: for some reason, if you combine both possibilities into one statement, the compiler rejects it:
The workaround is to use two nested statements, which gets
csharpsquid:S1066
flagged even though we CANNOT merge both statements here:Repro steps
Expected behavior
SonarQube detects that the two
if
statements cannot be merged into a single one and doesn't flag it as an issue.Actual behavior
SonarQube assumes the two statements can be merged and flags it as a
S1066
issue.Known workarounds
Flagging the issue as a false positive, or separating the conditions in different functions (which would make the code more complex and harder to understand in my specific case)
Related information
I have also reported the issue on the Sonar Community forum.
Thanks for taking the time to read!