diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java index b660d25de1b8..11ef82506a19 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/bugs/GroovyResultOfObjectAllocationIgnoredInspection.java @@ -65,14 +65,10 @@ public class GroovyResultOfObjectAllocationIgnoredInspection extends BaseInspect if (refElement == null) return; //new expression is not correct so we shouldn't check it final PsiElement parent = newExpression.getParent(); - if (parent instanceof GrClosableBlock) { - return; - } if (parent instanceof GrCodeBlock || parent instanceof GroovyFile) { - if (parent instanceof GrOpenBlock) { - final GrOpenBlock openBlock = (GrOpenBlock)parent; - if (ControlFlowUtils.openBlockCompletesWithStatement(openBlock, newExpression)) { + if (parent instanceof GrOpenBlock || parent instanceof GrClosableBlock) { + if (ControlFlowUtils.openBlockCompletesWithStatement(((GrCodeBlock)parent), newExpression)) { return; } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java index 995b0302494d..16b39c2fb69a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java @@ -330,22 +330,16 @@ public class ControlFlowUtils { } } - public static boolean openBlockCompletesWithStatement( - @NotNull GrOpenBlock body, - @NotNull GrStatement statement) { + public static boolean openBlockCompletesWithStatement(@NotNull GrCodeBlock body, @NotNull GrStatement statement) { GroovyPsiElement elementToCheck = statement; while (true) { - if (elementToCheck == null) { - return false; - } - final GroovyPsiElement container = - getContainingStatementOrBlock(elementToCheck); - if (container == null) { - return false; - } - if (isLoop(container)) { - return false; - } + if (elementToCheck == null) return false; + + final GroovyPsiElement container = getContainingStatementOrBlock(elementToCheck); + if (container == null) return false; + + if (isLoop(container)) return false; + if (container instanceof GrCodeBlock) { if (elementToCheck instanceof GrStatement) { final GrCodeBlock codeBlock = (GrCodeBlock) container; @@ -353,7 +347,7 @@ public class ControlFlowUtils { return false; } } - if (container instanceof GrOpenBlock) { + if (container instanceof GrOpenBlock || container instanceof GrClosableBlock) { if (container.equals(body)) { return true; } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java index ef725d71b557..384e207e25c1 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java @@ -149,6 +149,7 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase { public void testDefaultMapConstructorWhenDefConstructorExists() throws Throwable {doTest();} public void testSingleAllocationInClosure() throws Throwable {doTest(new GroovyResultOfObjectAllocationIgnoredInspection());} + public void testUnusedAllocationInClosure() throws Throwable {doTest(new GroovyResultOfObjectAllocationIgnoredInspection());} public void testUnresolvedLhsAssignment() throws Throwable { doTest(new GroovyUnresolvedAccessInspection()); } diff --git a/plugins/groovy/testdata/highlighting/UnusedAllocationInClosure.groovy b/plugins/groovy/testdata/highlighting/UnusedAllocationInClosure.groovy new file mode 100644 index 000000000000..cf6b0bbd2e25 --- /dev/null +++ b/plugins/groovy/testdata/highlighting/UnusedAllocationInClosure.groovy @@ -0,0 +1,5 @@ +def cl = { + new Object() + new Object() +} +print cl() \ No newline at end of file