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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java (+3 lines)
Lines 405-410 Link Here
405
				} else {
405
				} else {
406
					scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
406
					scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
407
				}
407
				}
408
			} else if (this.upstreamNullFlowInfo.isDefinitelyNonNull(local) && !flowInfo.isPotentiallyNull(local)) {    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418
409
				flowInfo.markAsDefinitelyNonNull(local);
410
				recordNullReference(local, reference, checkType);
408
			} else if (! flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
411
			} else if (! flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
409
				if (flowInfo.isPotentiallyNonNull(local)) {
412
				if (flowInfo.isPotentiallyNonNull(local)) {
410
					recordNullReference(local, reference, CAN_ONLY_NON_NULL | checkType & CONTEXT_MASK);
413
					recordNullReference(local, reference, CAN_ONLY_NON_NULL | checkType & CONTEXT_MASK);
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (-3 / +125 lines)
Lines 4313-4320 Link Here
4313
		"");
4313
		"");
4314
}
4314
}
4315
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=220788
4315
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=220788
4316
public void _test0470_while() {
4316
public void test0470_while() {
4317
	this.runConformTest(
4317
	this.runNegativeTest(
4318
		new String[] {
4318
		new String[] {
4319
			"X.java",
4319
			"X.java",
4320
			"public class X {\n" +
4320
			"public class X {\n" +
Lines 4332-4338 Link Here
4332
			"  }\n" +
4332
			"  }\n" +
4333
			"}"
4333
			"}"
4334
		},
4334
		},
4335
		"ERROR: o cannot be null on first if");
4335
		"----------\n" + 
4336
		"1. ERROR in X.java (at line 5)\n" + 
4337
		"	if (o != null && o.toString().equals(\"o\")) {\n" + 
4338
		"	    ^\n" + 
4339
		"Redundant null check: The variable o cannot be null at this location\n" + 
4340
		"----------\n");
4336
}
4341
}
4337
// null analysis -- try/finally
4342
// null analysis -- try/finally
4338
public void test0500_try_finally() {
4343
public void test0500_try_finally() {
Lines 10130-10133 Link Here
10130
		"Potential null pointer access: The variable o1 may be null at this location\n" +
10135
		"Potential null pointer access: The variable o1 may be null at this location\n" +
10131
		"----------\n");
10136
		"----------\n");
