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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/AssertStatement.java (-1 / +3 lines)
Lines 44-51 Link Here
44
	Constant cst = this.assertExpression.optimizedBooleanConstant();
44
	Constant cst = this.assertExpression.optimizedBooleanConstant();
45
	boolean isOptimizedTrueAssertion = cst != Constant.NotAConstant && cst.booleanValue() == true;
45
	boolean isOptimizedTrueAssertion = cst != Constant.NotAConstant && cst.booleanValue() == true;
46
	boolean isOptimizedFalseAssertion = cst != Constant.NotAConstant && cst.booleanValue() == false;
46
	boolean isOptimizedFalseAssertion = cst != Constant.NotAConstant && cst.booleanValue() == false;
47
47
	
48
	flowContext.hideNullComparisonWarnings = true;
48
	FlowInfo conditionFlowInfo = this.assertExpression.analyseCode(currentScope, flowContext, flowInfo.copy());
49
	FlowInfo conditionFlowInfo = this.assertExpression.analyseCode(currentScope, flowContext, flowInfo.copy());
50
	flowContext.hideNullComparisonWarnings = false;
49
	UnconditionalFlowInfo assertWhenTrueInfo = conditionFlowInfo.initsWhenTrue().unconditionalInits();
51
	UnconditionalFlowInfo assertWhenTrueInfo = conditionFlowInfo.initsWhenTrue().unconditionalInits();
50
	FlowInfo assertInfo = conditionFlowInfo.initsWhenFalse();
52
	FlowInfo assertInfo = conditionFlowInfo.initsWhenFalse();
51
	if (isOptimizedTrueAssertion) {
53
	if (isOptimizedTrueAssertion) {
(-)compiler/org/eclipse/jdt/internal/compiler/flow/FinallyFlowContext.java (-13 / +43 lines)
Lines 96-104 Link Here
96
				case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
96
				case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
97
					if (flowInfo.isDefinitelyNonNull(local)) {
97
					if (flowInfo.isDefinitelyNonNull(local)) {
98
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
98
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
99
							scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
99
							if (!this.hideNullComparisonWarnings) {
100
								scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
101
							}
100
						} else {
102
						} else {
101
							scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
103
							if (!this.hideNullComparisonWarnings) {
104
								scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
105
							}
102
						}
106
						}
103
						continue;
107
						continue;
104
					}
108
					}
Lines 110-119 Link Here
110
					if (flowInfo.isDefinitelyNull(local)) {
114
					if (flowInfo.isDefinitelyNull(local)) {
111
						switch(this.nullCheckTypes[i] & CONTEXT_MASK) {
115
						switch(this.nullCheckTypes[i] & CONTEXT_MASK) {
112
							case FlowContext.IN_COMPARISON_NULL:
116
							case FlowContext.IN_COMPARISON_NULL:
113
								scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
117
								if (!this.hideNullComparisonWarnings) {
118
									scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
119
								}
114
								continue;
120
								continue;
115
							case FlowContext.IN_COMPARISON_NON_NULL:
121
							case FlowContext.IN_COMPARISON_NON_NULL:
116
								scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
122
								if (!this.hideNullComparisonWarnings) {
123
									scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
124
								}
117
								continue;
125
								continue;
118
							case FlowContext.IN_ASSIGNMENT:
126
							case FlowContext.IN_ASSIGNMENT:
119
								scope.problemReporter().localVariableRedundantNullAssignment(local, expression);
127
								scope.problemReporter().localVariableRedundantNullAssignment(local, expression);
Lines 191-200 Link Here
191
					case CAN_ONLY_NULL | IN_INSTANCEOF:
199
					case CAN_ONLY_NULL | IN_INSTANCEOF:
192
						if (flowInfo.cannotBeNull(local)) {
200
						if (flowInfo.cannotBeNull(local)) {
193
							if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
201
							if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
194
								scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
202
								if (!this.hideNullComparisonWarnings) {
203
									scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
204
								}
195
								flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
205
								flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
196
							} else if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NULL)) {
206
							} else if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NULL)) {
197
								scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
207
								if (!this.hideNullComparisonWarnings) {
208
									scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
209
								}
198
								flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
210
								flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
199
							}
211
							}
200
							return;
212
							return;
