Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2013-02-22 22:32:55 +0000
committerJayaprakash Arthanareeswaran2013-04-03 09:02:17 +0000
commit03f81049867c7013f420abfd6b051cc95502966c (patch)
treeb520a3e40a512508b1a571f6647d5d89c77dbeb1
parent7f149cbd31a29082f285cf448e823f215f36990d (diff)
downloadeclipse.jdt.core-03f81049867c7013f420abfd6b051cc95502966c.tar.gz
eclipse.jdt.core-03f81049867c7013f420abfd6b051cc95502966c.tar.xz
eclipse.jdt.core-03f81049867c7013f420abfd6b051cc95502966c.zip
Bug 401088 - [compiler][null] Wrong warning "Redundant null check"
inside nested try statement
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java92
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java13
2 files changed, 103 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 6245e577cf..6465eb952e 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2012 IBM Corporation and others.
+ * Copyright (c) 2005, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -27,6 +27,7 @@
* bug 376263 - Bogus "Potential null pointer access" warning
* bug 331649 - [compiler][null] consider null annotations for fields
* bug 382789 - [compiler][null] warn when syntactically-nonnull expression is compared against null
+ * bug 401088 - [compiler][null] Wrong warning "Redundant null check" inside nested try statement
*******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;
@@ -16210,4 +16211,93 @@ public void testBug345305_14() {
"Potential null pointer access: The variable s may be null at this location\n" +
"----------\n");
}
+// Bug 401088 - [compiler][null] Wrong warning "Redundant null check" inside nested try statement
+public void testBug401088() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ "\n" +
+ " private static void occasionallyThrowException() throws Exception {\n" +
+ " throw new Exception();\n" +
+ " }\n" +
+ "\n" +
+ " private static void open() throws Exception {\n" +
+ " occasionallyThrowException();\n" +
+ " }\n" +
+ "\n" +
+ " private static void close() throws Exception {\n" +
+ " occasionallyThrowException();\n" +
+ " }\n" +
+ "\n" +
+ " public static void main(String s[]) {\n" +
+ " Exception exc = null;\n" +
+ " try {\n" +
+ " open();\n" +
+ " // do more things\n" +
+ " }\n" +
+ " catch (Exception e) {\n" +
+ " exc = e;\n" +
+ " }\n" +
+ " finally {\n" +
+ " try {\n" +
+ " close();\n" +
+ " }\n" +
+ " catch (Exception e) {\n" +
+ " if (exc == null) // should not warn on this line\n" +
+ " exc = e;\n" +
+ " }\n" +
+ " }\n" +
+ " if (exc != null)\n" +
+ " System.out.println(exc);\n" +
+ " }\n" +
+ "}\n"
+ },
+ "java.lang.Exception");
+}
+// Bug 401088 - [compiler][null] Wrong warning "Redundant null check" inside nested try statement
+public void testBug401088a() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ "\n" +
+ " private static void occasionallyThrowException() throws Exception {\n" +
+ " throw new Exception();\n" +
+ " }\n" +
+ "\n" +
+ " private static void open() throws Exception {\n" +
+ " occasionallyThrowException();\n" +
+ " }\n" +
+ "\n" +
+ " private static void close() throws Exception {\n" +
+ " occasionallyThrowException();\n" +
+ " }\n" +
+ "\n" +
+ " public static void main(String s[]) {\n" +
+ " Exception exc = null;\n" +
+ " try {\n" +
+ " open();\n" +
+ " // do more things\n" +
+ " }\n" +
+ " catch (Exception e) {\n" +
+ " exc = e;\n" +
+ " }\n" +
+ " finally {\n" +
+ " try {\n" +
+ " close();\n" +
+ " }\n" +
+ " catch (Exception e) {\n" +
+ " if (exc == null) // should not warn on this line\n" +
+ " exc = e;\n" +
+ " }\n" +
+ " finally { System.out.print(1); }\n" +
+ " }\n" +
+ " if (exc != null)\n" +
+ " System.out.println(exc);\n" +
+ " }\n" +
+ "}\n"
+ },
+ "1java.lang.Exception");
+}
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java
index 4df867697d..96987f6ca0 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -15,6 +15,7 @@
* bug 358903 - Filter practically unimportant resource leak warnings
* bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
* bug 388996 - [compiler][resource] Incorrect 'potential resource leak'
+ * bug 401088 - [compiler][null] Wrong warning "Redundant null check" inside nested try statement
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
@@ -113,6 +114,11 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
if (this.subRoutineStartLabel == null) {
// no finally block -- this is a simplified copy of the else part
+ if (flowContext instanceof FinallyFlowContext) {
+ // if this TryStatement sits inside another TryStatement,
+ // report into the initsOnFinally of the outer try-block.
+ flowContext.initsOnFinally = ((FinallyFlowContext)flowContext).tryContext.initsOnFinally;
+ }
// process the try block in a context handling the local exceptions.
ExceptionHandlingFlowContext handlingContext =
new ExceptionHandlingFlowContext(
@@ -253,6 +259,11 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
UnconditionalFlowInfo subInfo;
// analyse finally block first
insideSubContext = new InsideSubRoutineFlowContext(flowContext, this);
+ if (flowContext instanceof FinallyFlowContext) {
+ // if this TryStatement sits inside another TryStatement,
+ // let the nested context report into the initsOnFinally of the outer try-block.
+ insideSubContext.initsOnFinally = ((FinallyFlowContext)flowContext).tryContext.initsOnFinally;
+ }
// process the try block in a context handling the local exceptions.
// (advance instantiation so we can wire this into the FinallyFlowContext)

Back to the top