False Negative: SimplifyBooleanExpression does not report with paranthesized boolean literals in ternary operators · Issue #19716 · checkstyle/checkstyle · GitHub
Skip to content

False Negative: SimplifyBooleanExpression does not report with paranthesized boolean literals in ternary operators #19716

@lithops-zty

Description

@lithops-zty

I have read check documentation: https://checkstyle.org/checks/coding/simplifybooleanexpression.html
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

/var/tmp $ javac Test.java 
/var/tmp $ 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">
    <module name="TreeWalker">
        <module name="SimplifyBooleanExpression"/>
    </module>
</module>
/var/tmp $ cat Test.java 
public class Test {
    public void test(boolean x) {
        if (x && true) {}
        if (x && (true)) {}
        if (x ? true : false) {}
        if (x ? (true) : false) {}
    }
}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"                                         
/var/tmp $ java $RUN_LOCALE -jar checkstyle-13.4.0-all.jar -c config.xml Test.java 
Starting audit...
[ERROR] /mnt/d/IntellijProjects/checkstyle-playground/inputs/simplify-boolean-expression/Test.java:3:15: Expression can be simplified. [SimplifyBooleanExpression]
[ERROR] /mnt/d/IntellijProjects/checkstyle-playground/inputs/simplify-boolean-expression/Test.java:4:15: Expression can be simplified. [SimplifyBooleanExpression]
[ERROR] /mnt/d/IntellijProjects/checkstyle-playground/inputs/simplify-boolean-expression/Test.java:5:15: Expression can be simplified. [SimplifyBooleanExpression]
Audit done.
Checkstyle ends with 3 errors.

Describe what you expect in detail.

All four if statements should produce violations, instead of only the first three.

Since paranthesizing true does not suppress violation in boolean expressions, it should also not in ternary operators by the same logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions