#539 Add a check for counfusion between assignment and comparison

release_4.4
closed
nobody
Check (274)
5
2015-12-08
2010-05-20
No

For example :
if (a = 1) : ko
if (a == 1) : ok

the rule must check you never assign something in a control statement condition.

For example :
b = 2;
a = 1 + b; : ok

b == 2 ;
a = 1 + b; : ko

the rule must check you never compare two references outside a control statement condition.

Discussion

  • Roman Ivanov

    Roman Ivanov - 2015-11-02

    could be implemented by http://checkstyle.sourceforge.net/config_misc.html#DescendantToken

    /var/tmp $ java -jar checkstyle-6.12-SNAPSHOT-all.jar -c 3.xml   3.java
    Starting audit...
    /var/tmp/3.java:5:9: error: Count of 1 for 'LITERAL_IF' descendant 'ASSIGN' exceeds maximum count 0.
    Audit done.
    Checkstyle ends with 1 errors.
    
    /var/tmp $ cat 3.java 
    package test;
    
    class Test {
        Inpu boo() {
            if (a = 1) {}
        }
    }
    
    /var/tmp $ cat 3.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">
      <module name="TreeWalker">
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_IF"/>
            <property name="limitedTokens" value="ASSIGN"/>
            <property name="maximumNumber" value="0"/>
        </module>
      </module>
    </module>
    
     
  • Roman Ivanov

    Roman Ivanov - 2015-11-02
    • status: open --> closed
     

Log in to post a comment.