View | Details | Raw Unified | Return to bug 339250 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/CompoundAssignment.java (-2 / +13 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 42-48 Link Here
42
	if (this.resolvedType.id != T_JavaLangString) {
42
	if (this.resolvedType.id != T_JavaLangString) {
43
		this.lhs.checkNPE(currentScope, flowContext, flowInfo);
43
		this.lhs.checkNPE(currentScope, flowContext, flowInfo);
44
	}
44
	}
45
	return  ((Reference) this.lhs).analyseAssignment(currentScope, flowContext, flowInfo, this, true).unconditionalInits();
45
	flowInfo = ((Reference) this.lhs).analyseAssignment(currentScope, flowContext, flowInfo, this, true).unconditionalInits();
46
	if (this.resolvedType.id == T_JavaLangString) {
47
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=339250
48
		LocalVariableBinding local = this.lhs.localVariableBinding();
49
		if (local != null && this.resolvedType.id == T_JavaLangString) {
50
			// compound assignment results in a definitely non null value for String
51
			flowInfo.markAsDefinitelyNonNull(local);
52
			if (flowContext.initsOnFinally != null)
53
				flowContext.initsOnFinally.markAsDefinitelyNonNull(local);
54
		}
55
	}
56
	return flowInfo;
46
}
57
}
47
58
48
	public boolean checkCastCompatibility() {
59
	public boolean checkCastCompatibility() {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (-1 / +90 lines)
Lines 44-50 Link Here
44
// Only the highest compliance level is run; add the VM argument
44
// Only the highest compliance level is run; add the VM argument
45
// -Dcompliance=1.4 (for example) to lower it if needed
45
// -Dcompliance=1.4 (for example) to lower it if needed
46
static {
46
static {
47
//		TESTS_NAMES = new String[] { "testBug336428e" };
47
//		TESTS_NAMES = new String[] { "testBug339250" };
48
//		TESTS_NUMBERS = new int[] { 561 };
48
//		TESTS_NUMBERS = new int[] { 561 };
49
//		TESTS_RANGE = new int[] { 1, 2049 };
49
//		TESTS_RANGE = new int[] { 1, 2049 };
50
}
50
}
Lines 14441-14444 Link Here
14441
		"    17  return\n";
14441
		"    17  return\n";
14442
	checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
14442
	checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
14443
}
14443
}
14444
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=339250
14445
// Check code gen
14446
public void testBug339250() throws Exception {
14447
	Map options = getCompilerOptions();
14448
	options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
14449
	this.runConformTest(
14450
		new String[] {
14451
			"X.java",
14452
			"public class X {\n" + 
14453
			"	public static void main(String[] args) {\n" + 
14454
			"		String s = null;\n" +
14455
			"		s += \"correctly\";\n" +
14456
			"		if (s != null) {\n" + 	// s cannot be null
14457
			"			System.out.println(\"It works \" + s);\n" + 
14458
			"		}\n" +
14459
			"	}\n" + 
14460
			"}",
14461
		},
14462
		"It works nullcorrectly",
14463
		null,
14464
		true,
14465
		null,
14466
		options,
14467
		null);
14468
}
14469
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=339250
14470
// Check that the redundant null check warning is correctly produced
14471
public void testBug339250a() throws Exception {
14472
	this.runNegativeTest(
14473
		new String[] {
14474
			"X.java",
14475
			"public class X {\n" + 
14476
			"	public static void main(String[] args) {\n" + 
14477
			"		String s = null;\n" +
14478
			"		s += \"correctly\";\n" +
14479
			"		if (s != null) {\n" + 	// s cannot be null
14480
			"			System.out.println(\"It works \" + s);\n" + 
14481
			"		}\n" +
14482
			"	}\n" + 
14483
			"}",
14484
		},
14485
		"----------\n" + 
14486
		"1. ERROR in X.java (at line 5)\n" + 
14487
		"	if (s != null) {\n" + 
14488
		"	    ^\n" + 
14489
		"Redundant null check: The variable s cannot be null at this location\n" + 
14490
		"----------\n");
14491
}
14492
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=339250
14493
// Check that the redundant null check warning is correctly produced
14494
public void testBug339250b() throws Exception {
14495
	this.runNegativeTest(
14496
		new String[] {
14497
			"X.java",
14498
			"public class X {\n" + 
14499
			"	public static void main(String[] args) {\n" + 
14500
			"		String s = null;\n" +
14501
			"		s += null;\n" +
14502
			"		if (s != null) {\n" + 	// s is definitely not null
14503
			"			System.out.println(\"It works \" + s);\n" + 
14504
			"	    }\n" + 
14505
			"		s = null;\n" +
14506
			"		if (s != null) {\n" + 	// s is definitely null
14507
			"			System.out.println(\"Fails \" + s);\n" + 
14508
			"	    } else {\n" + 
14509
			"			System.out.println(\"Works second time too \" + s);\n" +
14510
			"       }\n" + 
14511
			"	}\n" + 
14512
			"}",
14513
		},
14514
		"----------\n" + 
14515
		"1. ERROR in X.java (at line 5)\n" + 
14516
		"	if (s != null) {\n" + 
14517
		"	    ^\n" + 
14518
		"Redundant null check: The variable s cannot be null at this location\n" + 
14519
		"----------\n" + 
14520
		"2. ERROR in X.java (at line 9)\n" + 
14521
		"	if (s != null) {\n" + 
14522
		"	    ^\n" + 
14523
		"Null comparison always yields false: The variable s can only be null at this location\n" + 
14524
		"----------\n" + 
14525
		"3. WARNING in X.java (at line 9)\n" + 
14526
		"	if (s != null) {\n" + 
14527
		"			System.out.println(\"Fails \" + s);\n" + 
14528
		"	    } else {\n" + 
14529
		"	               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
14530
		"Dead code\n" + 
14531
		"----------\n");
14532
}
14444
}
14533
}

Return to bug 339250