Bug 332713 - [compiler][null]Bogus "Null comparison always yields false"
Summary: [compiler][null]Bogus "Null comparison always yields false"
Status: VERIFIED DUPLICATE of bug 313870
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.7 M6   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-16 05:14 EST by till toenges CLA
Modified: 2011-05-16 00:58 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.