Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogicConditionNeedOptimizationCheck: false positive on conditions with assignments #682

Open
rnveach opened this issue Jun 8, 2018 · 0 comments
Labels

Comments

@rnveach
Copy link
Contributor

rnveach commented Jun 8, 2018

Identified at #678 (comment) ,

$ cat TestClass.java
public class TestClass {
    private int field = 1;

    void method() {
TestClass s = new TestClass(), t = null, u = null;

        // original
        if (((t = s).test()) &&
            ((u = t).field == 1) != null &&
            (u.field) != 2) {}
        // bad fix
        if (((u = t).field == 1) != null &&
            (u.field) != 2 &&
            ((t = s).test())) {}
    }

    public boolean test() { return true; }
}

$ 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="com.github.sevntu.checkstyle.checks.coding.LogicConditionNeedOptimizationCheck" />
    </module>
</module>

$ java -jar checkstyle-8.9-sevntu-1.29.0-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[ERROR] TestClass.java:8: Condition with && at line 8 position 29 need optimization. All method calls are advised to move to end of logic expression. [LogicConditionNeedOptimization]
[ERROR] TestClass.java:9: Condition with && at line 9 position 41 need optimization. All method calls are advised to move to end of logic expression. [LogicConditionNeedOptimization]
Audit done.
Checkstyle ends with 2 errors.

Re-arranging any of the conditions will cause an NPE. In other circumstances it could cause unwanted behavior from using old values from previous iteration, like the instanceof example.

@romani romani added the approved label Jun 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants