From 8a71cfc4049d35b016697c43bd9c414992477c31 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 15 Sep 2014 14:40:47 +0200 Subject: [PATCH] don't blink with pure contract on gutter when starting to write a method --- .../codeInspection/dataFlow/PurityInference.java | 11 ++++++++++- .../PurityInferenceFromSourceTest.groovy | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/PurityInference.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/PurityInference.java index 2e6d3dac06ba..29f530aba0f6 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/PurityInference.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/PurityInference.java @@ -58,6 +58,7 @@ public class PurityInference { if (body == null) return false; final Ref impureFound = Ref.create(false); + final Ref hasReturns = Ref.create(false); final List 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(); diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy b/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy index 815155e61fd2..c21e09c00a11 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy @@ -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])