如何在PMD中包含“无用的括号”规则

时间:2017-05-16 08:19:03

标签: pmd

我发现有很多参考从PMD中排除“无用的括号”。但我想包括这条规则。我的问题是,我没有得到任何PMD错误,而是没有多个无用的括号并运行pmd。请告诉我如何启用此规则以便我可以获得PMD错误 - “无用括号”。

在我的ruleset.xml中,编写了

这是我的ruleset.xml

<?xml version="1.0"?>
<ruleset name="Custom ruleset"
         xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">


    <description>
        NOTE !!!!  NOTE !!!!NOTE !!!!NOTE !!!!NOTE !!!!
        This list should be somewhat static. I have disabled most of the annoying checks.
        That said if we need to exclude rules we can.
        If there are specific instances we can use suppressions like:
        @SuppressWarnings("PMD.UseUtilityClass")  //Doesn't make sense as this is main...
        if we need to, but for the most part I would like to avoid suppressing and ignoring.
        If you do suppress, provide a note why.
        NOTE !!!!NOTE !!!!NOTE !!!!NOTE !!!!NOTE !!!!NOTE !!!!
    </description>

    <rule ref="rulesets/java/basic.xml"/>
    <rule ref="rulesets/java/braces.xml"/>
    <rule ref="rulesets/java/clone.xml"/>
    <rule ref="rulesets/java/codesize.xml"/>
    <!--<rule ref="rulesets/java/comments.xml"/>-->
    <rule ref="rulesets/java/coupling.xml">
        <exclude name="LawOfDemeter"/>
        <exclude name="LoosePackageCoupling"/>
    </rule>

    <rule ref="rulesets/java/design.xml">
        <exclude name="ConfusingTernary"/>
        <exclude name="UseLocaleWithCaseConversions"/>
    </rule>

    <rule ref="rulesets/java/empty.xml"/>
    <rule ref="rulesets/java/finalizers.xml"/>
    <!--<rule ref="rulesets/java/imports.xml"/>-->
    <rule ref="rulesets/java/javabeans.xml">
        <exclude name="BeanMembersShouldSerialize"/>
    </rule>
    <rule ref="rulesets/java/junit.xml"/>
    <rule ref="rulesets/java/logging-java.xml">
        <exclude name="GuardLogStatementJavaUtil"/>  <!--This rule assumes that we are using Log4J where a guard is needed
                                             but we are using slf4j where the guard isn't needed hence this is excluded  -->
    </rule>
    <rule ref="rulesets/java/optimizations.xml">
        <exclude name="LocalVariableCouldBeFinal"/>
        <exclude name="MethodArgumentCouldBeFinal"/>
        <exclude name="AvoidInstantiatingObjectsInLoops"/>
    </rule>
    <rule ref="rulesets/java/strictexception.xml"/>
    <rule ref="rulesets/java/strings.xml"/>
    <rule ref="rulesets/java/sunsecure.xml"/>
    <rule ref="rulesets/java/unnecessary.xml"/>
    <!--<rule ref="rulesets/java/unusedcode.xml"/>-->

</ruleset>

1 个答案:

答案 0 :(得分:0)

如果您查看文档,则可以看到您感兴趣的规则是有争议的规则集的一部分:https://pmd.github.io/pmd-5.7.0/pmd-java/rules/java/controversial.html#UnnecessaryParentheses

在规则集中,您不包含此规则集中的任何规则。因此,您应该将其添加到规则集中。您可以添加<rule ref="rulesets/java/controversial.xml"/>来添加整个规则集(不推荐),也可以通过添加<rule ref="rulesets/java/controversial.xml/UnnecessaryParentheses"/>

来添加此规则