mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
AutoCloseableResourceInspection: ignore immediately closed : IDEA-171866
This commit is contained in:
+5
@@ -27,6 +27,8 @@ import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.callMatcher.CallMatcher;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.MethodMatcher;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import one.util.streamex.StreamEx;
|
||||
@@ -44,6 +46,8 @@ import java.util.List;
|
||||
*/
|
||||
public class AutoCloseableResourceInspectionBase extends ResourceInspection {
|
||||
|
||||
private static final CallMatcher CLOSE = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, "close");
|
||||
|
||||
private static final List<String> DEFAULT_IGNORED_TYPES =
|
||||
Arrays.asList("java.util.stream.Stream", "java.util.stream.IntStream", "java.util.stream.LongStream", "java.util.stream.DoubleStream");
|
||||
@SuppressWarnings("PublicField")
|
||||
@@ -198,6 +202,7 @@ public class AutoCloseableResourceInspectionBase extends ResourceInspection {
|
||||
if (!isResourceCreation(expression)) {
|
||||
return false;
|
||||
}
|
||||
if (CLOSE.test(ExpressionUtils.getCallForQualifier(expression))) return false;
|
||||
final PsiVariable variable = ResourceInspection.getVariable(expression);
|
||||
if(variable instanceof PsiResourceVariable || isResourceEscapingFromMethod(variable, expression)) return false;
|
||||
if (variable == null) return true;
|
||||
|
||||
+10
@@ -163,6 +163,16 @@ public class AutoCloseableResourceInspectionTest extends LightInspectionTestCase
|
||||
"}");
|
||||
}
|
||||
|
||||
public void testClosedResource() {
|
||||
doTest("import java.io.*;\n" +
|
||||
"\n" +
|
||||
"class X {\n" +
|
||||
" private static void example(int a) throws IOException {\n" +
|
||||
" new FileOutputStream(\"\").close();\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LocalInspectionTool getInspection() {
|
||||
return new AutoCloseableResourceInspection();
|
||||
|
||||
Reference in New Issue
Block a user