10132
}
10137
}
10138
10139
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418
10140
// Test to verify that redundant null checks are properly reported in all loops
10141
public void testBug291418a() {
10142
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
10143
		this.runNegativeTest(
10144
				new String[] {
10145
						"X.java",
10146
						"class X {\n" +
10147
						"  void foo(int[] argArray) {\n" +
10148
						"    int[] array = {2};\n" +
10149
						"    int[] collectionVar = {1,2};\n" +
10150
						"	 if(argArray == null) return;" +
10151
						"    for(int x:collectionVar) {\n" +
10152
						"        if (collectionVar == null);\n" +	// collectionVar cannot be null here
10153
						"        if (array == null);\n" +				// array is not null here
10154
						"		 if (argArray == null);\n" +		// argArray cannot be null here
10155
						"    }\n" +
10156
						"	 int count = 0;\n" +
10157
						"    do {\n" +
10158
						"		 count++;\n" +
10159
						"        if (array == null);\n" +				// array is not null here
10160
						"		 if (argArray == null);\n" +		// argArray cannot be null here
10161
						"    } while (count<10);\n" +
10162
						"    for (int i=0; i<2; i++) {\n" +
10163
						"        if (array == null);\n" +				// array is not null here
10164
						"		 if (argArray == null);\n" +		// argArray cannot be null here
10165
						"    }\n" +
10166
						"    while (true) {\n" +
10167
						"        if (array == null);\n" +				// array is not null here
10168
						"		 if (argArray == null);\n" +		// argArray cannot be null here
10169
						"    }\n" +
10170
						"  }\n" +
10171
						"}"},
10172
				"----------\n" +
10173
				"1. ERROR in X.java (at line 6)\n" +
10174
				"	if (collectionVar == null);\n" +
10175
				"	    ^^^^^^^^^^^^^\n" +
10176
				"Null comparison always yields false: The variable collectionVar cannot be null at this location\n" +
10177
				"----------\n" +
10178
				"2. ERROR in X.java (at line 7)\n" +
10179
				"	if (array == null);\n" +
10180
				"	    ^^^^^\n" +
10181
				"Null comparison always yields false: The variable array cannot be null at this location\n" +
10182
				"----------\n" +
10183
				"3. ERROR in X.java (at line 8)\n" +
10184
				"	if (argArray == null);\n" +
10185
				"	    ^^^^^^^^\n" +
10186
				"Null comparison always yields false: The variable argArray cannot be null at this location\n" +
10187
				"----------\n" +
10188
				"4. ERROR in X.java (at line 13)\n" +
10189
				"	if (array == null);\n" +
10190
				"	    ^^^^^\n" +
10191
				"Null comparison always yields false: The variable array cannot be null at this location\n" +
10192
				"----------\n" +
10193
				"5. ERROR in X.java (at line 14)\n" +
10194
				"	if (argArray == null);\n" +
10195
				"	    ^^^^^^^^\n" +
10196
				"Null comparison always yields false: The variable argArray cannot be null at this location\n" +
10197
				"----------\n" +
10198
				"6. ERROR in X.java (at line 17)\n" +
10199
				"	if (array == null);\n" +
10200
				"	    ^^^^^\n" +
10201
				"Null comparison always yields false: The variable array cannot be null at this location\n" +
10202
				"----------\n" +
10203
				"7. ERROR in X.java (at line 18)\n" +
10204
				"	if (argArray == null);\n" +
10205
				"	    ^^^^^^^^\n" +
10206
				"Null comparison always yields false: The variable argArray cannot be null at this location\n" +
10207
				"----------\n" +
10208
				"8. ERROR in X.java (at line 21)\n" +
10209
				"	if (array == null);\n" +
10210
				"	    ^^^^^\n" +
10211
				"Null comparison always yields false: The variable array cannot be null at this location\n" +
10212
				"----------\n" +
10213
				"9. ERROR in X.java (at line 22)\n" +
10214
				"	if (argArray == null);\n" +
10215
				"	    ^^^^^^^^\n" +
10216
				"Null comparison always yields false: The variable argArray cannot be null at this location\n" +
10217
				"----------\n");
10218
	}
10219
}
10220
10221
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418
10222
// Test to verify that redundant null checks are properly reported
10223
// in a loop in case the null status is modified downstream in the loop
10224
public void testBug291418b() {
10225
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
10226
		this.runNegativeTest(
10227
				new String[] {
10228
						"X.java",
10229
						"class X {\n" +
10230
						"  void foo(int[] argArray) {\n" +
10231
						"    int[] array = {2};\n" +
10232
						"    int[] collectionVar = {1,2};\n" +
10233
						"	 if(argArray == null) return;" +
10234
						"    for(int x:collectionVar) {\n" +
10235
						"        if (collectionVar == null);\n" +	// collectionVar cannot be null here
10236
						"        if (array == null);\n" +		// array is not null in first iteration but assigned null later in the loop. So we keep quiet
10237
						"		 if (argArray == null);\n" +		// argArray cannot be null here
10238
						"		 array = null;\n" +
10239
						"    }\n" +
10240
						"  }\n" +
10241
						"}"},
10242
				"----------\n" + 
10243
				"1. ERROR in X.java (at line 6)\n" + 
10244
				"	if (collectionVar == null);\n" + 
10245
				"	    ^^^^^^^^^^^^^\n" + 
10246
				"Null comparison always yields false: The variable collectionVar cannot be null at this location\n" + 
10247
				"----------\n" + 
10248
				"2. ERROR in X.java (at line 8)\n" + 
10249
				"	if (argArray == null);\n" + 
10250
				"	    ^^^^^^^^\n" + 
10251
				"Null comparison always yields false: The variable argArray cannot be null at this location\n" + 
10252
				"----------\n");
10253
	}
10254
}
10133
}
10255
}

Return to bug 291418