diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConditionalExpressionGenerator.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConditionalExpressionGenerator.java index 7be0bb6d2715..662b0e3f3e51 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConditionalExpressionGenerator.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ConditionalExpressionGenerator.java @@ -69,12 +69,22 @@ public final class ConditionalExpressionGenerator { PsiExpression condition = model.getCondition(); PsiExpression thenExpression = model.getThenExpression(); PsiExpression elseExpression = model.getElseExpression(); - if (PsiType.BOOLEAN.equals(model.getType())) { + if (PsiType.BOOLEAN.equals(model.getType()) || model.getType().equalsToText(CommonClassNames.JAVA_LANG_BOOLEAN)) { PsiLiteralExpression thenLiteral = ExpressionUtils.getLiteral(thenExpression); PsiLiteralExpression elseLiteral = ExpressionUtils.getLiteral(elseExpression); Boolean thenValue = thenLiteral == null ? null : tryCast(thenLiteral.getValue(), Boolean.class); Boolean elseValue = elseLiteral == null ? null : tryCast(elseLiteral.getValue(), Boolean.class); - if (thenValue != null || elseValue != null) { + if (thenValue != null && elseValue != null) { + if (thenValue.equals(elseValue)) { + // Equal branches are handled by separate inspections + return null; + } + if (thenValue) { + return new ConditionalExpressionGenerator("", condition); + } + return new ConditionalExpressionGenerator("", ct -> BoolUtils.getNegatedExpressionText(condition, ct)); + } + if ((thenValue != null || elseValue != null) && PsiType.BOOLEAN.equals(model.getType())) { return getAndOrGenerator(condition, thenExpression, elseExpression, thenValue, elseValue); } if (BoolUtils.areExpressionsOpposite(thenExpression, elseExpression)) { @@ -93,15 +103,6 @@ public final class ConditionalExpressionGenerator { PsiExpression elseExpression, Boolean thenValue, Boolean elseValue) { - if (thenValue != null && elseValue != null) { - if (thenValue.equals(elseValue)) { - return new ConditionalExpressionGenerator("", thenExpression); - } - if (thenValue) { - return new ConditionalExpressionGenerator("", condition); - } - return new ConditionalExpressionGenerator("", ct -> BoolUtils.getNegatedExpressionText(condition, ct)); - } if (thenValue != null) { if (thenValue) { return new ConditionalExpressionGenerator("||", ct -> joinConditions(condition, elseExpression, false, ct)); diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/TrivialIfInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/TrivialIfInspectionTest.java index 21327205b0b6..67ecc66663ca 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/TrivialIfInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/TrivialIfInspectionTest.java @@ -91,6 +91,25 @@ public class TrivialIfInspectionTest extends LightJavaInspectionTestCase { " else return false;\n" + "}\n"); } + + public void testReturnEqualBranches() { + // no warning: another inspection takes care about this + doMemberTest("\n" + + " boolean b(int x) {\n" + + " if (x > 20) return true;\n" + + " else return true;\n" + + "}\n"); + } + + public void testMethodCall() { + doMemberTest("void test(int x, Boolean foo) {\n" + + " if (x == 0) System.out.println(foo);\n" + + " else {\n" + + " /*'if' statement can be simplified*/if/**/ (x > 0) test(0, true);\n" + + " else test(0, false);\n" + + " }\n" + + "}"); + } @Override protected InspectionProfileEntry getInspection() {