Menu

#1209 BC_UNCONFIRMED_CAST false positive in the sequence of "if" statements.

2.0.3
closed-wont-fix
None
5
2014-08-13
2013-10-31
No

Probably it's not very important problem as BC_UNCONFIRMED_CAST is switched off by default, but at least it worth reporting. Feel free to reject if fixing such case is too difficult.

public class TestCast {
    public double getValue(Object obj) {
        if(obj instanceof Number || obj instanceof String) {
            if(obj instanceof Number) {
                return ((Number)obj).doubleValue();
            } else {
                return Double.parseDouble((String)obj);
            }
        } else {
            throw new IllegalArgumentException();
        }
    }
}

FindBugs 2.0.2 reports at TestCast.java, line 8:

Bug: Unchecked/unconfirmed cast from java.io.Serializable 
to String in TestCast.getValue(Object)

This cast is unchecked, and not all instances of the type casted 
from can be cast to the type it is being cast to. Check that 
your program logic ensures that this cast will not fail. 

Confidence: Low, Rank: Of Concern (20)
Pattern: BC_UNCONFIRMED_CAST 
Type: BC, Category: STYLE (Dodgy code)

Program logic clearly displays that cast to String is completely safe.

Discussion

  • William Pugh

    William Pugh - 2013-10-31

    Really hard to fix, and the code is pretty bad and should be rewritten anyway.

     
  • William Pugh

    William Pugh - 2013-10-31
    • status: open --> closed-wont-fix
    • assigned_to: William Pugh
     

Log in to post a comment.