GeezerVison is showing github.com — [java] AvoidLiteralsInIfCondition: false negative for expressions · Issue #2140 · pmd/pmd
[java] AvoidLiteralsInIfCondition: false negative for expressions #2140DescriptionAffects PMD Version:
Running against 6.19.0,6.20.0.
Rule:
AvoidLiteralsInIfCondition
Description:
Expression with constants sidesteps this rule. The rule is configured with allowed values: -1,0,1.
Code Sample demonstrating the issue:
package pmdtests;
public enum MulticastSender {
;
private static byte getTTL(String... args) {
try {
if (args.length > (1 + 1))
return (byte)Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
// empty catch block
}
return (byte)1;
}
}
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other] Activitylinusjf commented on Dec 5, 2019 Do we need a new rule to flag only literals in expressions? Sometimes, it might be Ok, though. adangel commented on Dec 6, 2019 I've read your description and stumbled across:
The rule is configured with allowed values: -1,0,1.
Together with the example
if (args.length > (1 + 1))
So, this is actually, works as expected: you defined a exception for 1, so these literals are ignored.
But, this is indeed a false negative, since the rule doesn't check literals in deeper expressions.
So, let's define a real minimal example, to reproduce this case:
public class Foo {
public void bar(int a) {
if (a > 3 + 5) {
}
}
}
For this example, I would expect a rule violation. IMHO, we don't need a extra flag: "3+5" is equally bad as "3". Both use hard-coded literals, and that's what this rule should highlight:
Avoid using hard-coded literals in conditional statements.
The rule would not be triggered, if constants are used instead of the literals, e.g. MAX_NODES + MAX_LEAVES would be ok and the preferred approach. The point is, that 3 is no meaning, but "MAX_NODES" has. changed the title [-][java] False negative for AvoidLiteralsInIfCondition[/-] [+][java] AvoidLiteralsInIfCondition: false negative for expressions[/+] on Dec 6, 2019linusjf commented on Dec 6, 2019 The information was about my configuration.
Whether it was relevant to the rule or not would become clear only after testing it as you did. I suspected that it bore no relevance at all.
Actually, it's a pity that a fix will take care of this. I quite enjoyed that I was able to do this via constants in expression. It is tiresome to define constants for values other than -1,0 and 1. I'll simply have to go back to the old-fashioned way of suppressing violations rather than burden my class with irrelevant constants.
If developers were to follow the rule blindly, it'd be a case of following the 'letter of the law'. Coding must be in keeping with the 'spirit of the law'. adangel commented on Dec 8, 2019
It is tiresome to define constants for values other than -1,0 and 1. I'll simply have to go back to the
old-fashioned way of suppressing violations rather than burden my class with irrelevant constants.
That's the whole point of the rule... If you use only irrelevant literals, then you probably don't need this rule at all. And, you know, you can configure the rule via the property ignoreMagicNumbers.
And by the way: You reported an issue about a false negative... Now you are saying, this false negative is correct behaviour? Sorry, I don't understand you.
If developers were to follow the rule blindly, it'd be a case of following the 'letter of the law'. Coding
must be in keeping with the 'spirit of the law'.
That's what suppressions are used for - exceptions for single cases.
In the end, it is your decision as a user of PMD, whether you use the rule, whether you configure the priority of the rule to be "build failing", just a warning or just ignore the rule altogether. linusjf commented on Dec 9, 2019 No, this false negative is not correct behaviour as envisioned by the purpose of the rule. But it is as per the implementation which failed to consider an expression of two or more constants.
With regards to failing a build, is it possible to run all the configured rules irrespective of priority but fail the build only if a threshold priority rule (say 3 or less) is violated? I'd like all the violations listed in the output file but the build must not fail unless the above happens. As I understand it, the current implementation only runs rules up to the minimumPriority specified.
https://pmd.github.io/latest/pmd_userdocs_tools_ant.html
As an adjunct, the priority of the rule must be displayed in the violation message. This can be added as an additional requirement to this issue:
#1961 adangel commented on Dec 9, 2019
No, this false negative is not correct behaviour as envisioned by the purpose of the rule. But it is as
per the implementation which failed to consider an expression of two or more constants.
Sorry, maybe I'm stupid. But I lost you again: Are you saying, we can close this issue, everything works as expected?
Btw: I've prepared a fix already, see #2150 . Since this fixes a false-negative (if it is really a false negative, seems we are not sure here) and this seems to appear very often, I could be really annoying, if we introduce this behavioral change to rule. So I'm inclined to introduce a flag, which we might change with PMD 7 by default.
As per your other question: I think, that's what the failurePriority property is doing in the maven build. It will run pmd and create the report in any case. It will then only fail the build, if there are violations of this priority (or less).
The "minimumPriority" option for the ant build is indeed something different: It will filter the rules that are being executed.
With ant, I think, you currently need to execute PMD twice to achieve what you want. linusjf commented on Dec 9, 2019 It is a false positive. What I meant in my original statement was that working with the conditional statement that way subverts the intention of the rule. Any good tester will find ways to break a program's behaviour. It's not something I'm accustomed to doing. So , for a change, I enjoyed it. It had nothing to do with the issue being a false positive or not. You misunderstood my statement. It was not my intention to make anyone appear stupid. How does it make you or anyone that, anyway?
I wouldn't want to run PMD twice. I'd rather have the output in the text file rather than on the console. That would allow the build to progress while developers can work on addressing the minor violations in parallel. In fact, you could even have a process where developers are emailed the static analysis errors as they happen in real time during the build. linusjf commented on Dec 9, 2019 You can close this issue since you have the fix already.
With regards to priorities, it can be considered as part of report stats.
#2116 linusjf commented on Dec 15, 2019 @adangel I'm not sure what you're expecting for this rule wrt to suppressing it. My misgivings are based on the fact that if the rule is taken to its extremes and implemented literally, I could be defining constants like TWO, THREE and so on. In my opinion, the only three magic numbers allowed by default ought to be -1,0 and 1. All other numbers would be either be a constant or the rule suppressed. I imagine this could be quite restrictive. On the other hand, setting more values in the rule definition would make it a catch-all. A middle path would be to permitting overriding values at the class level. But how do we do this? Two options come to mind. A custom annotation that introduces a compile-time dependency or a smart suppression text in the existing @SuppressWarnings annotation that allows users to override existing defaults. This would provide much-needed granularity without being disruptive.
Of course, this might be overkill since rules can be suppressed at the method level. added a commit that references this issue on Dec 16, 2019This issue will close once commit ed25318 is merged into the 'main' branch.ed25318 adangel commented on Dec 16, 2019
implemented literally, I could be defining constants like TWO, THREE and so on.
Sure, that could happen. But that's not what we suggest in the rule description. We explicitly say "with descriptive names maintainability is enhanced" (see https://pmd.github.io/latest/pmd_rules_java_errorprone.html#avoidliteralsinifcondition) and "TWO" is absolutely not more descriptive than "2". So, I don't think, that would be real problem.
A custom annotation that introduces a compile-time dependency or a smart suppression text in the existing @SuppressWarnings annotation that allows users to override existing defaults. This would provide much-needed granularity without being disruptive.
I'd try to avoid this. I imagine, this adds too much complexity (both for implementing this feature and for using this feature) that's not worth it IMHO. linusjf commented on Dec 16, 2019 Unless you want to implement suppressions at the package level. A rule would have to have a high irritability quotient for users to prefer a compile-time dependency. SpotBugs has a custom annotation but it works on class files and there is an alternative to the annotation via an XML exclusion filter file.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|