mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
RedundantExplicitCloseInspection: support parentheses in close(); fixed support of resource expression; check field qualifiers
This commit is contained in:
+11
-15
@@ -3,8 +3,11 @@ package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.siyeh.ig.callMatcher.CallMatcher;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.EquivalenceChecker;
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -25,28 +28,21 @@ public class RedundantExplicitCloseInspection extends AbstractBaseJavaLocalInspe
|
||||
|
||||
PsiCodeBlock tryBlock = statement.getTryBlock();
|
||||
if (tryBlock == null) return;
|
||||
PsiStatement[] statements = tryBlock.getStatements();
|
||||
if(statements.length == 0) return;
|
||||
PsiStatement last = statements[statements.length - 1];
|
||||
PsiStatement last = ArrayUtil.getLastElement(tryBlock.getStatements());
|
||||
PsiExpressionStatement expressionStatement = tryCast(last, PsiExpressionStatement.class);
|
||||
if(expressionStatement == null) return;
|
||||
PsiMethodCallExpression call = tryCast(expressionStatement.getExpression(), PsiMethodCallExpression.class);
|
||||
if (!CLOSE.test(call)) return;
|
||||
PsiReferenceExpression reference = tryCast(call.getMethodExpression().getQualifierExpression(), PsiReferenceExpression.class);
|
||||
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
|
||||
PsiReferenceExpression reference = tryCast(PsiUtil.skipParenthesizedExprDown(qualifier), PsiReferenceExpression.class);
|
||||
if(reference == null) return;
|
||||
PsiVariable variable = tryCast(reference.resolve(), PsiVariable.class);
|
||||
if(variable == null) return;
|
||||
boolean isReferenceToResourceVariable = StreamEx.of(resourceList.iterator())
|
||||
.anyMatch(element -> {
|
||||
if (element instanceof PsiResourceVariable && variable == element) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
PsiReferenceExpression ref = tryCast(element, PsiReferenceExpression.class);
|
||||
if (ref == null) return false;
|
||||
return ref.resolve() == variable;
|
||||
}
|
||||
});
|
||||
boolean isReferenceToResourceVariable = StreamEx.of(resourceList.iterator()).anyMatch(
|
||||
element -> variable == element ||
|
||||
element instanceof PsiResourceExpression &&
|
||||
EquivalenceChecker.getCanonicalPsiEquivalence()
|
||||
.expressionsAreEquivalent(reference, ((PsiResourceExpression)element).getExpression()));
|
||||
if(!isReferenceToResourceVariable) return;
|
||||
holder.registerProblem(last, InspectionsBundle.message("inspection.redundant.explicit.close"),
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DeleteRedundantCloseFix());
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
class MyAutoCloseable implements AutoCloseable {
|
||||
@Override
|
||||
void close() {
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// "Remove redundant close" "true"
|
||||
|
||||
class MyAutoCloseable implements AutoCloseable {
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class RemoveTry {
|
||||
final MyAutoCloseable ac;
|
||||
|
||||
public void main(RemoveTry other) {
|
||||
try(other.ac) {
|
||||
System.out.println("asdasd");
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Remove redundant close" "true"
|
||||
|
||||
class MyAutoCloseable implements AutoCloseable {
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class RemoveTry {
|
||||
public static void main(MyAutoCloseable ac) {
|
||||
try(ac) {
|
||||
System.out.println("asdasd");
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
class MyAutoCloseable implements AutoCloseable {
|
||||
@Override
|
||||
void close() {
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ class RemoveTry {
|
||||
public static void main(String[] args) {
|
||||
try(MyAutoCloseable ac = new MyAutoCloseable()) {
|
||||
System.out.println("asdasd");
|
||||
ac.close<caret>();
|
||||
(ac).close<caret>();
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Remove redundant close" "false"
|
||||
|
||||
class MyAutoCloseable implements AutoCloseable {
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class RemoveTry {
|
||||
final MyAutoCloseable ac;
|
||||
|
||||
public void main(RemoveTry other) {
|
||||
try(other.ac) {
|
||||
System.out.println("asdasd");
|
||||
this.ac.clo<caret>se();
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Remove redundant close" "true"
|
||||
|
||||
class MyAutoCloseable implements AutoCloseable {
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class RemoveTry {
|
||||
final MyAutoCloseable ac;
|
||||
|
||||
public void main(RemoveTry other) {
|
||||
try(other.ac) {
|
||||
System.out.println("asdasd");
|
||||
other.ac.clo<caret>se();
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Remove redundant close" "true"
|
||||
|
||||
class MyAutoCloseable implements AutoCloseable {
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class RemoveTry {
|
||||
public static void main(MyAutoCloseable ac) {
|
||||
try(ac) {
|
||||
System.out.println("asdasd");
|
||||
(ac).close<caret>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user