5

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.

Community
  • 1
  • 1
raudi
  • 1,701
  • 1
  • 16
  • 16
  • 1
    Are you sure it's so important ? I mean I got a bunch of Warnings in all my projects in Eclipse even for release. If you know what you are doing, why would you care about a yellow sign in the margin in Eclipse ? – Snicolas Jun 20 '11 at 11:05
  • why don't you remove that check from checkstyle xml? – abalogh Jun 20 '11 at 11:10
  • 8
    @Snicolas It is very important. It is one thing to get better code quality. In our projects the code conventions contain setting for Warnings/Error in Eclipse and the whole project is warning-free. When you do not care about these Warnings then you will get projects with thousands of it in a very short time and no one cares about adding another 10 warnings... – Fabian Barney Jun 20 '11 at 11:13
  • 2
    @Snicolas: Honestly? I'd rarely even commit code in the development phase that has warnings... – musiKk Jun 20 '11 at 11:33
  • 4
    @Snicolas: shame on you :P There is no excuse to ever allow a compiler warning when you create and commit a new class. There is no excuse to commit changes to an existing class without also removing compiler warnings. This is code-quality and at it's core. – pap Jun 20 '11 at 11:56
  • Lol, I really wonder in which measure adding a SerialVersionId to a new Swing component class has anything to do with code quality. Everyone will let eclipse correct the warning by giving it 1 as ID... no ? Or having a unused import statement ? But you may be right guys, I am gonna think about it. – Snicolas Jun 20 '11 at 12:00
  • 3
    @Snicolas Without going into detail of certain warnings: When you and your team find some of them misleading or unnecessary then it is the right way to disable these warnings rather ignoring them. Ignoring them makes your list of warnings full with thousands of warnings and you'll miss the important ones then. In my point of view the 'Problems view' must always be totally clean. – Fabian Barney Jun 20 '11 at 12:28
  • Already solved. Please check the link [Click here][1] [1]: http://stackoverflow.com/questions/4023185/how-to-disable-a-particular-checkstyle-rule-for-a-particular-line-of-code –  Nov 20 '14 at 09:08

1 Answers1

12

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

Angelo
  • 166
  • 2
  • 4