Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Yes, I saw this question:

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

But I can not comment and I wanted to ask if it is possible to do what the OP asked:

How to disable one particular rule for a line of code

I also saw this: Is there a way to force Checkstyle to ignore particular warning in the source code?

I guess the answer to my question is to use SuppressWithNearbyCommentFilter

I just don't understand what my comments in java should look like.

Say I have the following code:

public class MyDataContainer {
  /** the name */
  public String name;
}

and I want to get rid of the "Visibility Modifier" warning and only that warning.

What do I have to enable (using the eclipse--cs.sf.net plugin)?

And how do my comments have to look?

I managed to enable Suppresion Comment Filter and surround my whole class with

//CHECKSTYLE:OFF
//CHECKSTYLE:ON

but I don't really like this solution.

like image 933
raudi Avatar asked Jun 20 '11 10:06

raudi


People also ask

How do I turn off checkstyle rules?

Then to disable Checkstyle, you use //CHECKSTYLE:OFF and //CHECKSTYLE:ON (default values) in your code.

How do I turn off checkstyle in eclipse?

In latest eclipse version(9/2014) we have to visit Help -> Eclipse Market and go to installed tab. Then select uninstall. After uninstallation this will ask for a eclipse restart. And that is it.


1 Answers

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).

like image 64
Angelo Avatar answered Sep 24 '22 19:09

Angelo