开发者

Checkstyle SuppressionCommentFilter not ignoring specified rule

I have a checkstyle.xml that looks something like this:

<module name="Checker">
    ....

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

    <module name="TreeWalker">
        <module name="LineLength">
     开发者_StackOverflow中文版       <property name="max" value="200"/>
        </module>
        ....
    </module>
</module>

In one of my classes, I have a line longer than 200 characters and put the following around it:

// CSOFF: LineLength
...
// CSON: LineLength

The line in question however is not ignored as part of checkstyle.

I have specified the following in the pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <configLocation>checkstyle.xml</configLocation>
            </configuration>
        </plugin>
    </plugins>
</build>

and executing this:

mvn checkstyle:checkstyle


Have you configured FileContentsHolder as documented?

<module name="TreeWalker">
    ...
    <module name="FileContentsHolder"/>
    ...
</module>


This wasn't working for me recently either, but the accepted answer is outdated since checkstyle 8.2:

remove FileContentsHolder module as FileContents object is available for filters on TreeWalker in TreeWalkerAudit Event.

However version 8.6 added SuppressWithPlainTextCommentFilter:

new Checker filter SuppressWithPlainTextCommentFilter as akin to Treewalker's SuppressionCommentFilter.

Instead of using SuppressionCommentFilter I used the above SuppressWithPlainTextCommentFilter and everything started working.

Example:

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

Now I can do

public static final int lowerCaseConstant; // CSOFF: ConstantNameCheck
public final static int MultipleERRORS;; // CSOFF: ALL


I came across a plan to use this module to ignore event auditing. Many of the comments noted earlier do not reflect the information posted in the documentation. The discussion will be about checkstyle 9.3. the main reason why SuppressionCommentFilter does not work according to the documentation is: This filter can only be specified in the TreeWalker module () and only applies to checks that are also defined in this module module. To filter out checks other than TreeWalker, such as RegexpSingleline, you must use SuppressWithPlainTextCommentFilter or a similar filter.

I also want to note an important note that checkC and checkCPP are similar to the absence in SuppressWithPlainTextCommentFilter.

Remember that module can contains the ignorePattern property which can help you to ignore value.

At the end i want to show you similar part of code, that i use in mine checkstyle.xml

<module name="Checker">
    <module name="SuppressWithPlainTextCommentFilter">
        <property name="offCommentFormat" value="CHECKSTYLE.OFF"/>
        <property name="onCommentFormat" value="CHECKSTYLE.ON"/>
    </module>
    <module name="TreeWalker">
       ...
    </module>
</module>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