### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java,v retrieving revision 1.100 diff -u -r1.100 ConditionalExpression.java --- compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java 4 Mar 2011 21:22:49 -0000 1.100 +++ compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java 12 Aug 2011 20:44:15 -0000 @@ -11,6 +11,7 @@ * bug 133125 - [compiler][null] need to report the null status of expressions and analyze them simultaneously * bug 292478 - Report potentially null across variable assignment * bug 324178 - [null] ConditionalExpression.nullStatus(..) doesn't take into account the analysis of condition itself + * bug 354554 - [null] conditional with redundant condition yields weak error message *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -156,6 +157,15 @@ this.nullStatus = ifTrueNullStatus; return; } + if (trueBranchInfo.reachMode() != FlowInfo.REACHABLE) { + this.nullStatus = ifFalseNullStatus; + return; + } + if (falseBranchInfo.reachMode() != FlowInfo.REACHABLE) { + this.nullStatus = ifTrueNullStatus; + return; + } + // is there a chance of null (or non-null)? -> potentially null etc. // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 int status = 0; #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.120 diff -u -r1.120 NullReferenceTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java 28 Jul 2011 17:06:24 -0000 1.120 +++ src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java 12 Aug 2011 20:44:34 -0000 @@ -17,6 +17,7 @@ * bug 338303 - Warning about Redundant assignment conflicts with definite assignment * bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop * bug 324178 - [null] ConditionalExpression.nullStatus(..) doesn't take into account the analysis of condition itself + * bug 354554 - [null] conditional with redundant condition yields weak error message *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -14898,4 +14899,49 @@ "----------\n"); } } +// Bug 354554 - [null] conditional with redundant condition yields weak error message +public void testBug354554() { + this.runNegativeTest( + new String[] { + "Bug354554.java", + "public class Bug354554{\n" + + " void foo() {\n" + + " Object u = new Object();\n" + + " Object r = (u == null ? u : null);\n" + // condition is always false - should not spoil subsequent null-analysis + " System.out.println(r.toString());\n" + // should strongly complain: r is definitely null + " }\n" + + "}\n" + }, + "----------\n" + + "1. ERROR in Bug354554.java (at line 4)\n" + + " Object r = (u == null ? u : null);\n" + + " ^\n" + + "Null comparison always yields false: The variable u cannot be null at this location\n" + + "----------\n" + + "2. ERROR in Bug354554.java (at line 5)\n" + + " System.out.println(r.toString());\n" + + " ^\n" + + "Null pointer access: The variable r can only be null at this location\n" + + "----------\n"); +} +//Bug 354554 - [null] conditional with redundant condition yields weak error message +public void testBug354554b() { + this.runNegativeTest( + new String[] { + "Bug354554.java", + "public class Bug354554{\n" + + " void foo() {\n" + + " Object u = new Object();\n" + + " Object r = (u != null ? u : null);\n" + // condition is always true - should not spoil subsequent null-analysis + " System.out.println(r.toString());\n" + // don't complain: r is definitely non-null + " }\n" + + "}\n" + }, + "----------\n" + + "1. ERROR in Bug354554.java (at line 4)\n" + + " Object r = (u != null ? u : null);\n" + + " ^\n" + + "Redundant null check: The variable u cannot be null at this location\n" + + "----------\n"); +} } \ No newline at end of file