diff options
| author | Stephan Herrmann | 2013-01-17 20:21:30 +0000 |
|---|---|---|
| committer | Stephan Herrmann | 2013-01-17 20:21:30 +0000 |
| commit | 5022709d9b0934611de6ddf11e79067d32b49675 (patch) | |
| tree | a40b7e7a1c4f02a3bf3c8a2ae899414d473b31b4 | |
| parent | 47f26d8c04a2e4027106cdff03430707105db9f2 (diff) | |
| download | eclipse.jdt.core-5022709d9b0934611de6ddf11e79067d32b49675.tar.gz eclipse.jdt.core-5022709d9b0934611de6ddf11e79067d32b49675.tar.xz eclipse.jdt.core-5022709d9b0934611de6ddf11e79067d32b49675.zip | |
Bug 376263: Bogus "Potential null pointer access" warning
2 files changed, 46 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java index 077a4468cd..c67196f8e5 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java @@ -24,6 +24,7 @@ * bug 367879 - Incorrect "Potential null pointer access" warning on statement after try-with-resources within try-finally * bug 383690 - [compiler] location of error re uninitialized final field should be aligned * bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null" + * bug 376263 - Bogus "Potential null pointer access" warning *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -32,6 +33,7 @@ import java.util.Map; import junit.framework.Test; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.core.tests.util.Util; import org.eclipse.jdt.core.util.ClassFileBytesDisassembler; @@ -53,6 +55,7 @@ public NullReferenceTest(String name) { static { // TESTS_NAMES = new String[] { "testBug345305_14" }; // TESTS_NAMES = new String[] { "test0515_try_finally" }; +// TESTS_NAMES = new String[] { "testBug376263" }; // TESTS_NUMBERS = new int[] { 561 }; // TESTS_RANGE = new int[] { 1, 2049 }; } @@ -15623,6 +15626,41 @@ public void testBug360328d() { "",/* expected error */ JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); } +public void testBug376263() { + Map customOptions = getCompilerOptions(); + customOptions.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR); + runConformTest( + new String[] { + "Test.java", + "public class Test {\n" + + " private int x;\n" + + "\n" + + " static void test(Test[] array) {\n" + + " Test elem = null;\n" + + " int i = 0;\n" + + " while (i < array.length) {\n" + + " if (i == 0) {\n" + + " elem = array[0];\n" + + " }\n" + + " if (elem != null) {\n" + + " while (true) {\n" + + " if (elem.x >= 0 || i >= array.length) { // should not warn here\n" + + " break;\n" + + " }\n" + + " elem = array[i++];\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + }, + "", + null/*classLibraries*/, + true/*shouldFlush*/, + null/*vmArgs*/, + customOptions, + null/*requestor*/); +} // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null" // simplified: only try-finally involved diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java index 5020e8aea0..5dcf13ac5e 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java @@ -15,6 +15,7 @@ * bug 365859 - [compiler][null] distinguish warnings based on flow analysis vs. null annotations * bug 385626 - @NonNull fails across loop boundaries * bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null" + * bug 376263 - Bogus "Potential null pointer access" warning *******************************************************************************/ package org.eclipse.jdt.internal.compiler.flow; @@ -148,6 +149,7 @@ public void complainOnDeferredNullChecks(BlockScope scope, FlowInfo callerFlowIn addPotentialNullInfoFrom(this.innerFlowInfos[i]); } this.innerFlowContextsCount = 0; + FlowInfo upstreamCopy = this.upstreamNullFlowInfo.copy(); UnconditionalFlowInfo flowInfo = this.upstreamNullFlowInfo. addPotentialNullInfoFrom(callerFlowInfo.unconditionalInitsWithoutSideEffect()); if ((this.tagBits & FlowContext.DEFER_NULL_DIAGNOSTIC) != 0) { @@ -277,8 +279,12 @@ public void complainOnDeferredNullChecks(BlockScope scope, FlowInfo callerFlowIn default: // never happens } - this.parent.recordUsingNullReference(scope, local, location, - this.nullCheckTypes[i], flowInfo); + // https://bugs.eclipse.org/376263: avoid further deferring if the upstream info + // already has definite information (which might get lost for deferred checking). + if (!(this.nullCheckTypes[i] == MAY_NULL && upstreamCopy.isDefinitelyNonNull(local))) { + this.parent.recordUsingNullReference(scope, local, location, + this.nullCheckTypes[i], flowInfo); + } } } else { |
