diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java index 5d4f1218ac31..28e8c09aad4c 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntention.java @@ -26,6 +26,7 @@ import com.siyeh.ipp.base.Intention; import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -88,13 +89,10 @@ public class ReplaceIfWithConditionalIntention extends Intention { if (elseReturnValue == null) { return; } - final PsiElement method = PsiTreeUtil.getParentOfType(thenReturn, PsiMethod.class, PsiLambdaExpression.class); - if (method == null) { + final String conditional = getConditional(condition, thenReturn, thenReturnValue, elseReturnValue); + if (conditional == null) { return; } - final PsiType returnType = method instanceof PsiMethod ? ((PsiMethod)method).getReturnType() - : LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)method); - final String conditional = getConditionalText(condition, thenReturnValue, elseReturnValue, returnType); replaceIfStatement(ifStatement, "return " + conditional + ';'); } else if (ReplaceIfWithConditionalPredicate.isReplaceableMethodCall(ifStatement)) { @@ -159,20 +157,31 @@ public class ReplaceIfWithConditionalIntention extends Intention { if (elseReturnValue == null) { return; } - final PsiMethod method = PsiTreeUtil.getParentOfType(thenBranch, PsiMethod.class); - if (method == null) { - return; - } - final PsiType methodType = method.getReturnType(); - final String conditional = getConditionalText(condition, thenReturnValue, elseReturnValue, methodType); - if (conditional == null) { - return; - } + final String conditional = getConditional(condition, thenBranch, thenReturnValue, elseReturnValue); + if (conditional == null) return; replaceIfStatement(ifStatement, "return " + conditional + ';'); elseBranch.delete(); } } + @Nullable + private static String getConditional(PsiExpression condition, + PsiElement thenBranch, + PsiExpression thenReturnValue, + PsiExpression elseReturnValue) { + final PsiElement method = PsiTreeUtil.getParentOfType(thenBranch, PsiMethod.class, PsiLambdaExpression.class); + if (method == null) { + return null; + } + final PsiType methodType = method instanceof PsiMethod ? ((PsiMethod)method).getReturnType() + : LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)method); + final String conditional = getConditionalText(condition, thenReturnValue, elseReturnValue, methodType); + if (conditional == null) { + return null; + } + return conditional; + } + private static void replaceIfStatement(PsiIfStatement ifStatement, String text) { PsiElement parent = ifStatement.getParent(); final Collection comments = PsiTreeUtil.findChildrenOfType(ifStatement, PsiComment.class); diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/replaceIfWithConditional/InsideLambda1.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/replaceIfWithConditional/InsideLambda1.java new file mode 100644 index 000000000000..c82c044167f7 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/replaceIfWithConditional/InsideLambda1.java @@ -0,0 +1,15 @@ +class Test { + interface I { + int m(); + } + + String foo() { + I i = () -> { + if (true) { + return 1; + } + return new Integer(2); + }; + return null; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/replaceIfWithConditional/InsideLambda1_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/replaceIfWithConditional/InsideLambda1_after.java new file mode 100644 index 000000000000..6a9591a2f06e --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/trivialif/replaceIfWithConditional/InsideLambda1_after.java @@ -0,0 +1,12 @@ +class Test { + interface I { + int m(); + } + + String foo() { + I i = () -> { + return true ? 1 : new Integer(2); + }; + return null; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntentionTest.java index da8ada756b4f..007fb4c3fed7 100644 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntentionTest.java +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/trivialif/ReplaceIfWithConditionalIntentionTest.java @@ -30,6 +30,10 @@ public class ReplaceIfWithConditionalIntentionTest extends IPPTestCase { public void testComments() { doTest(); } public void testInsideLambda() { doTest(); } + public void testInsideLambda1() { + doTest(); + } + @Override protected String getIntentionName() { return IntentionPowerPackBundle.message("replace.if.with.conditional.intention.name");