-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[java] New Rule: UselessPureMethodCall #5907
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
Conversation
6e6d960 to
f2780b4
Compare
|
Compared to main: (comment created at 2025-08-05 20:30:31+00:00 for b652b17) |
6da70c9 to
8857c09
Compare
8857c09 to
aba727f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
The idea is good, to create a more general rule for useless method calls. However, I think the usage of pure is not related here.
Have you had a look at the implementation of UselessOperationOnImmutableRule? It is basically a whitelist of some types, and it simply checks, whether a method (that doesn't return void) is called, and the result is not used.
If a pure method (pure == side-effect free) returns void, such a method in itself would be non-sense (in the strict sense - but System.out.println would fall into this category). In any case, as it doesn't return a value, it's ok not to use the return value (which doesn't exist).
If a non-pure method returns void, that would be ok. As it doesn't return anything, it's ok not to use the return value (which doesn't exist).
If a pure method returns a value, and that returned value is not used, this would be a violation of this rule.
If a non-pure method returns a value, and that returned value is not sued, this would be a violation of this rule (whether a method should have a side-effect and additionally return a value at the same time is a different topic).
So, I think, we really don't need to guess, whether a method is pure or not (which we can only guess, as we only see the method call).
If we make the rule very simple and just report any method call, that returns a value and whose value is not used, it might have a couple of false positives. I didn't try it, did you? This might be the reason, why UselessOperationOnImmutableRule is implemented with kind of a whitelist (only certain types are considered).
And lastly, if we add this rule, I think, this would overlap with UselessOperationOnImmutable... so we need to decided, how to move forward (e.g. deprecating UselessOperationOnImmutable).
But maybe not: E.g. https://github.com/openjdk/jdk/blob/jdk-11%2B28/src/java.base/share/classes/java/time/temporal/TemporalUnit.java#L179 will be detected by UselessOperationOnImmutable, but not by UselessPureMethodCall ...
...a/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UselessPureMethodCallRule.java
Show resolved
Hide resolved
...a/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/UselessPureMethodCallRule.java
Outdated
Show resolved
Hide resolved
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/JavaRuleUtil.java
Outdated
Show resolved
Hide resolved
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/JavaRuleUtil.java
Outdated
Show resolved
Hide resolved
...a/src/test/java/net/sourceforge/pmd/lang/java/rule/errorprone/UselessPureMethodCallTest.java
Outdated
Show resolved
Hide resolved
|
@adangel thanks for the thourough review. To the main point: I don't think it's reasonable to expect that code should handle all return values , the following would trigger a warning on each line the property that ensures that you can remove the method call without changing the code's behavior is really the lack of side effects. As I learned by checking the regressions caused by this PR, there is no guarantee that even something as simple as Let me know if you think this is worth continuing. (btw. ErrorProne does have a rule for this: https://errorprone.info/bugpattern/ReturnValueIgnored ) |
78d6e8d to
41aa02e
Compare
41aa02e to
06e5cda
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the property that ensures that you can remove the method call without changing the code's behavior is really the lack of side effects.
Yes, you're right.
I thought maybe reusing that approach to generalize UselessOperationOnImmutable and AvoidLosingExceptionInformation would be viable.
From the regression report, it looks good. We don't seem to have much violations for UselessOperationOnImmutable / AvoidLosingExceptionInformation - so, not so many violations have been removed (in openjdk11 and spring at least). A coarse check on the results of the checkstyle project, it also looks fine there (for every removed violation, we have a similar violation of UselessPureMethodCall).
Your new method of course finds more cases, so overall we have more new violations than removed.
Maybe we can improve the rule description of the new rule a bit. For me, I was not so familiar with the term "pure method". I guess, we should give some short explanation there e.g. with "side-effect". And also mention, that having such a code could be either useless (the fix is, to remove it) or a bug (not using the result value - the fix is, to use the result). And provide some prominent examples (like immutable classes, or the exception.getCause call, or List.size()).
Otherwise, I'm fine with this PR. Thank you for contributing!
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/JavaRuleUtil.java
Show resolved
Hide resolved
...c/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/UselessPureMethodCall.xml
Outdated
Show resolved
Hide resolved
...c/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/UselessPureMethodCall.xml
Show resolved
Hide resolved
Co-authored-by: Andreas Dangel <[email protected]>
Merge pull request pmd#5907 from zbynek:unused-pure-result
Describe the PR
Add a new rule that verifies that results of pure methods are used. Generalizes both
AvoidLosingExceptionInformationandUselessOperationOnImmutableRule, but more generic, since it's not restricted to catch blocks or immutable types.Deprecates the following rules
Related issues
#5881
Ready?
./mvnw clean verifypasses (checked automatically by github actions)