Eclipse marks lines as dead code
Asked Answered
D

1

9

I have this function with some dead code, marked by Eclipse.

I have two lines that check a & b. Lines that check b are marked as null.

    public int[] runThis(List<Integer> buildIds, List<Integer> scenarios, boolean oflag) {

    int rating[] = new int[scenarios.size()];

    if(buildIds == null) {
        System.out.println("ERROR - Building ID list is null!");
        return null;
    }

    if(scenarios == null) {
        System.out.println("ERROR - Scenario list is null!"); //dead
        return null; //dead
    }

    return rating;      

}

Why does Ellipse make the two lines as dead? Any help? Thanks very much for your time.

Dissert answered 7/11, 2011 at 16:55 Comment(5)
Is there any other code in that method? Maybe you've been dereferencing b before, which tells Eclipse that b can't be null at this point. In that case you should also get a warning like "unnecessary null check" at the if (b == null) line.Ariose
I don't know about Eclipse much but IntelliJ IDEA can analyze your code in real-time and warn you that some code is impossible to reach. In your case, if IntelliJ were to detect that b was never null, then it would warn you that these two lines can never be reached because the condition b == null* is always false.Lubbi
@Joachim: that particular warning is configureable in Eclipse's settings. I believe it's by default turned off.Aspia
can you paste the whole method?Ease
@BalusC: Oh, I didn't remember that. I probably turned it on as soon as I heard about it ;-) pretty useful these ones.Ariose
A
18

Because you've already called scenarios.size() in your array constructor. This guarantees scenarios isn't null or it will have thrown an exception by that point.

Amphibology answered 7/11, 2011 at 17:2 Comment(1)
Thank you so much for that explanation. I hadn't realized that by evaluating something earlier it's guaranteed NOT to get there because of prior NPE, so hence: "Dead code" warning.Ulcerate

© 2022 - 2024 — McMap. All rights reserved.