IDEA-115468 (for loop variables, catch block parameters and resource variables not reported by "local variable or parameter can be final" inspection)

This commit is contained in:
Bas Leijdekkers
2013-10-28 16:44:04 +01:00
parent 0024cf32e8
commit e62f352ab9
8 changed files with 93 additions and 1 deletions
@@ -145,6 +145,23 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
}
}
@Override
public void visitCatchSection(PsiCatchSection section) {
super.visitCatchSection(section);
final PsiParameter parameter = section.getParameter();
if (PsiTreeUtil.getParentOfType(parameter, PsiClass.class) != PsiTreeUtil.getParentOfType(body, PsiClass.class)) {
return;
}
final PsiCodeBlock catchBlock = section.getCatchBlock();
if (catchBlock == null) return;
final int from = flow.getStartOffset(catchBlock);
final int end = flow.getEndOffset(catchBlock);
if (!ControlFlowUtil.getWrittenVariables(flow, from, end, false).contains(parameter)) {
writtenVariables.remove(parameter);
result.add(parameter);
}
}
@Override public void visitForeachStatement(PsiForeachStatement statement) {
super.visitForeachStatement(statement);
final PsiParameter param = statement.getIterationParameter();
@@ -176,6 +193,22 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
if (declaredElement instanceof PsiVariable) result.add(declaredElement);
}
}
@Override
public void visitForStatement(PsiForStatement statement) {
super.visitForStatement(statement);
final PsiStatement initialization = statement.getInitialization();
if (!(initialization instanceof PsiDeclarationStatement)) {
return;
}
final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)initialization;
final PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
for (final PsiElement declaredElement : declaredElements) {
if (declaredElement instanceof PsiVariable) {
result.add(declaredElement);
}
}
}
});
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>CatchParameter.java</file>
<line>8</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local variable or parameter can be final</problem_class>
<description>Parameter &lt;code&gt;e&lt;/code&gt; can have &lt;code&gt;final&lt;/code&gt; modifier</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
import java.io.*;
class CatchParameter {
void m() {
try (InputStream in = new FileInputStream("filename")) { // don't warn about 'in' because it is implicitly final
} catch (FileNotFoundException | RuntimeException e) { // don't warn about 'e' because it is implicitly final
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>For.java</file>
<line>3</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local variable or parameter can be final</problem_class>
<description>Variable &lt;code&gt;it&lt;/code&gt; can have &lt;code&gt;final&lt;/code&gt; modifier</description>
</problem>
</problems>
@@ -0,0 +1,7 @@
class For {
void f(List<String> list) {
for (Iterator<String> it = list.iterator(); it.hasNext();) { // 'it' can be final but not reported
}
for (int i = 0; i < 10; i++) {}
}
}
@@ -15,6 +15,11 @@
<line>9</line>
<description>foo final</description>
</problem>
<problem>
<file>Junk.java</file>
<line>13</line>
<description>e final</description>
</problem>
<problem>
<file>Junk.java</file>
<line>15</line>
@@ -15,6 +15,11 @@
<line>9</line>
<description>foo final</description>
</problem>
<problem>
<file>Junk.java</file>
<line>13</line>
<description>e final</description>
</problem>
<problem>
<file>Junk.java</file>
<line>15</line>
@@ -131,8 +131,20 @@ public class LocalCanBeFinalTest extends InspectionTestCase {
}
public void testNestedForeach() throws Exception {
myTool.REPORT_VARIABLES = true;
myTool.REPORT_PARAMETERS = false;
myTool.REPORT_VARIABLES = true;
doTest();
}
public void testFor() throws Exception {
myTool.REPORT_PARAMETERS = false;
myTool.REPORT_VARIABLES = true;
doTest();
}
public void testCatchParameter() throws Exception {
myTool.REPORT_PARAMETERS = false;
myTool.REPORT_VARIABLES = true;
doTest();
}
}