diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java index 9e6ea841a280..deeed49144b2 100644 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java +++ b/java/java-impl/src/com/intellij/codeInspection/dataFlow/ControlFlowAnalyzer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -754,8 +754,13 @@ class ControlFlowAnalyzer extends JavaElementVisitor { @Override public void visitTryStatement(PsiTryStatement statement) { startElement(statement); - PsiCodeBlock finallyBlock = statement.getFinallyBlock(); + PsiResourceList resourceList = statement.getResourceList(); + if (resourceList != null) { + resourceList.accept(this); + } + + PsiCodeBlock finallyBlock = statement.getFinallyBlock(); if (finallyBlock != null) { myCatchStack.push(new CatchDescriptor(finallyBlock)); } @@ -817,6 +822,21 @@ class ControlFlowAnalyzer extends JavaElementVisitor { if (catchBlock != null) catchBlock.accept(this); } + @Override + public void visitResourceList(PsiResourceList resourceList) { + startElement(resourceList); + + List variables = resourceList.getResourceVariables(); + for (PsiResourceVariable variable : variables) { + PsiExpression initializer = variable.getInitializer(); + if (initializer != null) { + initializeVariable(variable, initializer); + } + } + + finishElement(resourceList); + } + @Override public void visitWhileStatement(PsiWhileStatement statement) { startElement(statement); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResourcesWarn.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResourcesWarn.java index c2adf2667a93..796de41f705c 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResourcesWarn.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting7/TryWithResourcesWarn.java @@ -1,5 +1,3 @@ -import java.lang.Exception; - class C { static class MyResource implements AutoCloseable { @Override public void close() { } @@ -25,4 +23,21 @@ class C { System.out.println(r); } } + + interface MyResourceProvider { + MyResource getResource(); + } + + void m3() throws Exception { + MyResourceProvider provider = null; + try (MyResource r = provider.getResource()) { + System.out.println(r); + } + } + + void m4() { + try (MyResource r = null) { + System.out.println(r); + } + } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java index ae05e2cb85f5..350deee39287 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/LightAdvHighlightingJdk7Test.java @@ -20,6 +20,7 @@ import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.compiler.JavacQuirksInspection; +import com.intellij.codeInspection.dataFlow.DataFlowInspection; import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; import com.intellij.codeInspection.defUse.DefUseInspection; import com.intellij.codeInspection.redundantCast.RedundantCastInspection; @@ -146,7 +147,7 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase { public void testNumericLiterals() throws Exception { doTest(false, false); } public void testMultiCatch() throws Exception { doTest(false, false); } public void testTryWithResources() throws Exception { doTest(false, false); } - public void testTryWithResourcesWarn() throws Exception { doTest(true, false, new DefUseInspection()); } + public void testTryWithResourcesWarn() throws Exception { doTest(true, false, new DefUseInspection(), new DataFlowInspection()); } public void testSafeVarargsApplicability() throws Exception { doTest(true, false); } public void testUncheckedGenericsArrayCreation() throws Exception { doTest(true, false); } public void testPreciseRethrow() throws Exception { doTest(false, false); }