Bug 253896 - [compiler][null] wrong "Null comparison always yields false" problem for auto-unboxing
Summary: [compiler][null] wrong "Null comparison always yields false" problem for auto...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 minor (vote)
Target Milestone: 3.6 M6   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-11-05 06:57 EST by Markus Keller CLA
Modified: 2016-06-21 07:12 EDT (History)
2 users (show)

See Also:
frederic_fusier: review+


Attachments
proposed fix v0.5 + regression tests (7.95 KB, patch)
2010-01-15 01:54 EST, Ayushman Jain CLA
no flags Details | Diff
proposed fix v1.0 + regression tests (7.17 KB, patch)
2010-01-15 12:54 EST, Ayushman Jain CLA
no flags Details | Diff
updated fix + regression tests (26.53 KB, patch)
2010-02-04 14:16 EST, Ayushman Jain CLA
no flags Details | Diff
updated patch for HEAD (26.95 KB, patch)
2010-02-10 06:51 EST, Ayushman Jain CLA
Olivier_Thomann: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2008-11-05 06:57:01 EST
I20081104-0916

public class Unboxing {
	public static void main(String[] args) {
		a();
		b();
	}

	private static void a() {
		Integer i= null;
		if (i == 1) // wrong problem: "Null comparison always yields
// false: The variable i can only be null at this location"
			System.out.println("a");
	}
	
	private static void b() {
		Integer i= null;
		if (i.intValue() == 1) // correct: "Null pointer access: The
// variable i can only be null at this location"
			System.out.println("b");
	}
}

The problem reported in a() should be the same problem as in b(), since both methods are effectively the same and result in an NPE.
Comment 1 Ayushman Jain CLA 2010-01-15 01:54:53 EST
Created attachment 156203 [details]
proposed fix v0.5 + regression tests

Modified FlowContext#recordUsingNullReference, to check for unboxing. If it is taking place, then report the null pointer access warning instead of redundant null check warning.
Comment 2 Frederic Fusier CLA 2010-01-15 04:46:25 EST
The patch does not look good to me. There a specific field on Expression for boxing/unboxing (implicitConversion) which has to be used instead calling lookup methods to know whether an unboxing has been done or not.

Hence the condition can be reduced to: 
if ((checkType & CHECK_MASK) == CAN_ONLY_NULL &&
   (reference.implicitConversion & TypeIds.UNBOXING) != 0) {
	scope.problemReporter().localVariableNullReference(local, reference);
	return;
}

Which will be less time consuming as there won't be any extra methods calls...
Comment 3 Ayushman Jain CLA 2010-01-15 12:54:19 EST
Created attachment 156255 [details]
proposed fix v1.0 + regression tests

fixed the check for autounboxing as per comment 2.
Comment 4 Frederic Fusier CLA 2010-02-03 10:55:21 EST
Patch looks good to me
Comment 5 Ayushman Jain CLA 2010-02-04 02:22:52 EST
Oops! I just noticed that for two cases the old message is still shown- for loops and finally blocks. I'll append the fix for these 2 cases also, and submit a fresh patch.
Comment 6 Ayushman Jain CLA 2010-02-04 14:16:01 EST
Created attachment 158215 [details]
updated fix + regression tests

The fix is the same, but its now applied to the places that I earlier missed - precisely looping context and finally context.

Also added two new regression tests for these cases, and updated the previously supplied tests with the new dead code warnings.
Comment 7 Ayushman Jain CLA 2010-02-04 14:17:52 EST
Frederic can you please re-review?
Thanks!
Comment 8 Frederic Fusier CLA 2010-02-05 06:22:46 EST
(In reply to comment #7)
> Frederic can you please re-review?
> Thanks!

Can you update the patch as it's no longer applicable since fix for bug 250056 has been released, thanks
Comment 9 Ayushman Jain CLA 2010-02-10 06:51:54 EST
Created attachment 158695 [details]
updated patch for HEAD

patch updated for HEAD.
Comment 10 Frederic Fusier CLA 2010-02-23 08:01:37 EST
(In reply to comment #9)
> Created an attachment (id=158695) [details]
> updated patch for HEAD
> 

The patch looks good to me.
Comment 11 Olivier Thomann CLA 2010-02-23 13:26:58 EST
Released for 3.6M6.
Regression tests added in:
org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#testBug253896a
org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#testBug253896b
org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#testBug253896c
org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#testBug253896d
Comment 12 Olivier Thomann CLA 2010-03-08 13:41:31 EST
Verified for 3.6M6 using I20100307-2000.
Comment 13 Marvin Fröhlich CLA 2016-06-21 07:12:31 EDT
Here is another case of the same problem.

Map< String, Integer > map = new HashMap<>();

#############################
Integer test = ( map == null ) ? 0 : map.get( "foo" );
if ( test == null ) // Null comparison always yields false...
    test = 0;
#############################

If I write it using regular if selection instead of inline-if, it works as expected. And it also works, if I use String and "" instead of Integer and 0.

This is for Eclipse 4.5.2. So, I would suggest to reopen this bug.