From 822473f00ebeceb10774f16dcfbde03a0e95255b Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Wed, 13 Sep 2017 16:16:28 +0200 Subject: [PATCH] [groovy] honor returns when computing last statement (IDEA-178598) --- .../codeInspection/utils/ControlFlowUtils.java | 14 ++------------ .../lang/highlighting/GrInspectionTest.groovy | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java index 450f5d0abffe..458a88e632a4 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/utils/ControlFlowUtils.java @@ -415,20 +415,10 @@ public class ControlFlowUtils { } private static boolean statementIsLastInBlock(@NotNull GrStatementOwner block, @NotNull GrStatement statement) { - final GrStatement[] statements = block.getStatements(); - for (int i = statements.length - 1; i >= 0; i--) { - final GrStatement childStatement = statements[i]; - if (statement.equals(childStatement)) { - return true; - } - if (!(childStatement instanceof GrReturnStatement)) { - return false; - } - } - return false; + GrStatement lastStatement = ArrayUtil.getLastElement(block.getStatements()); + return statement == lastStatement; } - @NotNull public static List collectReturns(@Nullable PsiElement element) { return collectReturns(element, element instanceof GrCodeBlock || element instanceof GroovyFile); diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy index 21767b186e71..8054da137ab9 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -292,6 +292,21 @@ for(i in []) { print 4 } } + +for (i in []) { + if (cond) { + continue + } + return +} + +for (i in []) { + if (cond) { + continue + } else { + return + } +} ''', GroovyUnnecessaryContinueInspection) }