[groovy] honor returns when computing last statement (IDEA-178598)

This commit is contained in:
Daniil Ovchinnikov
2017-09-13 16:19:35 +02:00
parent 11e95c565d
commit 822473f00e
2 changed files with 18 additions and 13 deletions
@@ -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<GrStatement> collectReturns(@Nullable PsiElement element) {
return collectReturns(element, element instanceof GrCodeBlock || element instanceof GroovyFile);
@@ -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) {
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
} else {
return
}
}
''', GroovyUnnecessaryContinueInspection)
}