• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / 23

30 May 2025 04:25PM UTC coverage: 78.377% (-0.2%) from 78.601%
23

push

github

adangel
[core] Add rule to report unnecessary suppression comments/annotations (#5609)

Merge pull request #5609 from oowekyala:new-rule-UnnecessarySuppression

17712 of 23434 branches covered (75.58%)

Branch coverage included in aggregate %.

159 of 328 new or added lines in 22 files covered. (48.48%)

36 existing lines in 4 files now uncovered.

38902 of 48799 relevant lines covered (79.72%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/impl/UnnecessaryPmdSuppressionRule.java
1
/**
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.rule.impl;
6

7
import java.util.List;
8
import java.util.Set;
9

10
import net.sourceforge.pmd.annotation.Experimental;
11
import net.sourceforge.pmd.lang.LanguageVersionHandler;
12
import net.sourceforge.pmd.lang.ast.Node;
13
import net.sourceforge.pmd.lang.ast.RootNode;
14
import net.sourceforge.pmd.lang.rule.AbstractRule;
15
import net.sourceforge.pmd.lang.rule.internal.RuleSets;
16
import net.sourceforge.pmd.reporting.InternalApiBridge;
17
import net.sourceforge.pmd.reporting.RuleContext;
18
import net.sourceforge.pmd.reporting.ViolationSuppressor;
19
import net.sourceforge.pmd.reporting.ViolationSuppressor.UnusedSuppressorNode;
20
import net.sourceforge.pmd.util.CollectionUtil;
21

22
/**
23
 * A rule that reports unused suppression annotations and comments.
24
 * This rule class supports any PMD language, but language-specific behavior
25
 * needs to be implemented to avoid false positives for some languages.
26
 * Violations of this rule cannot be suppressed. It is special cased
27
 * by {@link RuleSets} to execute after all other rules, so that whether
28
 * those produce warnings or not is known to this rule.
29
 *
30
 * @experimental Since 7.14.0. See <a href="https://github.com/pmd/pmd/pull/5609">[core] Add rule to report unnecessary suppression comments/annotations #5609</a>
31
 */
NEW
32
@Experimental
×
NEW
33
public class UnnecessaryPmdSuppressionRule extends AbstractRule {
×
34

35
    @Override
36
    public void apply(Node rootNode, RuleContext ctx) {
NEW
37
        assert rootNode instanceof RootNode;
×
38

NEW
39
        LanguageVersionHandler handler = rootNode.getAstInfo().getLanguageProcessor().services();
×
NEW
40
        List<ViolationSuppressor> suppressors = CollectionUtil.concatView(
×
NEW
41
            handler.getExtraViolationSuppressors(),
×
42
            InternalApiBridge.DEFAULT_SUPPRESSORS
43
        );
44

NEW
45
        for (ViolationSuppressor suppressor : suppressors) {
×
NEW
46
            Set<UnusedSuppressorNode> unusedSuppressors = suppressor.getUnusedSuppressors((RootNode) rootNode);
×
NEW
47
            for (UnusedSuppressorNode unusedSuppressor : unusedSuppressors) {
×
NEW
48
                ctx.addViolationNoSuppress(
×
NEW
49
                    unusedSuppressor.getLocation(),
×
NEW
50
                    rootNode.getAstInfo(),
×
NEW
51
                    unusedSuppressor.unusedReason()
×
52
                );
NEW
53
            }
×
NEW
54
        }
×
NEW
55
    }
×
56

57
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc