### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java,v retrieving revision 1.44 diff -u -r1.44 LoopingFlowContext.java --- compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java 7 Mar 2009 01:08:10 -0000 1.44 +++ compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java 10 Nov 2009 09:07:46 -0000 @@ -405,6 +405,9 @@ } else { scope.problemReporter().localVariableNullComparedToNonNull(local, reference); } + } else if (this.upstreamNullFlowInfo.isDefinitelyNonNull(local) && !flowInfo.isPotentiallyNull(local)) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418 + flowInfo.markAsDefinitelyNonNull(local); + recordNullReference(local, reference, checkType); } else if (! flowInfo.cannotBeDefinitelyNullOrNonNull(local)) { if (flowInfo.isPotentiallyNonNull(local)) { recordNullReference(local, reference, CAN_ONLY_NON_NULL | checkType & CONTEXT_MASK); #P org.eclipse.jdt.core.tests.compiler Index: src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java,v retrieving revision 1.81 diff -u -r1.81 NullReferenceTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java 27 Aug 2009 15:26:58 -0000 1.81 +++ src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java 10 Nov 2009 09:07:54 -0000 @@ -4313,8 +4313,8 @@ ""); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=220788 -public void _test0470_while() { - this.runConformTest( +public void test0470_while() { + this.runNegativeTest( new String[] { "X.java", "public class X {\n" + @@ -4332,7 +4332,12 @@ " }\n" + "}" }, - "ERROR: o cannot be null on first if"); + "----------\n" + + "1. ERROR in X.java (at line 5)\n" + + " if (o != null && o.toString().equals(\"o\")) {\n" + + " ^\n" + + "Redundant null check: The variable o cannot be null at this location\n" + + "----------\n"); } // null analysis -- try/finally public void test0500_try_finally() { @@ -10130,4 +10135,121 @@ "Potential null pointer access: The variable o1 may be null at this location\n" + "----------\n"); } + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418 +// Test to verify that redundant null checks are properly reported in all loops +public void testBug291418a() { + if (this.complianceLevel >= ClassFileConstants.JDK1_5) { + this.runNegativeTest( + new String[] { + "X.java", + "class X {\n" + + " void foo(int[] argArray) {\n" + + " int[] array = {2};\n" + + " int[] collectionVar = {1,2};\n" + + " if(argArray == null) return;" + + " for(int x:collectionVar) {\n" + + " if (collectionVar == null);\n" + // collectionVar cannot be null here + " if (array == null);\n" + // array is not null here + " if (argArray == null);\n" + // argArray cannot be null here + " }\n" + + " int count = 0;\n" + + " do {\n" + + " count++;\n" + + " if (array == null);\n" + // array is not null here + " if (argArray == null);\n" + // argArray cannot be null here + " } while (count<10);\n" + + " for (int i=0; i<2; i++) {\n" + + " if (array == null);\n" + // array is not null here + " if (argArray == null);\n" + // argArray cannot be null here + " }\n" + + " while (true) {\n" + + " if (array == null);\n" + // array is not null here + " if (argArray == null);\n" + // argArray cannot be null here + " }\n" + + " }\n" + + "}"}, + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " if (collectionVar == null);\n" + + " ^^^^^^^^^^^^^\n" + + "Null comparison always yields false: The variable collectionVar cannot be null at this location\n" + + "----------\n" + + "2. ERROR in X.java (at line 7)\n" + + " if (array == null);\n" + + " ^^^^^\n" + + "Null comparison always yields false: The variable array cannot be null at this location\n" + + "----------\n" + + "3. ERROR in X.java (at line 8)\n" + + " if (argArray == null);\n" + + " ^^^^^^^^\n" + + "Null comparison always yields false: The variable argArray cannot be null at this location\n" + + "----------\n" + + "4. ERROR in X.java (at line 13)\n" + + " if (array == null);\n" + + " ^^^^^\n" + + "Null comparison always yields false: The variable array cannot be null at this location\n" + + "----------\n" + + "5. ERROR in X.java (at line 14)\n" + + " if (argArray == null);\n" + + " ^^^^^^^^\n" + + "Null comparison always yields false: The variable argArray cannot be null at this location\n" + + "----------\n" + + "6. ERROR in X.java (at line 17)\n" + + " if (array == null);\n" + + " ^^^^^\n" + + "Null comparison always yields false: The variable array cannot be null at this location\n" + + "----------\n" + + "7. ERROR in X.java (at line 18)\n" + + " if (argArray == null);\n" + + " ^^^^^^^^\n" + + "Null comparison always yields false: The variable argArray cannot be null at this location\n" + + "----------\n" + + "8. ERROR in X.java (at line 21)\n" + + " if (array == null);\n" + + " ^^^^^\n" + + "Null comparison always yields false: The variable array cannot be null at this location\n" + + "----------\n" + + "9. ERROR in X.java (at line 22)\n" + + " if (argArray == null);\n" + + " ^^^^^^^^\n" + + "Null comparison always yields false: The variable argArray cannot be null at this location\n" + + "----------\n"); + } +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418 +// Test to verify that redundant null checks are properly reported +// in a loop in case the null status is modified downstream in the loop +public void testBug291418b() { + if (this.complianceLevel >= ClassFileConstants.JDK1_5) { + this.runNegativeTest( + new String[] { + "X.java", + "class X {\n" + + " void foo(int[] argArray) {\n" + + " int[] array = {2};\n" + + " int[] collectionVar = {1,2};\n" + + " if(argArray == null) return;" + + " for(int x:collectionVar) {\n" + + " if (collectionVar == null);\n" + // collectionVar cannot be null here + " if (array == null);\n" + // array is not null in first iteration but assigned null later in the loop. So we keep quiet + " if (argArray == null);\n" + // argArray cannot be null here + " array = null;\n" + + " }\n" + + " }\n" + + "}"}, + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " if (collectionVar == null);\n" + + " ^^^^^^^^^^^^^\n" + + "Null comparison always yields false: The variable collectionVar cannot be null at this location\n" + + "----------\n" + + "2. ERROR in X.java (at line 8)\n" + + " if (argArray == null);\n" + + " ^^^^^^^^\n" + + "Null comparison always yields false: The variable argArray cannot be null at this location\n" + + "----------\n"); + } +} } \ No newline at end of file