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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java (-1 / +4 lines)
Lines 548-554 Link Here
548
		case CAN_ONLY_NULL | IN_ASSIGNMENT:
548
		case CAN_ONLY_NULL | IN_ASSIGNMENT:
549
		case CAN_ONLY_NULL | IN_INSTANCEOF:
549
		case CAN_ONLY_NULL | IN_INSTANCEOF:
550
			if (flowInfo.isPotentiallyNonNull(local)
550
			if (flowInfo.isPotentiallyNonNull(local)
551
					|| flowInfo.isPotentiallyUnknown(local)) {
551
					|| flowInfo.isPotentiallyUnknown(local)
552
					|| flowInfo.isProtectedNonNull(local)) {
553
				// if variable is not null, we are not interested in recording null reference for deferred checks.
554
				// This is because CAN_ONLY_NULL means we're only interested in cases when variable can be null.
552
				return;
555
				return;
553
			}
556
			}
554
			if (flowInfo.isDefinitelyNull(local)) {
557
			if (flowInfo.isDefinitelyNull(local)) {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (+48 lines)
Lines 14530-14533 Link Here
14530
		"Dead code\n" + 
14530
		"Dead code\n" + 
14531
		"----------\n");
14531
		"----------\n");
14532
}
14532
}
14533
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=342300
14534
public void testBug342300() throws Exception {
14535
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14536
		this.runConformTest(
14537
			new String[] {
14538
				"X.java",
14539
				"public class X {\n" + 
14540
				"	public static void initPattern(String p, Character escapeChar) {\n" + 
14541
				"		int len = p.length();\n" +
14542
				"		for (int i = 0; i < len; i++) {\n" +
14543
				"			char c = p.charAt(i);\n" +
14544
				"			if (escapeChar != null && escapeChar == c) {\n" +	// quiet
14545
				"				c = p.charAt(++i);\n" +
14546
				"			}\n" + 
14547
				"	    }\n" + 
14548
				"	}\n" + 
14549
				"}",
14550
			},
14551
			"");
14552
	}
14553
}
14554
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=342300
14555
// To make sure only the redundant null check is given and not a potential NPE
14556
public void testBug342300b() throws Exception {
14557
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14558
		this.runNegativeTest(
14559
			new String[] {
14560
				"X.java",
14561
				"public class X {\n" + 
14562
				"	public static void initPattern(String p, Character escapeChar) {\n" + 
14563
				"		int len = p.length();\n" +
14564
				"		for (int i = 0; i < len; i++) {\n" +
14565
				"			char c = p.charAt(i);\n" +
14566
				"			if (escapeChar != null && escapeChar != null) {\n" +	// look here
14567
				"				c = p.charAt(++i);\n" +
14568
				"			}\n" + 
14569
				"	    }\n" + 
14570
				"	}\n" + 
14571
				"}",
14572
			},
14573
			"----------\n" + 
14574
			"1. ERROR in X.java (at line 6)\n" + 
14575
			"	if (escapeChar != null && escapeChar != null) {\n" + 
14576
			"	                          ^^^^^^^^^^\n" + 
14577
			"Redundant null check: The variable escapeChar cannot be null at this location\n" + 
14578
			"----------\n");
14579
	}
14580
}
14533
}
14581
}

Return to bug 342300