diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/localCanBeFinal/LocalCanBeFinal.java b/java/java-analysis-impl/src/com/intellij/codeInspection/localCanBeFinal/LocalCanBeFinal.java index 112aee0814de..e508a77b11b6 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/localCanBeFinal/LocalCanBeFinal.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/localCanBeFinal/LocalCanBeFinal.java @@ -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 checkCodeBlock(final PsiCodeBlock body, InspectionManager manager, boolean onTheFly) { + private List 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 writtenVariables = new ArrayList(ControlFlowUtil.getWrittenVariables(flow, start, end, false)); + final List problems = new ArrayList(); final HashSet ssaVarsSet = new HashSet(); body.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitCodeBlock(PsiCodeBlock block) { + if (block.getParent() instanceof PsiLambdaExpression && block != body) { + final List 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 result = new ArrayList(ssaVarsSet); + final ArrayList result = new ArrayList(ssaVarsSet); if (body.getParent() instanceof PsiMethod) { PsiMethod method = (PsiMethod)body.getParent(); @@ -215,7 +223,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool { iterator.remove(); } } - List problems = new ArrayList(result.size()); + for (PsiVariable variable : result) { final PsiIdentifier nameIdenitier = variable.getNameIdentifier(); PsiElement problemElement = nameIdenitier != null ? nameIdenitier : variable; diff --git a/java/java-tests/testData/inspection/localCanBeFinal/LambdaBody/expected.xml b/java/java-tests/testData/inspection/localCanBeFinal/LambdaBody/expected.xml new file mode 100644 index 000000000000..1da1935ad438 --- /dev/null +++ b/java/java-tests/testData/inspection/localCanBeFinal/LambdaBody/expected.xml @@ -0,0 +1,16 @@ + + + + Junk.java + 4 + Local variable or parameter can be final + Variable <code>i</code> can have <code>final</code> modifier + + + Junk.java + 3 + Local variable or parameter can be final + Variable <code>r</code> can have <code>final</code> modifier + + + diff --git a/java/java-tests/testData/inspection/localCanBeFinal/LambdaBody/src/Junk.java b/java/java-tests/testData/inspection/localCanBeFinal/LambdaBody/src/Junk.java new file mode 100644 index 000000000000..f89a1e2f4786 --- /dev/null +++ b/java/java-tests/testData/inspection/localCanBeFinal/LambdaBody/src/Junk.java @@ -0,0 +1,10 @@ +public final class Junk { + public void sillyMethod() { + Runnable r = () -> { + int i = 0; + System.out.println(i); + }; + } +} + + diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/LocalCanBeFinalTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/LocalCanBeFinalTest.java index c8e5ecf6fa1c..7dfa608fc142 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/LocalCanBeFinalTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/LocalCanBeFinalTest.java @@ -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(); + } }