Try-with-resource support: variable usage inspection

This commit is contained in:
Roman Shevchenko
2011-02-22 20:33:24 +01:00
parent c59584e8ef
commit 0f2e1cd4f6
5 changed files with 136 additions and 47 deletions
@@ -0,0 +1,51 @@
import java.lang.Exception;
class C {
static class MyResource implements AutoCloseable {
@Override public void close() { }
}
void m1() throws Exception {
MyResource r1;
try (r1 = new MyResource()) {
System.out.println(r1);
}
try (MyResource r2 = new MyResource()) {
System.out.println(r2);
}
MyResource r3 = new MyResource();
try (MyResource r = r3) {
System.out.println(r);
}
}
void m2() throws Exception {
MyResource r1 = <warning descr="Variable 'r1' initializer 'null' is redundant">null</warning>;
try (r1 = new MyResource()) {
System.out.println(r1);
}
try (MyResource r2 = <warning descr="Variable 'r2' initializer 'new MyResource()' is redundant">new MyResource()</warning>) {
r2 = null; // todo: check for NPE
System.out.println(r2);
}
MyResource r3 = null;
System.out.println(r3);
try (r3 = <warning descr="The value 'new MyResource()' assigned to r3 is never used">new MyResource()</warning>) { }
try (MyResource <warning descr="Variable 'r4' is never used">r4</warning> = new MyResource()) { }
try (MyResource r5 = new MyResource()) {
System.out.println(r5);
r5 = <warning descr="The value 'new MyResource()' assigned to r5 is never used">new MyResource()</warning>;
}
MyResource <warning descr="Variable 'r6' is never assigned">r6</warning>;
try (MyResource r = <error descr="Variable 'r6' might not have been initialized">r6</error>) {
System.out.println(r);
}
}
}
@@ -4,6 +4,7 @@ import com.intellij.ExtensionPoints;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
import com.intellij.codeInspection.defUse.DefUseInspection;
import com.intellij.codeInspection.reference.EntryPoint;
import com.intellij.codeInspection.reference.RefElement;
import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection;
@@ -31,7 +32,7 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new UnusedSymbolLocalInspection(), new UncheckedWarningLocalInspection()};
return new LocalInspectionTool[]{new UnusedSymbolLocalInspection(), new UncheckedWarningLocalInspection(), new DefUseInspection()};
}
public void testDuplicateAnnotations() throws Exception {
@@ -192,6 +193,10 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
doTest(false, false);
}
public void testTryWithResourcesWarn() throws Exception {
doTest(true, false);
}
public void testSafeVarargsApplicability() throws Exception {
doTest(true, false);
}