-
Notifications
You must be signed in to change notification settings - Fork 133
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
AssignmentInConditional reports false positives for getters #447
Comments
Hmmm, interesting scenario. So far I can't think of a way to avoid that without also silencing a bunch of "legitimate" violations (actual mistakes of = instead of ==), e.g. |
Yeah, I agree in an A I believe that many of such problems can be avoided well if you write tests. A good set of unit test should easily reveal an unintended assignment, considering you would test not just happy paths. E.g. an infinite while loop due to an unintended assignment should be easily pointed out by any kind of unit test covering that line actually. Testing complements static code analysis. So this problem has quite a few faces. First, the rule is already useful as giving warnings, analogously to "security hotspots" in SonarQube, where only potential mistakes are told, but manual review is needed. Probably more correctness can be achieved for this rule with deeper analysis, but if I recall, that's an NP-hard problem. So we might want to stick to a strategy like "which guesses can result in less probable false positives." In other words, I propose that:
I know no better, but I'm open for better ideas. |
Re: warnings, what I have done in such cases is to set the rule priority to 4, and include those in the report(s), but those violations will not fail the build. But of course that applies to the rule in all cases. |
Using Gradle 5.6.4 (Groovy 2.5.4) with the codenarc plugin, the following example code produces a false positive for the
AssignmentInConditional
rule.The error is:
The problem is, when the right side of the assignment is not a constant or literal, but a function/closure call, then it is more likely to match the actual intention of the code author.
Yes, I am aware that the code above should be rewritten to functional style to make it more readable:
However, Groovy still does not have a shorthand equivalent for
ZipInputStream
entries handling (and probably never will), where the rule would report a violation too:Therefore, I would suggest to relax this rule somehow.
The text was updated successfully, but these errors were encountered: