#228 Operations with LHS and RHS equal

open
nobody
Check (274)
5
2012-10-10
2003-06-15
Tim Tyler
No

Certain types of operations where the LHS and RHS
are equal frequently signal programming errors.

Examples:

a = a; // Eclipse alreadly finds this one
a - a;
a / a;
a ^ a;
a & a;
a | a;
(a && a);
(a || a);
(a == a);
(a != a);
(a > a);

Some care needs to be taken with precedence for
some of these expressions - that nothing binds
to either argument tightly.

Except for the first case, the expression can
be practically anything - provided there are
no side effects - so method calls, ++, and
assignments would have to be prohibited.

Discussion

  • Tim Tyler
    Tim Tyler
    2003-06-15

    Logged In: YES
    user_id=796025

    I've implemented this:

        <module name="IdenticalArguments"/>
    

    Currently parsed tokens:

    ASSIGN,
    BAND,
    BAND_ASSIGN,
    BOR,
    BOR_ASSIGN,
    BXOR,
    BXOR_ASSIGN,
    DIV,
    DIV_ASSIGN,
    EQUAL,
    GE,
    GT,
    LAND,
    LE,
    LT,
    LOR,
    MINUS,
    MOD,
    MOD_ASSIGN,
    NOT_EQUAL

    Currently prohibited tokens:

    ASSIGN,
    DEC,
    INC,
    POST_DEC,
    POST_INC,
    METHOD_CALL

    From the associated test:

    // things it finds...
    a = a;
    a = 1 - 1 + a;
    this.a = this.a;
    super.a = super.a;
    Foo.bar = Foo.bar;
    a = a - a;
    a = a / a;
    a = a ^ a;
    a = a & a;
    a = a | a;
    a &= a;
    a |= a;
    a = (a && a);
    a = (a || a);
    a = (a == a);
    a = (a != a);
    a = (a > a);

    // things it (conservatively) fails to find...
    a = foo(a) == foo(a);
    a = a++ == a++;

    // things it currently fails to find - but should ideally report...
    a = a ? b : b;
    a = a + 1 - 1;

    // things it correctly fails to report about...
    a = 1 - 1 * 3;

     
  • Logged In: YES
    user_id=746148

    I've just wonder where i can find IdenticalArguments module ;)

     
  • Paul Wagland
    Paul Wagland
    2004-09-22

    Logged In: YES
    user_id=19812

    Hmm. Interesting comment there... the code for this would be
    quite interesting... Is there any chance of attaching it?

     
  • Tim Tyler
    Tim Tyler
    2004-09-25

    Logged In: YES
    user_id=796025

    My code was all posted in July 2003 to:

    http://timtyler.org/checkstyle/

    ...where is is still accessible.

    The code there has all been placed into the public domain.

    The status of the code is that it consists of a set of
    Checkstyle checks - complete with unit tests. However,
    the code never made it through the "Checkstyle" set of
    checks - since I never managed to get this to work from
    within Eclipse, due to this problem:

    http://sourceforge.net/tracker/index.php?
    func=detail&aid=771490&group_id=80344&atid=559494