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

False positive in LogicConditionNeedOptimization on instanceof with pattern variable #882

Open
nrmancuso opened this issue Jan 20, 2022 · 0 comments
Labels

Comments

@nrmancuso
Copy link
Contributor

nrmancuso commented Jan 20, 2022

From checkstyle/checkstyle#11100

Discovered in report at https://nmancus1.github.io/issue-10848/issue-10848_check_diff_reports_2021_12_31/diff_sevntu-check-regression_part_2/index.html

➜  javac --enable-preview --release 17 Test.java

➜  cat Test.java
public class Test {
    void m1(Object o) {
        for (int i = 0; o instanceof Integer myInt && myInt > 5;) { // should be ok
        }
    }
}

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

➜  java -classpath sevntu-checks-1.41.0.jar:checkstyle-9.3-SNAPSHOT-all.jar com.puppycrawl.tools.checkstyle.Main -c config.xml Test.java
Starting audit...
[ERROR] Test.java:3:52: Condition with && at line 3 position 51 need optimization. All method calls are advised to move to end of logic expression. [LogicConditionNeedOptimization]
Audit done.
Checkstyle ends with 1 errors.

But, if we follow advice of LogicConditionNeedOptimization check:

➜  cat Test.java                                                                                                                        
public class Test {
    void m1(Object o) {
        for (int i = 0; myInt > 5 && o instanceof Integer myInt;) { // should be ok
        }
    }
}

➜  javac --enable-preview --release 17 Test.java                                                                                        
Test.java:3: error: cannot find symbol
        for (int i = 0; myInt > 5 && o instanceof Integer myInt;) { // should be ok
                        ^
  symbol:   variable myInt
  location: class Test
1 error


So, this is a false positive.

nrmancuso added a commit to nrmancuso/sevntu.checkstyle that referenced this issue Jan 20, 2022
romani pushed a commit to nrmancuso/sevntu.checkstyle that referenced this issue Oct 10, 2022
rnveach pushed a commit to nrmancuso/sevntu.checkstyle that referenced this issue Oct 14, 2022
rnveach pushed a commit to nrmancuso/sevntu.checkstyle that referenced this issue Oct 14, 2022
rnveach pushed a commit to nrmancuso/sevntu.checkstyle that referenced this issue Oct 14, 2022
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