View | Details | Raw Unified | Return to bug 305590
Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java (-3 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 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 32-43 Link Here
32
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
32
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
33
	LocalVariableBinding local = this.expression.localVariableBinding();
33
	LocalVariableBinding local = this.expression.localVariableBinding();
34
	if (local != null && (local.type.tagBits & TagBits.IsBaseType) == 0) {
34
	if (local != null && (local.type.tagBits & TagBits.IsBaseType) == 0) {
35
		flowContext.recordUsingNullReference(currentScope, local,
36
			this.expression, FlowContext.CAN_ONLY_NULL | FlowContext.IN_INSTANCEOF, flowInfo);
37
		flowInfo = this.expression.analyseCode(currentScope, flowContext, flowInfo).
35
		flowInfo = this.expression.analyseCode(currentScope, flowContext, flowInfo).
38
			unconditionalInits();
36
			unconditionalInits();
39
		FlowInfo initsWhenTrue = flowInfo.copy();
37
		FlowInfo initsWhenTrue = flowInfo.copy();
40
		initsWhenTrue.markAsComparedEqualToNonNull(local);
38
		initsWhenTrue.markAsComparedEqualToNonNull(local);
39
		flowContext.recordUsingNullReference(currentScope, local,
40
				this.expression, FlowContext.CAN_ONLY_NULL | FlowContext.IN_INSTANCEOF, flowInfo);
41
		// no impact upon enclosing try context
41
		// no impact upon enclosing try context
42
		return FlowInfo.conditional(initsWhenTrue, flowInfo.copy());
42
		return FlowInfo.conditional(initsWhenTrue, flowInfo.copy());
43
	}
43
	}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (+26 lines)
Lines 11636-11639 Link Here
11636
		"    12  return\n";
11636
		"    12  return\n";
11637
	checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
11637
	checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
11638
}
11638
}
11639
11640
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=305590
11641
// To verify that a "instanceof always yields false" warning is not elicited in the
11642
// case when the expression has been assigned a non null value in the instanceof check.
11643
public void testBug305590() {
11644
	runNegativeTest(
11645
		new String[] {
11646
			"X.java",
11647
			"public class X {\n" +
11648
			"  public void foo() {\n" +
11649
			"	 Object str = null;\n" +
11650
			"	 if ((str = \"str\") instanceof String) {}\n" + // shouldn't warn
11651
			"	 str = null;\n" +
11652
			"	 if ((str = \"str\") instanceof Number) {}\n" + // shouldn't warn
11653
			"	 str = null;\n" +
11654
			"	 if (str instanceof String) {}\n" + // should warn
11655
			"  }\n" +
11656
			"}"},
11657
		"----------\n" +
11658
		"1. ERROR in X.java (at line 8)\n" + 
11659
		"	if (str instanceof String) {}\n" + 
11660
		"	    ^^^\n" + 
11661
		"instanceof always yields false: The variable str can only be null at this location\n" + 
11662
		"----------\n",
11663
	    JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11664
}
11639
}
11665
}

Return to bug 305590