Lines 202-212 Link Here
202
						if (flowInfo.canOnlyBeNull(local)) {
214
						if (flowInfo.canOnlyBeNull(local)) {
203
							switch(checkType & CONTEXT_MASK) {
215
							switch(checkType & CONTEXT_MASK) {
204
								case FlowContext.IN_COMPARISON_NULL:
216
								case FlowContext.IN_COMPARISON_NULL:
205
									scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
217
									if (!this.hideNullComparisonWarnings) {
218
										scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
219
									}
206
									flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
220
									flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
207
									return;
221
									return;
208
								case FlowContext.IN_COMPARISON_NON_NULL:
222
								case FlowContext.IN_COMPARISON_NON_NULL:
209
									scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
223
									if (!this.hideNullComparisonWarnings) {
224
										scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
225
									}
210
									flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
226
									flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
211
									return;
227
									return;
212
								case FlowContext.IN_ASSIGNMENT:
228
								case FlowContext.IN_ASSIGNMENT:
Lines 237-246 Link Here
237
					case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
253
					case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
238
						if (flowInfo.isDefinitelyNonNull(local)) {
254
						if (flowInfo.isDefinitelyNonNull(local)) {
239
							if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
255
							if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
240
								scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
256
								if (!this.hideNullComparisonWarnings) {
257
									scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
258
								}
241
								flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
259
								flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
242
							} else {
260
							} else {
243
								scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
261
								if (!this.hideNullComparisonWarnings) {
262
									scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
263
								}
244
								flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);	
264
								flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);	
245
							}
265
							}
246
							return;
266
							return;
Lines 253-263 Link Here
253
						if (flowInfo.isDefinitelyNull(local)) {
273
						if (flowInfo.isDefinitelyNull(local)) {
254
							switch(checkType & CONTEXT_MASK) {
274
							switch(checkType & CONTEXT_MASK) {
255
								case FlowContext.IN_COMPARISON_NULL:
275
								case FlowContext.IN_COMPARISON_NULL:
256
									scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
276
									if (!this.hideNullComparisonWarnings) {
277
										scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
278
									}
257
									flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
279
									flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
258
									return;
280
									return;
259
								case FlowContext.IN_COMPARISON_NON_NULL:
281
								case FlowContext.IN_COMPARISON_NON_NULL:
260
									scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
282
									if (!this.hideNullComparisonWarnings) {
283
										scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
284
									}
261
									flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
285
									flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
262
									return;
286
									return;
263
								case FlowContext.IN_ASSIGNMENT:
287
								case FlowContext.IN_ASSIGNMENT:
Lines 286-292 Link Here
286
						// never happens
310
						// never happens
287
				}
311
				}
288
			}
312
			}
289
			recordNullReference(local, reference, checkType);
313
			// if the contention is inside assert statement, we want to avoid null warnings only in case of
314
			// comparisons and not in case of assignment, instanceof, or may be null.
315
			if(!this.hideNullComparisonWarnings || checkType == MAY_NULL
316
					|| (checkType & CONTEXT_MASK) == FlowContext.IN_ASSIGNMENT
317
					|| (checkType & CONTEXT_MASK) == FlowContext.IN_INSTANCEOF) {
318
				recordNullReference(local, reference, checkType);
319
			}
290
			// prepare to re-check with try/catch flow info
320
			// prepare to re-check with try/catch flow info
291
		}
321
		}
292
	}
322
	}
(-)compiler/org/eclipse/jdt/internal/compiler/flow/FlowContext.java (-4 / +16 lines)
Lines 44-49 Link Here
44
		// any null related operation happening within the try block
44
		// any null related operation happening within the try block
45
45
46
boolean deferNullDiagnostic, preemptNullDiagnostic;
46
boolean deferNullDiagnostic, preemptNullDiagnostic;
47
/**
48
 * used to hide null comparison related warnings inside assert statements 
49
 */
50
public boolean hideNullComparisonWarnings = false;
47
51
48
public static final int
52
public static final int
49
  CAN_ONLY_NULL_NON_NULL = 0x0000,
53
  CAN_ONLY_NULL_NON_NULL = 0x0000,
Lines 542-551 Link Here
542
		case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
546
		case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
543
			if (flowInfo.isDefinitelyNonNull(local)) {
547
			if (flowInfo.isDefinitelyNonNull(local)) {
544
				if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
548
				if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
545
					scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
549
					if (!this.hideNullComparisonWarnings) {
550
						scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
551
					}
546
					flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
552
					flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
547
				} else {
553
				} else {
548
					scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
554
					if (!this.hideNullComparisonWarnings) {
555
						scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
556
					}
549
					flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
557
					flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
550
				}
558
				}
551
				return;
559
				return;
Lines 561-571 Link Here
561
			if (flowInfo.isDefinitelyNull(local)) {
569
			if (flowInfo.isDefinitelyNull(local)) {
562
				switch(checkType & CONTEXT_MASK) {
570
				switch(checkType & CONTEXT_MASK) {
563
					case FlowContext.IN_COMPARISON_NULL:
571
					case FlowContext.IN_COMPARISON_NULL:
564
						scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
572
						if (!this.hideNullComparisonWarnings) {
573
							scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
574
						}
565
						flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
575
						flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
566
						return;
576
						return;
567
					case FlowContext.IN_COMPARISON_NON_NULL:
577
					case FlowContext.IN_COMPARISON_NON_NULL:
568
						scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
578
						if (!this.hideNullComparisonWarnings) {
579
							scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
580
						}
569
						flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
581
						flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
570
						return;
582
						return;
571
					case FlowContext.IN_ASSIGNMENT:
583
					case FlowContext.IN_ASSIGNMENT:
(-)compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java (-21 / +67 lines)
Lines 127-135 Link Here
127
					if (flowInfo.isDefinitelyNonNull(local)) {
127
					if (flowInfo.isDefinitelyNonNull(local)) {
128
						this.nullReferences[i] = null;
128
						this.nullReferences[i] = null;
129
						if (this.nullCheckTypes[i] == (CAN_ONLY_NON_NULL | IN_COMPARISON_NON_NULL)) {
129
						if (this.nullCheckTypes[i] == (CAN_ONLY_NON_NULL | IN_COMPARISON_NON_NULL)) {
130
							scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
130
							if (!this.hideNullComparisonWarnings) {
131
								scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
132
							}
131
						} else {
133
						} else {
132
							scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
134
							if (!this.hideNullComparisonWarnings) {
135
								scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
136
							}
133
						}
137
						}
134
						continue;
138
						continue;
135
					}
139
					}
Lines 139-156 Link Here
139
					if (flowInfo.isDefinitelyNonNull(local)) {
143
					if (flowInfo.isDefinitelyNonNull(local)) {
140
						this.nullReferences[i] = null;
144
						this.nullReferences[i] = null;
141
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
145
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
142
							scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
146
							if (!this.hideNullComparisonWarnings) {
147
								scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
148
							}
143
						} else {
149
						} else {
144
							scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
150
							if (!this.hideNullComparisonWarnings) {
151
								scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
152
							}
145
						}
153
						}
146
						continue;
154
						continue;
147
					}
155
					}
148
					if (flowInfo.isDefinitelyNull(local)) {
156
					if (flowInfo.isDefinitelyNull(local)) {
149
						this.nullReferences[i] = null;
157
						this.nullReferences[i] = null;
150
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NULL)) {
158
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NULL)) {
151
							scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
159
							if (!this.hideNullComparisonWarnings) {
160
								scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
161
							}
152
						} else {
162
						} else {
153
							scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
163
							if (!this.hideNullComparisonWarnings) {
164
								scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
165
							}
154
						}
166
						}
155
						continue;
167
						continue;
156
					}
168
					}
Lines 163-172 Link Here
163
						this.nullReferences[i] = null;
175
						this.nullReferences[i] = null;
164
						switch(this.nullCheckTypes[i] & CONTEXT_MASK) {
176
						switch(this.nullCheckTypes[i] & CONTEXT_MASK) {
165
							case FlowContext.IN_COMPARISON_NULL:
177
							case FlowContext.IN_COMPARISON_NULL:
166
								scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
178
								if (!this.hideNullComparisonWarnings) {
179
									scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
180
								}
167
								continue;
181
								continue;
168
							case FlowContext.IN_COMPARISON_NON_NULL:
182
							case FlowContext.IN_COMPARISON_NON_NULL:
169
								scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
183
								if (!this.hideNullComparisonWarnings) {
184
									scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
185
								}
170
								continue;
186
								continue;
171
							case FlowContext.IN_ASSIGNMENT:
187
							case FlowContext.IN_ASSIGNMENT:
172
								scope.problemReporter().localVariableRedundantNullAssignment(local, expression);
188
								scope.problemReporter().localVariableRedundantNullAssignment(local, expression);
Lines 203-211 Link Here
203
					if (flowInfo.isDefinitelyNonNull(local)) {
219
					if (flowInfo.isDefinitelyNonNull(local)) {
204
						this.nullReferences[i] = null;
220
						this.nullReferences[i] = null;
205
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
221
						if (this.nullCheckTypes[i] == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
206
							scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
222
							if (!this.hideNullComparisonWarnings) {
223
								scope.problemReporter().localVariableRedundantCheckOnNonNull(local, expression);
224
							}
207
						} else {
225
						} else {
208
							scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
226
							if (!this.hideNullComparisonWarnings) {
227
								scope.problemReporter().localVariableNonNullComparedToNull(local, expression);
228
							}
209
						}
229
						}
210
						continue;
230
						continue;
211
					}
231
					}
Lines 218-227 Link Here
218
						this.nullReferences[i] = null;
238
						this.nullReferences[i] = null;
219
						switch(this.nullCheckTypes[i] & CONTEXT_MASK) {
239
						switch(this.nullCheckTypes[i] & CONTEXT_MASK) {
220
							case FlowContext.IN_COMPARISON_NULL:
240
							case FlowContext.IN_COMPARISON_NULL:
221
								scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
241
								if (!this.hideNullComparisonWarnings) {
242
									scope.problemReporter().localVariableRedundantCheckOnNull(local, expression);
243
								}
222
								continue;
244
								continue;
223
							case FlowContext.IN_COMPARISON_NON_NULL:
245
							case FlowContext.IN_COMPARISON_NON_NULL:
224
								scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
246
								if (!this.hideNullComparisonWarnings) {
247
									scope.problemReporter().localVariableNullComparedToNonNull(local, expression);
248
								}
225
								continue;
249
								continue;
226
							case FlowContext.IN_ASSIGNMENT:
250
							case FlowContext.IN_ASSIGNMENT:
227
								scope.problemReporter().localVariableRedundantNullAssignment(local, expression);
251
								scope.problemReporter().localVariableRedundantNullAssignment(local, expression);
Lines 395-422 Link Here
395
		case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
419
		case CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL:
396
			if (flowInfo.isDefinitelyNonNull(local)) {
420
			if (flowInfo.isDefinitelyNonNull(local)) {
397
				if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
421
				if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NON_NULL)) {
398
					scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
422
					if (!this.hideNullComparisonWarnings) {
423
						scope.problemReporter().localVariableRedundantCheckOnNonNull(local, reference);
424
					}
399
					flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
425
					flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
400
				} else {
426
				} else {
401
					scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
427
					if (!this.hideNullComparisonWarnings) {
428
						scope.problemReporter().localVariableNonNullComparedToNull(local, reference);
429
					}
402
					flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
430
					flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
403
				}
431
				}
404
			} else if (flowInfo.isDefinitelyNull(local)) {
432
			} else if (flowInfo.isDefinitelyNull(local)) {
405
				if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NULL)) {
433
				if (checkType == (CAN_ONLY_NULL_NON_NULL | IN_COMPARISON_NULL)) {
406
					scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
434
					if (!this.hideNullComparisonWarnings) {
435
						scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
436
					}
407
					flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
437
					flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
408
				} else {
438
				} else {
409
					scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
439
					if (!this.hideNullComparisonWarnings) {
440
						scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
441
					}
410
					flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
442
					flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
411
				}
443
				}
412
			} else if (this.upstreamNullFlowInfo.isDefinitelyNonNull(local) && !flowInfo.isPotentiallyNull(local)) {    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418
444
			} else if (this.upstreamNullFlowInfo.isDefinitelyNonNull(local) && !flowInfo.isPotentiallyNull(local)) {    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418
413
				flowInfo.markAsDefinitelyNonNull(local);
445
				flowInfo.markAsDefinitelyNonNull(local);
414
				recordNullReference(local, reference, checkType);
446
				if (!this.hideNullComparisonWarnings) {
447
					recordNullReference(local, reference, checkType);
448
				}
415
			} else if (! flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
449
			} else if (! flowInfo.cannotBeDefinitelyNullOrNonNull(local)) {
416
				if (flowInfo.isPotentiallyNonNull(local)) {
450
				if (flowInfo.isPotentiallyNonNull(local)) {
417
					recordNullReference(local, reference, CAN_ONLY_NON_NULL | checkType & CONTEXT_MASK);
451
					recordNullReference(local, reference, CAN_ONLY_NON_NULL | checkType & CONTEXT_MASK);
418
				} else {
452
				} else {
419
					recordNullReference(local, reference, checkType);
453
					if (!this.hideNullComparisonWarnings) {
454
						recordNullReference(local, reference, checkType);
455
					}
420
				}
456
				}
421
			}
457
			}
422
			return;
458
			return;
Lines 431-441 Link Here
431
			if (flowInfo.isDefinitelyNull(local)) {
467
			if (flowInfo.isDefinitelyNull(local)) {
432
				switch(checkType & CONTEXT_MASK) {
468
				switch(checkType & CONTEXT_MASK) {
433
					case FlowContext.IN_COMPARISON_NULL:
469
					case FlowContext.IN_COMPARISON_NULL:
434
						scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
470
						if (!this.hideNullComparisonWarnings) {
471
							scope.problemReporter().localVariableRedundantCheckOnNull(local, reference);
472
						}
435
						flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
473
						flowInfo.initsWhenFalse().setReachMode(FlowInfo.UNREACHABLE);
436
						return;
474
						return;
437
					case FlowContext.IN_COMPARISON_NON_NULL:
475
					case FlowContext.IN_COMPARISON_NON_NULL:
438
						scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
476
						if (!this.hideNullComparisonWarnings) {
477
							scope.problemReporter().localVariableNullComparedToNonNull(local, reference);
478
						}
439
						flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
479
						flowInfo.initsWhenTrue().setReachMode(FlowInfo.UNREACHABLE);
440
						return;
480
						return;
441
					case FlowContext.IN_ASSIGNMENT:
481
					case FlowContext.IN_ASSIGNMENT:
Lines 446-452 Link Here
446
						return;
486
						return;
447
				}
487
				}
448
			}
488
			}
449
			recordNullReference(local, reference, checkType);
489
			// if the contention is inside assert statement, we want to avoid null warnings only in case of
490
			// comparisons and not in case of assignment and instanceof
491
			if (!this.hideNullComparisonWarnings 
492
					|| (checkType & CONTEXT_MASK) == FlowContext.IN_ASSIGNMENT
493
					|| (checkType & CONTEXT_MASK) == FlowContext.IN_INSTANCEOF) {
494
				recordNullReference(local, reference, checkType);
495
			}
450
			return;
496
			return;
451
		case MAY_NULL :
497
		case MAY_NULL :
452
			if (flowInfo.isDefinitelyNonNull(local)) {
498
			if (flowInfo.isDefinitelyNonNull(local)) {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (-10 / +288 lines)
Lines 8156-8180 Link Here
8156
				"public class X {\n" +
8156
				"public class X {\n" +
8157
				"  void foo() {\n" +
8157
				"  void foo() {\n" +
8158
				"    Object o = null;\n" +
8158
				"    Object o = null;\n" +
8159
				"    assert(o != null);\n" +    // complain
8159
				"    assert(o != null);\n" +    // don't complain
8160
				"    if (o == null) { };\n" +   // complain
8160
				"    if (o == null) { };\n" +   // complain
8161
				"  }\n" +
8161
				"  }\n" +
8162
				"}\n"},
8162
				"}\n"},
8163
		"----------\n" +
8163
		"----------\n" +
8164
		"1. ERROR in X.java (at line 4)\n" +
8164
		"1. ERROR in X.java (at line 5)\n" +
8165
		"	assert(o != null);\n" +
8166
		"	       ^\n" +
8167
		"Null comparison always yields false: The variable o can only be null at this location\n" +
8168
		"----------\n" +
8169
		"2. ERROR in X.java (at line 5)\n" +
8170
		"	if (o == null) { };\n" +
8165
		"	if (o == null) { };\n" +
8171
		"	    ^\n" +
8166
		"	    ^\n" +
8172
		"Null comparison always yields false: The variable o cannot be null at this location\n" +
8167
		"Null comparison always yields false: The variable o cannot be null at this location\n" +
8173
		"----------\n" +
8168
		"----------\n" +
8174
		"3. WARNING in X.java (at line 5)\n" + 
8169
		"2. WARNING in X.java (at line 5)\n" +
8175
		"	if (o == null) { };\n" + 
8170
		"	if (o == null) { };\n" +
8176
		"	               ^^^\n" + 
8171
		"	               ^^^\n" +
