添加FindBugs排除规则后全部目标Bug被屏蔽,仅需忽略IMC类Bug如何解决?
Looks like you ran into a common logic pitfall with FindBugs' exclude configuration—your rule is doing the exact opposite of what you intended, which is why all four bugs disappeared instead of just the IMC_IMMATURE_CLASS_NO_TOSTRING one. Let's break this down and fix it:
Why Your Current Rule Is Broken
The rule you added:
<Match> <Not> <Bug code="IMC_IMMATURE_CLASS_NO_TOSTRING"/> </Not> </Match>
In findbugs-exclude.xml, the <Match> tag defines which bugs should be excluded (ignored). By wrapping the bug code in <Not>, you're telling FindBugs: "Exclude every bug that is NOT IMC_IMMATURE_CLASS_NO_TOSTRING". In practice, this can combine with default settings to end up hiding everything, which matches what you're seeing.
The Correct Rule to Ignore Only One Bug
To exclude just the IMC_IMMATURE_CLASS_NO_TOSTRING bug, you don't need the <Not> tag. Use this simple, direct match instead:
<Match> <Bug code="IMC_IMMATURE_CLASS_NO_TOSTRING"/> </Match>
Or you can use the shorter 3-letter bug code IMC (FindBugs recognizes both full bug names and their abbreviated codes):
<Match> <Bug code="IMC"/> </Match>
This tells FindBugs: "Ignore any bug with the code IMC", which is exactly what you want.
Extra Checks If You Still See Unexpected Behavior
If you're still having issues with unintended exclusions, try these steps:
- Double-check your
findbugs-exclude.xmlfor other<Match>rules that might be combining to hide more bugs than intended - Verify the bug codes match exactly what FindBugs reports: your four bugs use codes
OPM,IMC,PRMC, andRI - For extra precision, narrow the rule to specific classes or methods to avoid accidental exclusion elsewhere:
<Match> <Class name="com.your.package.YourTargetClass"/> <Bug code="IMC"/> </Match>
After updating your rule, you should see the other three bugs (OPM_OVERLY_PERMISSIVE_METHOD, PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS, RI_REDUNDANT_INTERFACES) reappear, with only IMC_IMMATURE_CLASS_NO_TOSTRING hidden.
内容的提问来源于stack exchange,提问作者klind

