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
S1226: rule doesn't consider null checking as a read #2603
Comments
I think Sonar is ride here. You should do something like: public ReturnObject Foo(MyParamObject bar) {
var b = bar ?? new MyParamObject();
// ...
} Just as suggested, you should not reuse the parameter, but instead introduce a new variable. |
The rule description says
I read this as "Read the parameter at least one time, then you can reuse/overwrite it". Am I wrong ? |
#2555 is related to this |
@Elanis : I assume (always tricky) that the implementation triggers the rule when a reassign is done, no matter if is validated. The description is therefore confusing/not matching the behaviour. I would argue though, that you should never reassign a parameter, but introduce a new one, if you want to change the value before proceeding, just as I suggested. But let us wait what the Sonar guys/girls think about this. |
It is indeed a false positive. I'm closing this issue as a duplicate of #2555. |
Thanks for your answer :) |
Hello,
This issue is about rule S1226: Method parameters, caught exceptions and foreach variables' initial values should not be ignored.
When we run analysis on our projects with this kind of code:
Sonar detect it as a bug, but the variable is read (null checking). Is it a false positive ?
Sonarqube version: 7.9.1.27448
Sonar C# version: 7.16 (build 8981)
SonarScanner for Team Foundation Server version: 4.7.2
SonarScanner-Msbuild version: 4.6.2
Thanks :)
The text was updated successfully, but these errors were encountered: