IDEA-106665 Groovy highlight empty return statement if something should be returned

This commit is contained in:
Max Medvedev
2013-05-06 09:16:04 +04:00
parent 674d6cc73d
commit a4fd13920e
2 changed files with 50 additions and 29 deletions
@@ -32,7 +32,6 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrAssertStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
@@ -120,38 +119,19 @@ public class MissingReturnInspection extends GroovySuppressableInspectionTool {
ControlFlowUtils.visitAllExitPoints(block, new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (instruction instanceof MaybeReturnInstruction) {
if (((MaybeReturnInstruction)instruction).mayReturnValue()) {
sometimesHaveReturn.set(true);
}
else {
alwaysHaveReturn.set(false);
}
if (instruction instanceof ThrowingInstruction) return true;
if (instruction instanceof MaybeReturnInstruction && ((MaybeReturnInstruction)instruction).mayReturnValue()) {
sometimesHaveReturn.set(true);
return true;
}
final PsiElement element = instruction.getElement();
if (element instanceof GrReturnStatement) {
if (instruction.getElement() instanceof GrReturnStatement && returnValue != null) {
sometimesHaveReturn.set(true);
if (returnValue != null) {
hasExplicitReturn.set(true);
}
}
else if (instruction instanceof ThrowingInstruction) {
//skip throwing instructions
}
else if (element instanceof GrAssertStatement) {
sometimesHaveReturn.set(true);
int count = 0;
for (Instruction _i : instruction.allSuccessors()) {
count++;
}
if (count <= 1) {
alwaysHaveReturn.set(false);
}
}
else {
alwaysHaveReturn.set(false);
hasExplicitReturn.set(true);
return true;
}
alwaysHaveReturn.set(false);
return true;
}
});
@@ -169,6 +169,47 @@ List createFilters3() {
''')
}
void testReturnWithoutValue0() {
doTextText('''\
int foo() {
if (abc) {
return
}
return 2
<warning>}</warning>
''')
}
void testReturnWithoutValue1() {
doTextText('''\
int foo() {
return
<warning>}</warning>
''')
}
void testReturnWithoutValue2() {
doTextText('''\
void foo() {
if (abc) {
return
}
print 2
} //no error
''')
}
void testReturnWithoutValue3() {
doTextText('''\
void foo() {
return
} //no error
''')
}
void doTextText(String text) {
myFixture.configureByText('___.groovy', text)
myFixture.enableInspections(MissingReturnInspection)