don't blink with pure contract on gutter when starting to write a method

This commit is contained in:
peter
2014-09-15 14:49:58 +02:00
parent 2c20268f48
commit 8a71cfc404
2 changed files with 19 additions and 1 deletions
@@ -58,6 +58,7 @@ public class PurityInference {
if (body == null) return false;
final Ref<Boolean> impureFound = Ref.create(false);
final Ref<Boolean> hasReturns = Ref.create(false);
final List<PsiMethodCallExpression> calls = ContainerUtil.newArrayList();
body.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
@@ -68,6 +69,14 @@ public class PurityInference {
super.visitAssignmentExpression(expression);
}
@Override
public void visitReturnStatement(PsiReturnStatement statement) {
if (statement.getReturnValue() != null) {
hasReturns.set(true);
}
super.visitReturnStatement(statement);
}
@Override
public void visitPrefixExpression(PsiPrefixExpression expression) {
if (isMutatingOperation(expression.getOperationTokenType()) && !isLocalVarReference(expression.getOperand())) {
@@ -95,7 +104,7 @@ public class PurityInference {
}
});
if (impureFound.get() || calls.size() > 1) return false;
if (impureFound.get() || calls.size() > 1 || !hasReturns.get()) return false;
if (calls.isEmpty()) return true;
final PsiMethod called = calls.get(0).resolveMethod();
@@ -120,6 +120,15 @@ int smthPure() { return 3; }
"""
}
public void "test don't analyze methods without returns"() {
assertPure false, """
Object method() {
smthPure();
}
int smthPure() { return 3; }
"""
}
private void assertPure(boolean expected, String classBody) {
def clazz = myFixture.addClass("final class Foo { $classBody }")
assert expected == PurityInference.inferPurity(clazz.methods[0])