Philipp Ytkin
Philipp Ytkin

Reputation: 55

Suppress all rules for certain class but leave one enabled

I want to suppress all checks for particular classes, but leave specific one enabled.

I don't want to list explicitly all the rules I want to suppress (like showed here: https://checkstyle.sourceforge.io/config_filters.html#SuppressionFilter), but rather to disable them all, then enable some specific one.

In documentation I found this to disable all checks with ".*":

<suppress files="[/\\]target[/\\]" checks=".*"/>

What I want is a short, elegant way, to add single rule here that will not be suppressed for this files. Please help me, or provide proof that only way to do it is to actually list all the rules to be suppressed explicitly.

Upvotes: 0

Views: 329

Answers (2)

Philipp Ytkin
Philipp Ytkin

Reputation: 55

strkk, I chose just to write regex rule to suppress all rule checks except for rules, that contain "Import" in their name. It doesn't matter if "Import" is in the beginning, middle or the end of the rule name. For that I used following regexp:

 checks="^((?!Import).)*$"/>

You can test my regex here: https://regex101.com/r/YC6QuU/1

strkk, your regex doesn't work in case the desired name is in the end or in the middle of the name. See: https://regex101.com/r/XvPISI/1

But if someone runs in same problem, consider using BeforeExecutionExclusionFileFilter, like strkk suggested.

Upvotes: 0

strkk
strkk

Reputation: 669

Basically, fastest way is to modify checks regex with negative lookahead, e.g.

<suppress files="[/\\]target[/\\]" checks="^(?!(YourCheckName)).+"/>

It wont suppress violations for your check.

But I would recommend other way - create separate config for one check and use BeforeExecutionExclusionFileFilter to include only files you want to check. This approach is better since you make it clearer and avoid a lot of unnecessary work, since suppressions are applied only after files are parsed and analysed.

Upvotes: 1

Related Questions