Bug 332713

Summary: [compiler][null]Bogus "Null comparison always yields false"
Product: [Eclipse Project] JDT Reporter: till toenges <tt>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: VERIFIED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, jarthana, paul
Version: 3.7   
Target Milestone: 3.7 M6   
Hardware: All   
OS: All   
Whiteboard:

Description till toenges CLA 2010-12-16 05:14:24 EST
Build Identifier: 20100917-0705

I get a "Null comparison always yields false: The variable s cannot be null at this location" in the following code. That should not be the case here. The difference between the good and the bad test case is only one if.

public class NullTest {

	public static void main(String[] args) {
		good(args);
		bad(args);
	}
	public static void good(String[] args) {
		String s = "";
		for (int i = 0; i < 13; i++) {
			s = test(args, i);
			if (s == null) {
				System.out.println("null");
			}
		}
	}
	public static void bad(String[] args) {
		String s = "";
		for (int i = 0; i < 13; i++) {
			if (i != 0) {
				s = test(args, i);
			}
			if (s == null) {
				System.out.println("null");
			}
		}
	}
	public static String test(String[] args, int i) {
		if (args.length > i) {
			return args[i];
		}
		return null;
	}
}


Reproducible: Always

Steps to Reproduce:
1. Put the code into Eclipse.
2. Have all the code warnings on.
3. See bogus warning about unneccessary null check.
Comment 1 Ayushman Jain CLA 2010-12-16 06:42:22 EST
I'll take a look.
Comment 2 Paul Wagland CLA 2011-02-08 21:30:21 EST
I have essentially the same problem, with essentially the same reproduction test case. If the initial line = "" is changed to line= null then the warning goes away. It does not change based on whether the initial doRead is set to true or false.


package net.wagland.paul.test;

import java.io.BufferedReader;
import java.io.IOException;

public class NullWarningTest {
  public void testNullWarning(BufferedReader bufReader) throws IOException {
    String line = "";
    boolean doRead = false;
    while (true) {
      if (doRead)
        line = bufReader.readLine();
      if (line == null)
        return;
      doRead = true;
    }
  }
}
Comment 3 Paul Wagland CLA 2011-02-08 21:32:58 EST
Actually, this bug should be marked as a duplicate of bug 313870.
Comment 4 Ayushman Jain CLA 2011-02-09 02:16:54 EST
Closing as dup

*** This bug has been marked as a duplicate of bug 313870 ***
Comment 5 Jay Arthanareeswaran CLA 2011-05-16 00:58:11 EDT
Verified for 3.7 RC1 with build I20110514-0800.