Disable a Particular Checkstyle Rule for a Particular Line of Code

Disable a particular Checkstyle rule for a particular line of code?

Check out the use of the supressionCommentFilter at http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter. You'll need to add the module to your checkstyle.xml

<module name="SuppressionCommentFilter"/>

and it's configurable. Thus you can add comments to your code to turn off checkstyle (at various levels) and then back on again through the use of comments in your code. E.g.

//CHECKSTYLE:OFF
public void someMethod(String arg1, String arg2, String arg3, String arg4) {
//CHECKSTYLE:ON

Or even better, use this more tweaked version:

<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)"/>
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)"/>
<property name="checkFormat" value="$1"/>
</module>

which allows you to turn off specific checks for specific lines of code:

//CHECKSTYLE.OFF: IllegalCatch - Much more readable than catching 7 exceptions
catch (Exception e)
//CHECKSTYLE.ON: IllegalCatch

*Note: you'll also have to add the FileContentsHolder:

<module name="FileContentsHolder"/>

See also

<module name="SuppressionFilter">
<property name="file" value="docs/suppressions.xml"/>
</module>

under the SuppressionFilter section on that same page, which allows you to turn off individual checks for pattern matched resources.

So, if you have in your checkstyle.xml:

<module name="ParameterNumber">
<property name="id" value="maxParameterNumber"/>
<property name="max" value="3"/>
<property name="tokens" value="METHOD_DEF"/>
</module>

You can turn it off in your suppression xml file with:

<suppress id="maxParameterNumber" files="YourCode.java"/>

Another method, now available in Checkstyle 5.7 is to suppress violations via the @SuppressWarnings java annotation. To do this, you will need to add two new modules (SuppressWarningsFilter and SuppressWarningsHolder) in your configuration file:

<module name="Checker">
...
<module name="SuppressWarningsFilter" />
<module name="TreeWalker">
...
<module name="SuppressWarningsHolder" />
</module>
</module>

Then, within your code you can do the following:

@SuppressWarnings("checkstyle:methodlength")
public void someLongMethod() throws Exception {

or, for multiple suppressions:

@SuppressWarnings({"checkstyle:executablestatementcount", "checkstyle:methodlength"})
public void someLongMethod() throws Exception {

NB: The "checkstyle:" prefix is optional (but recommended). According to the docs the parameter name have to be in all lowercase, but practice indicates any case works.

How to disable a particular checkstyle rule for a particular line of code?

It is a bit late, for future reference:

you can use the SuppressWithNearbyCommentFilter this way:

<module name="SuppressWithNearbyCommentFilter">
<property name="commentFormat" value="CHECKSTYLE DISABLE ([\w\|]+) FOR (-?\d+) LINES"/>
<property name="checkFormat" value="$1"/>
<property name="influenceFormat" value="$2"/>
</module>

On the line where you want to disable the warning you can write:

// CHECKSTYLE DISABLE <WarningName> FOR <# of lines> LINES

for example

// CHECKSTYLE DISABLE MagicNumber FOR 2 LINES

The number can also be negative (if the warning is in some automatically generated code that you cannot touch).

How can you suppress checkstyle checks within a block of code only for specific rules?

Recommend reading the documentation on SuppressionCommentFilter (it is buried at bit) for lots of examples.

An example of how to do configure the filter is:

<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CSOFF\: ([\w\|]+)"/>
<property name="onCommentFormat" value="CSON\: ([\w\|]+)"/>
<property name="checkFormat" value="$1"/>
</module>

You can then use the following to turn off the RequireThis check for a block of code:

// CSOFF: RequireThis
... code
// CSON: RequireThis

How can I ignore a CheckStyle rule for a line matching a pattern if (log.isDebugEnabled()) {

Answered my own question. I add a suppression file to my checkstyle configuration file, where the suppression file had the following contents:

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionXpathFilter Experimental Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2_xpath_experimental.dtd">

<suppressions>
<suppress-xpath checks="LeftCurly"
query="//LITERAL_IF[.//METHOD_CALL/DOT/IDENT[@text='isDebugEnabled']]/SLIST"/>
</suppressions>

This finds all if statements that contain a reference to a isDebugEnabled call and matches on the following "list of statements" which begins with the leading left curly bracket.

Is there a way to force Checkstyle to ignore particular warning in the source code?

Yes, look at the SuppressionCommentFilter and SuppressWithNearbyCommentFilter options.

How do I Suppress Warnings in CheckStyle?

Seems pretty tedious but there needs to be explicit XML configuration to ignore it. You can probably find a GUI to do it via using the Checkstyle plugin for Eclipse or Netbeans. The example I've found is on the Checkstyle configuration page.

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
<suppress checks="JavadocStyleCheck"
files="AbstractComplexityCheck.java"
lines="82,108-122"/>
<suppress checks="MagicNumberCheck"
files="JavadocStyleCheck.java"
lines="221"/>
</suppressions>

Disallow Multi-Line Commenting via Checkstyle

It is not clear from your question, but I am assuming you want to ban all multi-line comments and not just specific styles of them.

This can be done with IllegalToken.

<module name="IllegalToken">
<property name="tokens" value="BLOCK_COMMENT_BEGIN"/>
</module>

Though it wasn't explicit, if you still want to allow Javadocs, which are just special block comments, you will have to use xpath suppression to allow them and ban the others. See SuppressionXpathFilter.



Related Topics



Leave a reply



Submit