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

LogicConditionNeedOptimization: false positive when many conditions are wrapped in parenthesis #683

Closed
rnveach opened this issue Jun 8, 2018 · 2 comments

Comments

@rnveach
Copy link
Contributor

@rnveach rnveach commented Jun 8, 2018

$ cat TestClass.java
import java.nio.*;

public class NativeOrder {
    public static void main(String[] args) throws Exception {
        ByteOrder bo = ByteOrder.nativeOrder();
        System.err.println(bo);
        String arch = System.getProperty("os.arch");
        if (((arch.equals("sparc") && (bo != ByteOrder.BIG_ENDIAN))) ||
            ((arch.equals("i386") && (bo != ByteOrder.LITTLE_ENDIAN)))) {
            throw new Exception("Wrong byte order");
        }
    }
}

$ 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>
</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 35 need optimization. All method calls are advised to move to end of logic expression. [LogicConditionNeedOptimization]
[ERROR] TestClass.java:8: Condition with || at line 8 position 69 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 34 need optimization. All method calls are advised to move to end of logic expression. [LogicConditionNeedOptimization]
Audit done.
Checkstyle ends with 3 errors.

Violations for 8:35 and 9:34 are valid.
The violation at 8:69 is not. The || between the 2 big blocks both have method calls, so there is no better ordering switching them around, and the violation will stay even if they are switched.
Once the other 2 violations are fixed, the other one disappears.

I couldn't create an example that only involved 1 violation.

@rnveach
Copy link
Contributor Author

@rnveach rnveach commented Jun 8, 2018

The issue is LPAREN and RPAREN aren't nested in each other in this type of situation.

    |      |  |  `--LOR -> || [8:69]
    |      |  |      |--LPAREN -> ( [8:12]
    |      |  |      |--LPAREN -> ( [8:13]
    |      |  |      |--LAND -> && [8:35]
....
    |      |  |      |--RPAREN -> ) [8:66]
    |      |  |      |--RPAREN -> ) [8:67]
    |      |  |      |--LPAREN -> ( [9:12]
    |      |  |      |--LPAREN -> ( [9:13]
    |      |  |      |--LAND -> && [9:34]

So instead of comparing (a && b) to (c && d), are probably comparing (a && b) to d.
So instead of doing a single branchContains, we would have to do it on all inside the first set of parenthesis.

rnveach added a commit to rnveach/sevntu.checkstyle that referenced this issue Jun 8, 2018
rnveach added a commit to rnveach/sevntu.checkstyle that referenced this issue Jun 9, 2018
@romani romani added the approved label Jun 9, 2018
rnveach added a commit that referenced this issue Jun 9, 2018
@rnveach
Copy link
Contributor Author

@rnveach rnveach commented Jun 9, 2018

Fix was merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants