mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-106227 (Incorrect inspection "Local variable is redundant' in try-with-resources clause)
This commit is contained in:
+11
-13
@@ -25,6 +25,7 @@ import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.fixes.InlineVariableFix;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import com.siyeh.ig.psiutils.VariableAccessUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -114,7 +115,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
private boolean isCopyVariable(PsiVariable variable) {
|
||||
final PsiExpression initializer = variable.getInitializer();
|
||||
final PsiExpression initializer = ParenthesesUtils.stripParentheses(variable.getInitializer());
|
||||
if (!(initializer instanceof PsiReferenceExpression)) {
|
||||
return false;
|
||||
}
|
||||
@@ -126,6 +127,9 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
if (!(referent instanceof PsiLocalVariable || referent instanceof PsiParameter)) {
|
||||
return false;
|
||||
}
|
||||
if (!(referent instanceof PsiResourceVariable) && variable instanceof PsiResourceVariable) {
|
||||
return false;
|
||||
}
|
||||
final PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class);
|
||||
if (containingScope == null) {
|
||||
return false;
|
||||
@@ -169,7 +173,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
return false;
|
||||
}
|
||||
final PsiReturnStatement returnStatement = (PsiReturnStatement)nextStatement;
|
||||
final PsiExpression returnValue = returnStatement.getReturnValue();
|
||||
final PsiExpression returnValue = ParenthesesUtils.stripParentheses(returnStatement.getReturnValue());
|
||||
if (!(returnValue instanceof PsiReferenceExpression)) {
|
||||
return false;
|
||||
}
|
||||
@@ -178,10 +182,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
if (referent == null || !referent.equals(variable)) {
|
||||
return false;
|
||||
}
|
||||
if (isVariableUsedInFollowingDeclarations(variable, declarationStatement)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return !isVariableUsedInFollowingDeclarations(variable, declarationStatement);
|
||||
}
|
||||
|
||||
private boolean isImmediatelyThrown(PsiVariable variable) {
|
||||
@@ -206,7 +207,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
return false;
|
||||
}
|
||||
final PsiThrowStatement throwStatement = (PsiThrowStatement)nextStatement;
|
||||
final PsiExpression returnValue = throwStatement.getException();
|
||||
final PsiExpression returnValue = ParenthesesUtils.stripParentheses(throwStatement.getException());
|
||||
if (!(returnValue instanceof PsiReferenceExpression)) {
|
||||
return false;
|
||||
}
|
||||
@@ -214,10 +215,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
if (referent == null || !referent.equals(variable)) {
|
||||
return false;
|
||||
}
|
||||
if (isVariableUsedInFollowingDeclarations(variable, declarationStatement)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return !isVariableUsedInFollowingDeclarations(variable, declarationStatement);
|
||||
}
|
||||
|
||||
private boolean isImmediatelyAssigned(PsiVariable variable) {
|
||||
@@ -253,7 +251,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
if (tokenType != JavaTokenType.EQ) {
|
||||
return false;
|
||||
}
|
||||
final PsiExpression rhs = assignmentExpression.getRExpression();
|
||||
final PsiExpression rhs = ParenthesesUtils.stripParentheses(assignmentExpression.getRExpression());
|
||||
if (!(rhs instanceof PsiReferenceExpression)) {
|
||||
return false;
|
||||
}
|
||||
@@ -305,7 +303,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection {
|
||||
continue;
|
||||
}
|
||||
final PsiVariable nextVariable = (PsiVariable)declaration;
|
||||
final PsiExpression initializer = nextVariable.getInitializer();
|
||||
final PsiExpression initializer = ParenthesesUtils.stripParentheses(nextVariable.getInitializer());
|
||||
if (!referenceFound && initializer instanceof PsiReferenceExpression) {
|
||||
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)initializer;
|
||||
final PsiElement referent = referenceExpression.resolve();
|
||||
|
||||
+35
-3
@@ -5,8 +5,8 @@ class C {
|
||||
System.out.println(s2 + s3);
|
||||
|
||||
AutoCloseable r1 = null;
|
||||
try (AutoCloseable r2 = r1, AutoCloseable r3 = null) {
|
||||
System.out.println(r2 + r3);
|
||||
try (AutoCloseable r2 = r1; AutoCloseable r3 = null) {
|
||||
System.out.println(r2.toString() + r3.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ class C {
|
||||
System.out.println(r2 + r3);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int boxing(Long l) {
|
||||
long ll = l;
|
||||
return (int) ll;
|
||||
@@ -83,4 +83,36 @@ class C {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void neededResourceVariable(java.io.InputStream in) throws java.io.IOException {
|
||||
try (java.io.InputStream inn = in) {
|
||||
final int read = inn.read();
|
||||
// do stuff with in
|
||||
}
|
||||
}
|
||||
|
||||
int parenthesized() {
|
||||
final int i = 1 + 2;
|
||||
return (i);
|
||||
}
|
||||
|
||||
void parenthesized2() {
|
||||
final RuntimeException t = new RuntimeException();
|
||||
throw (t);
|
||||
}
|
||||
|
||||
void parenthesized3(int i) {
|
||||
int j = (i);
|
||||
}
|
||||
|
||||
void parenthesized4(int k) {
|
||||
final int j = 1;
|
||||
k = (j);
|
||||
}
|
||||
|
||||
void parenthesized5() {
|
||||
final int j = 1;
|
||||
int k = (j);
|
||||
System.out.println(k);
|
||||
}
|
||||
|
||||
}
|
||||
+41
-21
@@ -21,13 +21,6 @@
|
||||
<description>Local variable <code>r1</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>8</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>r2</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>15</line>
|
||||
@@ -42,20 +35,6 @@
|
||||
<description>Local variable <code>s3</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>19</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>r2</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>19</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>r3</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>30</line>
|
||||
@@ -98,5 +77,46 @@
|
||||
<description>Local variable <code>value</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>94</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>i</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>99</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>t</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>108</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>j</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>104</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>j</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>113</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>j</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>C.java</file>
|
||||
<line>114</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class>
|
||||
<description>Local variable <code>k</code> is redundant #loc</description>
|
||||
</problem>
|
||||
|
||||
</problems>
|
||||
Reference in New Issue
Block a user