Bug 310074

Summary: Warning for unnecessary boolean operations
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3    
Version: 3.6   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Markus Keller CLA 2010-04-22 05:24:54 EDT
N20100421-2000

Would be nice to have a warning for unnecessary boolean operations that should be simplified.

Example (there are probably more cases):

public class Try {
    boolean foo(boolean b) {
        b&= true; // b unchanged
        b&= false; // b= false
        b|= false; // b unchanged
        b= b & true; // b unchanged
        if (b) { // 'if(b)' followed by 'return true' and 'return false'
                 // should be written as: 'return b' (with a '!' if reversed)
            return true;
        }
        return false;
    }
    
    boolean bar() {
        return Math.random() < .5 ? false : true;
        // use 'return !(Math.random() < .5)' 
    }
}


This would e.g. help to spot wrong implementations like ApiFilterStore#removeFilters(IApiProblemFilter[]), which uses
"success &= true;" and "success &= false;" in a for-loop (that case does not cause problems in the current code base, because the return value is only used in an invocation that passes a single filter).