繁体   English   中英

Checkstyle SuppressionCommentFilter 不忽略指定的规则

[英]Checkstyle SuppressionCommentFilter not ignoring specified rule

我有一个 checkstyle.xml 看起来像这样:

<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">
            <property name="max" value="200"/>
        </module>
        ....
    </module>
</module>

在我的一个课程中,我有一行超过 200 个字符,并在其周围放置以下内容:

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

然而,有问题的行不会作为 checkstyle 的一部分被忽略。

我在 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>

并执行这个:

mvn checkstyle:checkstyle

您是否已按文档配置FileContentsHolder?

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

这最近对我也不起作用,但是自checkstyle 8.2以来,接受的答案已经过时了:

删除 FileContentsHolder 模块,因为 FileContents object 可用于 TreeWalkerAudit 事件中 TreeWalker 上的过滤器。

但是8.6 版添加了SuppressWithPlainTextCommentFilter

新的 Checker 过滤器 SuppressWithPlainTextCommentFilter 类似于 Treewalker 的 SuppressionCommentFilter。

我没有使用SuppressionCommentFilter ,而是使用了上面的SuppressWithPlainTextCommentFilter ,一切都开始工作了。

例子:

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

现在我可以做

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

我遇到了一个使用这个模块来忽略事件审计的计划。 前面提到的许多评论并未反映文档中发布的信息。 讨论将关于 checkstyle 9.3。 根据文档, SuppressionCommentFilter不起作用的主要原因是:此过滤器只能在 TreeWalker 模块 () 中指定,并且仅适用于也在该模块模块中定义的检查。 要过滤除 TreeWalker 之外的检查,例如 RegexpSingleline,您必须使用SuppressWithPlainTextCommentFilter或类似的过滤器。

我还想注意一个重要说明,即 checkC 和 checkCPP 类似于 SuppressWithPlainTextCommentFilter 中的缺失。

请记住,模块可以包含 ignorePattern 属性,它可以帮助您忽略值。

最后,我想向您展示我在我的 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM