Menu

#1361 False positive UC_USELESS_CONDITION if negated value used as parameter

3.0.1
closed-fixed
5
2015-02-20
2015-02-17
No

The code below produces false positive, but it is valid.

Rank: Of Concern (16), confidence: Normal
Pattern: UC_USELESS_CONDITION
Type: UC, Category: STYLE (Dodgy code)

        if (!fill) {
            rectangle.setBorder(new LineBorder(color, LINE_WIDTH));
        } else {
            rectangle.setOutline(!fill); // UC_USELESS_CONDITION!!
        }

Same problem here: warning at !includeInstructions line

        if (includeInstructions) {
            // insert complete line with previously copied instruction, comment and
            // vector data
            cvcCount = adapter.insertLine(signals, fromCvc,
                    fromTvn, cvcSize, tvnSize, !includeInstructions, true, forUndo,
                    copyBuffer, repetitions);
        }

Discussion

  • Tagir Valeev

    Tagir Valeev - 2015-02-17

    Well, it's useless boolean operation which looks like condition in the bytecode (fill ? false : true). Such condition is useless, because it's known that fill is true in else branch, thus !fill is definitely false. So rectangle.setOutline(false) looks more clear.

    The same in second case: !includeInstructions is definitely false, so it would probably be more clear to write simply false there.

    The only problem I see here is bug confidence. It was intended to assign low confidence to such cases, so users can filter it out. I will check why it doesn't work.

    Probably I should create separate bug pattern for such boolean negation operations which will clearly explain what's happening and users can filter them out if they don't like them. Sometimes real bugs are lurking in such expressions though.

     
  • Andrey Loskutov

    Andrey Loskutov - 2015-02-17

    The problem is that the code is valid and sometimes I prefer to see variable name and not "false" in a method with few parameters, because then I know what happens here without looking at the javadoc.

    Code "clarity" or "style" is not a bug with rank 16 and normal confidence. If this should be a warning, then please low confidence, rank 20.

     
  • Tagir Valeev

    Tagir Valeev - 2015-02-17

    I will think again. Probably I'll remove the warning at all in such cases. You're right, it's quite noisy message. You may commit a testcase with @DesireNoWarning if you have time right now :-)

     
  • Tagir Valeev

    Tagir Valeev - 2015-02-17
    • status: open --> closed-fixed
     
  • Andrey Loskutov

    Andrey Loskutov - 2015-02-20

    Thanks, it is OK now.

     

Log in to post a comment.