Skip to content

UnnecessaryParenthesesCheck does not flag unnecessary parentheses in conditional expression #15263

@mahfouz72

Description

@mahfouz72

detected at #15250 (comment)
I have read check documentation: https://checkstyle.org/checks/coding/unnecessaryparentheses.html#UnnecessaryParentheses
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words


PS D:\CS\test> javac src/Test.java
PS D:\CS\test> cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
  <property name="haltOnException" value="true"/>
  <module name="TreeWalker">
    <module name="UnnecessaryParentheses">
    </module>
  </module>
</module>
PS D:\CS\test> cat src/Test.java

public class Test {
    void test(int x) {
        boolean a = (x == 3); // violation
        boolean b = (!(x == 3)); // violation

        boolean c = (x == 3) ? (x == 3) : (!(x == 3));  // expected 3 violations
    }

}
PS D:\CS\test> java  -jar checkstyle-10.17.0-all.jar -c config.xml  src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:4:21: Unnecessary parentheses around assignment right-hand side. [UnnecessaryParentheses]
[ERROR] D:\CS\test\src\Test.java:5:21: Unnecessary parentheses around assignment right-hand side. [UnnecessaryParentheses]
Audit done.
Checkstyle ends with 2 errors.



Describe what you expect in detail.

UnnecessaryParentheses should flag unnecessary parentheses in conditional expressions. We should add Question to the acceptable tokens or something.


Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions