mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
local can be final inside lambda body (IDEA-112630)
This commit is contained in:
+12
-4
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -77,7 +77,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private List<ProblemDescriptor> checkCodeBlock(final PsiCodeBlock body, InspectionManager manager, boolean onTheFly) {
|
||||
private List<ProblemDescriptor> checkCodeBlock(final PsiCodeBlock body, final InspectionManager manager, final boolean onTheFly) {
|
||||
if (body == null) return null;
|
||||
final ControlFlow flow;
|
||||
try {
|
||||
@@ -120,9 +120,17 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
|
||||
final List<PsiVariable> writtenVariables = new ArrayList<PsiVariable>(ControlFlowUtil.getWrittenVariables(flow, start, end, false));
|
||||
|
||||
final List<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>();
|
||||
final HashSet<PsiVariable> ssaVarsSet = new HashSet<PsiVariable>();
|
||||
body.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override public void visitCodeBlock(PsiCodeBlock block) {
|
||||
if (block.getParent() instanceof PsiLambdaExpression && block != body) {
|
||||
final List<ProblemDescriptor> descriptors = checkCodeBlock(block, manager, onTheFly);
|
||||
if (descriptors != null) {
|
||||
problems.addAll(descriptors);
|
||||
}
|
||||
return;
|
||||
}
|
||||
super.visitCodeBlock(block);
|
||||
PsiElement anchor = block;
|
||||
if (block.getParent() instanceof PsiSwitchStatement) {
|
||||
@@ -177,7 +185,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
}
|
||||
});
|
||||
|
||||
ArrayList<PsiVariable> result = new ArrayList<PsiVariable>(ssaVarsSet);
|
||||
final ArrayList<PsiVariable> result = new ArrayList<PsiVariable>(ssaVarsSet);
|
||||
|
||||
if (body.getParent() instanceof PsiMethod) {
|
||||
PsiMethod method = (PsiMethod)body.getParent();
|
||||
@@ -215,7 +223,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
List<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>(result.size());
|
||||
|
||||
for (PsiVariable variable : result) {
|
||||
final PsiIdentifier nameIdenitier = variable.getNameIdentifier();
|
||||
PsiElement problemElement = nameIdenitier != null ? nameIdenitier : variable;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Junk.java</file>
|
||||
<line>4</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local variable or parameter can be final</problem_class>
|
||||
<description>Variable <code>i</code> can have <code>final</code> modifier</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>Junk.java</file>
|
||||
<line>3</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local variable or parameter can be final</problem_class>
|
||||
<description>Variable <code>r</code> can have <code>final</code> modifier</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
public final class Junk {
|
||||
public void sillyMethod() {
|
||||
Runnable r = () -> {
|
||||
int i = 0;
|
||||
System.out.println(i);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
@@ -102,4 +117,10 @@ public class LocalCanBeFinalTest extends InspectionTestCase {
|
||||
myTool.REPORT_VARIABLES = true;
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testLambdaBody() throws Exception {
|
||||
myTool.REPORT_PARAMETERS = true;
|
||||
myTool.REPORT_VARIABLES = true;
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user