[core] Add rule to report unnecessary suppression comments/annotations#5609
[core] Add rule to report unnecessary suppression comments/annotations#5609
Conversation
Need to figure out how to test it
This comment was marked as outdated.
This comment was marked as outdated.
Compare suppressors by specificity
|
First off, thanks! I've long wanted to tackle this, it's a massive win; as shown by the number of findings it has. Spcially as we remove FPs over time that used to be suppressed. As for this…
I'm actually not against it… in my long list of "maybe interesting ideas" was having a Universal AST (UAST) in the form of interfaces, that languages could implement in their corresponding nodes; and some rules could be rewritten in terms of the UAST as language-agnostic rules (ie: Ensuring a single implementation allows us to make sure any fixes / improvements are applied across the board too. I see no reason why we wouldn't want to take such an approach here to be honest… |
|
Thank you for the feedback.
I think we are definitely going to share the implementation. But I'm not sure we should surface that to users, which is why one rule per language (which use the same rule class) is my preferred course for now. Actually for most language a default implementation that is in an internal ruleset would be enough though (with a reference as you show). I'm not sure it is possible though, as I don't think it is possible to override the language of a rule, and it is definitely not possible to define a rule that doesn't have a language for now. One thing I want to do with this rule is also have a java-specific implementation that also covers more |
adangel
left a comment
There was a problem hiding this comment.
Thanks for starting this effort! This will definitely help a lot to cleanup old suppressions...
But I'm not sure we should surface that to users
Having this implemented as an internal rule is IMHO ok - that's an implementation detail.
If we expose this as a rule, then there are other implications, that cannot be expressed in the ruleset (there is no defined execution order of the enabled rules, executing this rule alone doesn't work etc...).
What about having a feature toggle for this? So, you run "pmd check --report-unused-suppressions" - and internally, we will add the new internal rule to the analysis execution (like this happens now in this PR). We could then later still change this to be a different kind of rule or any other element (report summarizer, whatever, multi-file analysis aware rule, #5254, ...). How we implemented this is then hidden and can be changed.
As this is implemented as a rule, the results are reported as a rule violation. But as we have seen, these violations are special - they can't be suppressed. So maybe this "thing" (implemented now as a rule) doesn't produce RuleViolations, but something else, that is consumed by the reporters? That way, the reporter could put this into a special section and not mix it under the "normal" violations.
I'm just thinking out loud here...
That would mean, that we couple this suppression a bit more with the reporting infrastructure. But we have already some coupling: There is a feature toggle to report suppressed violations ("--show-suppressed").
Maybe we should add a new method in "FileAnalysisListener" to report such "special" violations (e.g. onUnusedSuppression)?
For documentation, we probably also should emphasize more the "suppression reason"/"user message" (see also #5454). And side note: When suppressing with annotation, it is not possible to provide a user message... But that's another topic. When we add such a functionality, then probably a rule "suppression without reason" could be useful.
I think there are very good reasons for having this as a rule.
About this point, I think it isn't as bad as you think. It is appropriate for this check to not be suppressible, otherwise you end up with recursive suppression... where does it end. The only constraint on this rule is that all violations that may be suppressed are emitted before this rule's execution. This defines an easy execution order, which should be completely transparent to users anyway: all rules that may be suppressed have to executed before all rules that may not be suppressed. There is no defined execution order within either of those two categories (as of now). There may be other rules that don't support suppressions in the future, maybe for instance, rules that don't report violations, but metric values, if we want to implement that. If we do make "not suppressible" applicable to more rules, then it is likely we want a marker interface to tag rules for instance. |
|
Thanks for the explanations!
Yes, I think you convinced me 😄 It makes sense to expose this as a rule/ruleviolations.
The only point here I have is: this is another special behavior for one special rule, that users need to be aware of - it differs from other rules. But true, it is logical and easy to understand, that this "report unnecessary suppression rule" needs to run at the end (after all other rules). I'm ok to start with that. For now we don't need to expose this feature to other rules (for now "UnnecessaryPmdSuppressionRule" is the only rule with that special behavior). The generalization of this behavior would be part of #5254 . FYI - I'm moving this PR to 7.14.0 now... and doing the release of 7.13.0. |
|
Compared to main: (comment created at 2025-05-30 16:04:12+00:00 for d24775b) |
adangel
left a comment
There was a problem hiding this comment.
Thanks!
This is basically, ready, isn't it?
I'll try to write up the release notes then and merge for the release tomorrow. As this is a experimental feature for now, I'd say it's fine, if it is not yet well documented on only available for java.
|
I just wonder if the new violations are not mostly FPs... They are related to igniring with |
This is not ready yet - we need to figure out whether the private methods are used or not.
…JavaAnnotationSuppressor.java
I think, this now as good as it gets now. We have a lot violations about unused suppressions on private static inner classes - which look on first glance legit. I think, this all can be improved later on - after all: we explicitly say, this is experimental. And this rule is not enabled by default. |
Describe the PR
Improve ViolationSuppressor to keep track of used/unused suppressors. Create a rule in pmd-core that reports those unused things using the ViolationSuppressors. The rule is special cased to execute after all other rules. Its violations cannot be suppressed. The rule implementation can be shared between all languages, sort of like XPathRule. We could have a ruleset in pmd-core with just this rule predefined (eg
category/core/bestpractices.xml), but I don't think it's the right way, even though it would save me some typing... For the purpose of having language specific tests and documentation, I will add a rule definition for each language, in category bestpractices.Side note, there are 100 unnecessary comments/annotations in pmd-core and pmd-java. Cleanup is overdue :)
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)