8172
		"Dead code\n" +
8173
		"----------\n",
8174
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8175
	}
8176
}
8177
8178
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
8179
// Test to verify that asserts are exempted from null comparison warnings,
8180
// but this doesn't affect the downstream info.
8181
public void test0957_assert() {
8182
	if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8183
		this.runNegativeTest(
8184
			new String[] {
8185
				"X.java",
8186
				"public class X {\n" +
8187
				"  void m() {\n" +
8188
				"    X foo = new X();\n" +
8189
				"	 assert (foo != null);\n" +	//don't warn
8190
				"	 if (foo == null) {}\n" +
8191
				"    X foo2 = new X();\n" +
8192
				"	 assert (foo2 == null);\n" +	//don't warn
8193
				"	 if (foo2 == null) {}\n" +
8194
				"    X bar = null;\n" +
8195
				"	 assert (bar == null);\n" +	//don't warn
8196
				"	 if (bar == null) {}\n" +
8197
				"    X bar2 = null;\n" +
8198
				"	 assert (bar2 != null);\n" +	//don't warn
8199
				"	 if (bar2 == null) {}\n" +
8200
				"  }\n" +
8201
				"}\n"},
8202
		"----------\n" +
8203
		"1. ERROR in X.java (at line 5)\n" + 
8204
		"	if (foo == null) {}\n" + 
8205
		"	    ^^^\n" + 
8206
		"Null comparison always yields false: The variable foo cannot be null at this location\n" + 
8207
		"----------\n" + 
8208
		"2. WARNING in X.java (at line 5)\n" + 
8209
		"	if (foo == null) {}\n" + 
8210
		"	                 ^^\n" + 
8177
		"Dead code\n" + 
8211
		"Dead code\n" + 
8212
		"----------\n" + 
8213
		"3. ERROR in X.java (at line 8)\n" + 
8214
		"	if (foo2 == null) {}\n" + 
8215
		"	    ^^^^\n" + 
8216
		"Redundant null check: The variable foo2 can only be null at this location\n" + 
8217
		"----------\n" + 
8218
		"4. ERROR in X.java (at line 11)\n" + 
8219
		"	if (bar == null) {}\n" + 
8220
		"	    ^^^\n" + 
8221
		"Redundant null check: The variable bar can only be null at this location\n" + 
8222
		"----------\n" + 
8223
		"5. ERROR in X.java (at line 14)\n" + 
8224
		"	if (bar2 == null) {}\n" + 
8225
		"	    ^^^^\n" + 
8226
		"Null comparison always yields false: The variable bar2 cannot be null at this location\n" + 
8227
		"----------\n" + 
8228
		"6. WARNING in X.java (at line 14)\n" + 
8229
		"	if (bar2 == null) {}\n" + 
8230
		"	                  ^^\n" + 
8231
		"Dead code\n" + 
8232
		"----------\n",
8233
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8234
	}
8235
}
8236
8237
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
8238
// Test to verify that asserts are exempted from null comparison warnings,
8239
// but this doesn't affect the downstream info.
8240
public void test0958_assert() {
8241
	if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
8242
		this.runNegativeTest(
8243
			new String[] {
8244
				"X.java",
8245
				"import java.util.HashMap;\n" +
8246
				"public class X {\n" +
8247
				"  void m() {\n" +
8248
				"    HashMap<Integer,X> map = new HashMap<Integer,X>();\n" +
8249
				"	 X bar = null;\n" +
8250
				"    X foo = map.get(1);\n" +
8251
				"    if (foo == null) {\n" +
8252
				"	 	foo = new X();\n" +
8253
				"		map.put(1, foo);\n" +
8254
				"	 }\n" +
8255
				"	 assert (foo != null && bar == null);\n" +	// don't warn but do the null analysis
8256
				"	 if (foo != null) {}\n" +		// warn
8257
				"	 if (bar == null) {}\n" +		// warn
8258
				"  }\n" +
8259
				"}\n"},
8260
		"----------\n" +
8261
		"1. ERROR in X.java (at line 12)\n" + 
8262
		"	if (foo != null) {}\n" + 
8263
		"	    ^^^\n" + 
8264
		"Redundant null check: The variable foo cannot be null at this location\n" + 
8265
		"----------\n" + 
8266
		"2. ERROR in X.java (at line 13)\n" + 
8267
		"	if (bar == null) {}\n" + 
8268
		"	    ^^^\n" + 
8269
		"Redundant null check: The variable bar can only be null at this location\n" + 
8270
		"----------\n",
8271
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8272
	}
8273
}
8274
8275
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
8276
// Test to verify that asserts are exempted from null comparison warnings in a looping context,
8277
// but this doesn't affect the downstream info.
8278
public void test0959a_assert_loop() {
8279
	if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8280
		this.runNegativeTest(
8281
			new String[] {
8282
				"X.java",
8283
				"public class X {\n" +
8284
				"  void m() {\n" +
8285
				"    X foo = new X();\n" +
8286
				"    X foo2 = new X();\n" +
8287
				"    X bar = null;\n" +
8288
				"    X bar2 = null;\n" +
8289
				"	 while (true) {\n" +
8290
				"	 	assert (foo != null);\n" +	//don't warn
8291
				"	 	if (foo == null) {}\n" +
8292
				"	 	assert (foo2 == null);\n" +	//don't warn
8293
				"	 	if (foo2 == null) {}\n" +				
8294
				"	 	assert (bar == null);\n" +	//don't warn
8295
				"	 	if (bar == null) {}\n" +				
8296
				"	 	assert (bar2 != null);\n" +	//don't warn
8297
				"	 	if (bar2 == null) {}\n" +
8298
				"	 }\n" +
8299
				"  }\n" +
8300
				"}\n"},
8301
		"----------\n" +
8302
		"1. ERROR in X.java (at line 9)\n" + 
8303
		"	if (foo == null) {}\n" + 
8304
		"	    ^^^\n" + 
8305
		"Null comparison always yields false: The variable foo cannot be null at this location\n" + 
8306
		"----------\n" + 
8307
		"2. WARNING in X.java (at line 9)\n" + 
8308
		"	if (foo == null) {}\n" + 
8309
		"	                 ^^\n" + 
8310
		"Dead code\n" + 
8311
		"----------\n" + 
8312
		"3. ERROR in X.java (at line 11)\n" + 
8313
		"	if (foo2 == null) {}\n" + 
8314
		"	    ^^^^\n" + 
8315
		"Redundant null check: The variable foo2 can only be null at this location\n" + 
8316
		"----------\n" + 
8317
		"4. ERROR in X.java (at line 13)\n" + 
8318
		"	if (bar == null) {}\n" + 
8319
		"	    ^^^\n" + 
8320
		"Redundant null check: The variable bar can only be null at this location\n" + 
8321
		"----------\n" + 
8322
		"5. ERROR in X.java (at line 15)\n" + 
8323
		"	if (bar2 == null) {}\n" + 
8324
		"	    ^^^^\n" + 
8325
		"Null comparison always yields false: The variable bar2 cannot be null at this location\n" + 
8326
		"----------\n" + 
8327
		"6. WARNING in X.java (at line 15)\n" + 
8328
		"	if (bar2 == null) {}\n" + 
8329
		"	                  ^^\n" + 
8330
		"Dead code\n" + 
8331
		"----------\n",
8332
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8333
	}
8334
}
8335
8336
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
8337
// Test to verify that asserts are exempted from null comparison warnings in a looping context,
8338
// but this doesn't affect the downstream info.
8339
public void test0959b_assert_loop() {
8340
	if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8341
		this.runNegativeTest(
8342
			new String[] {
8343
				"X.java",
8344
				"public class X {\n" +
8345
				"  void m() {\n" +
8346
				"	 while (true) {\n" +
8347
				"   	X foo = new X();\n" +
8348
				"	 	assert (foo != null);\n" +	//don't warn
8349
				"	 	if (foo == null) {}\n" +
8350
				"    	X foo2 = new X();\n" +
8351
				"	 	assert (foo2 == null);\n" +	//don't warn
8352
				"	 	if (foo2 == null) {}\n" +
8353
				"    	X bar = null;\n" +
8354
				"	 	assert (bar == null);\n" +	//don't warn
8355
				"	 	if (bar == null) {}\n" +
8356
				"    	X bar2 = null;\n" +
8357
				"	 	assert (bar2 != null);\n" +	//don't warn
8358
				"	 	if (bar2 == null) {}\n" +
8359
				"	 }\n" +
8360
				"  }\n" +
8361
				"}\n"},
8362
		"----------\n" +
8363
		"1. ERROR in X.java (at line 6)\n" + 
8364
		"	if (foo == null) {}\n" + 
8365
		"	    ^^^\n" + 
8366
		"Null comparison always yields false: The variable foo cannot be null at this location\n" + 
8367
		"----------\n" + 
8368
		"2. WARNING in X.java (at line 6)\n" + 
8369
		"	if (foo == null) {}\n" + 
8370
		"	                 ^^\n" + 
8371
		"Dead code\n" + 
8372
		"----------\n" + 
8373
		"3. ERROR in X.java (at line 9)\n" + 
8374
		"	if (foo2 == null) {}\n" + 
8375
		"	    ^^^^\n" + 
8376
		"Redundant null check: The variable foo2 can only be null at this location\n" + 
8377
		"----------\n" + 
8378
		"4. ERROR in X.java (at line 12)\n" + 
8379
		"	if (bar == null) {}\n" + 
8380
		"	    ^^^\n" + 
8381
		"Redundant null check: The variable bar can only be null at this location\n" + 
8382
		"----------\n" + 
8383
		"5. ERROR in X.java (at line 15)\n" + 
8384
		"	if (bar2 == null) {}\n" + 
8385
		"	    ^^^^\n" + 
8386
		"Null comparison always yields false: The variable bar2 cannot be null at this location\n" + 
8387
		"----------\n" + 
8388
		"6. WARNING in X.java (at line 15)\n" + 
8389
		"	if (bar2 == null) {}\n" + 
8390
		"	                  ^^\n" + 
8391
		"Dead code\n" + 
8392
		"----------\n",
8393
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8394
	}
8395
}
8396
8397
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
8398
// Test to verify that asserts are exempted from null comparison warnings in a finally context,
8399
// but this doesn't affect the downstream info.
8400
public void test0960_assert_finally() {
8401
	if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8402
		this.runNegativeTest(
8403
			new String[] {
8404
				"X.java",
8405
				"public class X {\n" +
8406
				"  void m() {\n" +
8407
				"    X foo = new X();\n" +
8408
				"    X foo2 = new X();\n" +
8409
				"    X bar = null;\n" +
8410
				"    X bar2 = null;\n" +
8411
				"	 try {\n" +
8412
				"		System.out.println(\"Inside try\");\n" +
8413
				"	 }\n" +
8414
				"	 finally {\n" +
8415
				"	 	assert (foo != null);\n" +	//don't warn
8416
				"	 	if (foo == null) {}\n" +
8417
				"	 	assert (foo2 == null);\n" +	//don't warn
8418
				"	 	if (foo2 == null) {}\n" +				
8419
				"	 	assert (bar == null);\n" +	//don't warn
8420
				"	 	if (bar == null) {}\n" +				
8421
				"	 	assert (bar2 != null);\n" +	//don't warn
8422
				"	 	if (bar2 == null) {}\n" +
8423
				"	 }\n" +
8424
				"  }\n" +
8425
				"}\n"},
8426
		"----------\n" +
8427
		"1. ERROR in X.java (at line 12)\n" +
8428
		"	if (foo == null) {}\n" +
8429
		"	    ^^^\n" +
8430
		"Null comparison always yields false: The variable foo cannot be null at this location\n" +
8431
		"----------\n" +
8432
		"2. WARNING in X.java (at line 12)\n" +
8433
		"	if (foo == null) {}\n" +
8434
		"	                 ^^\n" +
8435
		"Dead code\n" +
8436
		"----------\n" +
8437
		"3. ERROR in X.java (at line 14)\n" +
8438
		"	if (foo2 == null) {}\n" +
8439
		"	    ^^^^\n" +
8440
		"Redundant null check: The variable foo2 can only be null at this location\n" +
8441
		"----------\n" +
8442
		"4. ERROR in X.java (at line 16)\n" +
8443
		"	if (bar == null) {}\n" +
8444
		"	    ^^^\n" +
8445
		"Redundant null check: The variable bar can only be null at this location\n" +
8446
		"----------\n" +
8447
		"5. ERROR in X.java (at line 18)\n" +
8448
		"	if (bar2 == null) {}\n" +
8449
		"	    ^^^^\n" +
8450
		"Null comparison always yields false: The variable bar2 cannot be null at this location\n" +
8451
		"----------\n" +
8452
		"6. WARNING in X.java (at line 18)\n" +
8453
		"	if (bar2 == null) {}\n" +
8454
		"	                  ^^\n" +
8455
		"Dead code\n" +
8178
		"----------\n",
8456
		"----------\n",
8179
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8457
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8180
	}
8458
	}

Return to bug 250056