【问题标题】:checkstyle disallow SuppressWarnings annotation unless there is a comment nearbycheckstyle 不允许 SuppressWarnings 注释,除非附近有注释
【发布时间】:2019-02-27 15:34:59
【问题描述】:

在我们的项目中,我们有时必须抑制一些警告(例如,“WeakerAccess”可能会被抑制,因为项目也被用作另一个项目中的库,或者 instanceof 的“表达式始终为假”@ 已检查异常从一个掩盖了抛出该异常的事实的库中抛出)。

另一方面,仅仅添加一个抑制是不好的,因为它可能不清楚为什么会在那里。所以,我想添加一个 checkstyler 规则,如果附近有评论,它只允许 SuppressWarnings 注释。这应该足以让人们开始添加解释。

但我找不到这样做的方法。有这个块:

<module name="SuppressWarnings">
  <property name="format"
      value="^unchecked$|^unused$"/>
  <property name="tokens"
    value="
    CLASS_DEF,INTERFACE_DEF,ENUM_DEF,
    ANNOTATION_DEF,ANNOTATION_FIELD_DEF,
    ENUM_CONSTANT_DEF,METHOD_DEF,CTOR_DEF
    "/>
</module>

还有一些关于特殊 cmets 的东西来关闭一条线的 checkstyler,但这只是另一个抑制警告的事情,也需要解释......但是有没有办法说如果有任何抑制是可以的在附近评论(在前一行或同一行上)?

【问题讨论】:

    标签: checkstyle suppress-warnings maven-checkstyle-plugin


    【解决方案1】:

    我建议同时使用 2 次检查。使用SuppressWarningsCheck 标记您想要记录的方法,并显示一条错误消息,指出它是违规的,因为它没有记录。然后在添加文档时使用SuppressWithNearbyCommentFilter 禁止违反其他检查。为了使过滤器起作用,文档必须以特定文本开头,这样它就不会错误地抑制实际上没有文档的 SuppressWarnings。

    例子:

    $ cat TestClass.java
    public class TestClass {
        //SuppressWarnings: this is my reason for the suppression
        @SuppressWarnings("unchecked")
        void method() {
        }
    
        //this is just a comment and not a reason
        @SuppressWarnings("unused")
        void method2() {
        }
    
        @SuppressWarnings("unused")
        void noComment() {
        }
    }
    
    $ cat TestConfig.xml
    <?xml version="1.0"?>
    <!DOCTYPE module PUBLIC
              "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
              "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
    
    <module name="Checker">
        <property name="charset" value="UTF-8"/>
    
        <module name="TreeWalker">
        <module name="SuppressWarnings">
            <property name="format" value="^(unchecked|unused)$"/>
            <message key="suppressed.warning.not.allowed"
                 value="The warning ''{0}'' cannot be suppressed at this location unless a comment is given for the reason for the suppression." />
            <property name="tokens" value="CLASS_DEF,INTERFACE_DEF,ENUM_DEF,ANNOTATION_DEF,ANNOTATION_FIELD_DEF,ENUM_CONSTANT_DEF,METHOD_DEF,CTOR_DEF"/>
        </module>
        <module name="SuppressWithNearbyCommentFilter">
          <property name="commentFormat"
                    value="SuppressWarnings: .{10,}"/>
          <property name="checkFormat" value="SuppressWarnings"/>
          <property name="influenceFormat" value="3"/>
        </module>
        </module>
    </module>
    
    $ java -jar checkstyle-8.18-all.jar -c TestConfig.xml TestClass.java
    Starting audit...
    [ERROR] TestClass.java:8:23: The warning 'unused' cannot be suppressed at this location unless a comment is given for the reason for the suppression. [SuppressWarnings]
    [ERROR] TestClass.java:12:23: The warning 'unused' cannot be suppressed at this location unless a comment is given for the reason for the suppression. [SuppressWarnings]
    Audit done.
    Checkstyle ends with 2 errors.
    

    您会注意到有 2 个违规行为,但有 3 个 SuppressWarnings。第一个示例显示了如何正确抑制没有文档。第 2 条仅显示评论,但没有显示有关压制的文档,第 3 条根本没有评论。

    &lt;property name="format" value="^(unchecked|unused)$"/&gt;

    这指定了未经检查和未使用的抑制只需要文档。如果您想要除这 2 种以外的所有类型的文档,我建议使用表达式 "^((?!unchecked|unused).)*$"

    【讨论】:

    • 这个选项有两个问题:我不想强制执行任何特定样式的 cmets(部分原因是已经有 cmets 解释了抑制,我不想全部修复) .而且我不想允许为任何其他规则和案例抑制 checkstyler。
    • checkFormat 强制仅针对这些违规行为禁止此检查。它不会错误地压制其他东西。如果您不想要特定样式,只需将commentFormat 更改为.*,附近的任何评论都会禁止违规。您可以将influenceFormat 更改为较小的数字,这样就不会拾取其他 cmets。
    猜你喜欢
    • 2015-02-25
    • 2012-03-17
    • 1970-01-01
    • 2017-11-03
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 2012-10-15
    相关资源
    最近更新 更多