#ref statement
default.not.last.case.in.switch.problem.descriptor=#ref branch not last case in 'switch' statement #loc
loop.statements.that.dont.loop.problem.descriptor=#ref statement does not loop #loc
conditional.expression.with.identical.branches.problem.descriptor=Conditional expression #ref with identical branches #loc
-conditional.expression.with.similar.branches.problem.descriptor=Conditional expression #ref with similar branches #loc
+conditional.can.be.pushed.inside.expression.problem.descriptor=Conditional expression can be pushed inside branch #loc
if.statement.with.identical.branches.problem.descriptor=#ref statement with identical branches #loc
duplicate.condition.problem.descriptor=Duplicate condition #ref #loc
duplicate.condition.ignore.method.calls.option=Ignore method calls in condition
@@ -1123,8 +1126,6 @@ standard.variable.names.ignore.override.option=Ignore for parameter names identi
static.variable.naming.convention.mutable.option=Check 'static final' fields with a mutable type
boolean.method.name.must.start.with.question.table.column.name=Boolean method name prefix
conditional.expression.with.identical.branches.collapse.quickfix=Collapse conditional expression
-conditional.expression.with.identical.branches.push.inside.quickfix=Push conditional inside expression
-conditional.expression.with.identical.branches.collapse.quickfix.family=Conditional expression can be simplified
redundant.else.unwrap.quickfix=Remove redundant 'else'
constant.conditional.expression.problem.descriptor=#ref can be simplified to ''{0}'' #loc
constant.conditional.expression.simplify.quickfix=Simplify
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/ConditionalCanBePushedInsideExpressionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/ConditionalCanBePushedInsideExpressionInspection.java
new file mode 100644
index 000000000000..56427c51b476
--- /dev/null
+++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/ConditionalCanBePushedInsideExpressionInspection.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2000-2017 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.siyeh.ig.controlflow;
+
+import com.intellij.codeInspection.ProblemDescriptor;
+import com.intellij.codeInspection.ProblemHighlightType;
+import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.*;
+import com.siyeh.InspectionGadgetsBundle;
+import com.siyeh.ig.BaseInspection;
+import com.siyeh.ig.BaseInspectionVisitor;
+import com.siyeh.ig.InspectionGadgetsFix;
+import com.siyeh.ig.psiutils.EquivalenceChecker;
+import com.siyeh.ig.psiutils.ParenthesesUtils;
+import org.jetbrains.annotations.Nls;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import javax.swing.*;
+
+/**
+ * @author Bas Leijdekkers
+ */
+public class ConditionalCanBePushedInsideExpressionInspection extends BaseInspection {
+
+ public boolean ignoreSingleArgument = true;
+
+ @Nls
+ @NotNull
+ @Override
+ public String getDisplayName() {
+ return InspectionGadgetsBundle.message("conditional.can.be.pushed.inside.expression.display.name");
+ }
+
+ @NotNull
+ @Override
+ protected String buildErrorString(Object... infos) {
+ return InspectionGadgetsBundle.message("conditional.can.be.pushed.inside.expression.problem.descriptor");
+ }
+
+ @Nullable
+ @Override
+ public JComponent createOptionsPanel() {
+ return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("conditional.can.be.pushed.inside.expression.option"),
+ this, "ignoreSingleArgument");
+ }
+
+ @Nullable
+ @Override
+ protected InspectionGadgetsFix buildFix(Object... infos) {
+ return new PushConditionalInsideFix();
+ }
+
+ private static class PushConditionalInsideFix extends InspectionGadgetsFix {
+ @Nls
+ @NotNull
+ @Override
+ public String getFamilyName() {
+ return InspectionGadgetsBundle.message("conditional.can.be.pushed.inside.expression.quickfix");
+ }
+
+ @Override
+ protected void doFix(Project project, ProblemDescriptor descriptor) {
+ final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)descriptor.getPsiElement();
+ final PsiExpression thenExpression = conditionalExpression.getThenExpression();
+ if (thenExpression == null) {
+ return;
+ }
+ final EquivalenceChecker.Match match =
+ EquivalenceChecker.getCanonicalPsiEquivalence().expressionsMatch(thenExpression, conditionalExpression.getElseExpression());
+ if (!match.isPartialMatch()) {
+ return;
+ }
+ final PsiElement leftDiff = match.getLeftDiff();
+ final PsiElement rightDiff = match.getRightDiff();
+ final String expression = "(" + conditionalExpression.getCondition().getText() + " ? " +
+ leftDiff.getText() + " : " + rightDiff.getText() + ")";
+ final PsiExpression newConditionalExpression =
+ JavaPsiFacade.getElementFactory(project).createExpressionFromText(expression, conditionalExpression);
+ final PsiElement replacedConditionalExpression = leftDiff.replace(newConditionalExpression);
+ ParenthesesUtils.removeParentheses((PsiExpression)replacedConditionalExpression, false);
+ conditionalExpression.replace(thenExpression);
+ }
+ }
+
+ @Override
+ public BaseInspectionVisitor buildVisitor() {
+ return new ConditionalCanBePushedInsideExpressionVisitor();
+ }
+
+ private class ConditionalCanBePushedInsideExpressionVisitor extends BaseInspectionVisitor {
+
+ @Override
+ public void visitConditionalExpression(PsiConditionalExpression expression) {
+ super.visitConditionalExpression(expression);
+ final PsiExpression thenExpression = expression.getThenExpression();
+ if (thenExpression == null) {
+ return;
+ }
+ final PsiExpression elseExpression = expression.getElseExpression();
+ final EquivalenceChecker.Match match =
+ EquivalenceChecker.getCanonicalPsiEquivalence().expressionsMatch(thenExpression, elseExpression);
+ if (match.isExactMismatch() || match.isExactMatch()) {
+ return;
+ }
+ registerError(expression, ignoreSingleArgument && isOnlyArgumentOfMethodCall(match.getLeftDiff())
+ ? ProblemHighlightType.INFORMATION
+ : ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
+ }
+
+ private boolean isOnlyArgumentOfMethodCall(PsiElement element) {
+ if (element == null) {
+ return false;
+ }
+ final PsiElement parent = element.getParent();
+ if (!(parent instanceof PsiExpressionList)) {
+ return false;
+ }
+ final PsiExpressionList expressionList = (PsiExpressionList)parent;
+ if (expressionList.getExpressions().length != 1) {
+ return false;
+ }
+ final PsiElement grandParent = expressionList.getParent();
+ return grandParent instanceof PsiMethodCallExpression;
+ }
+ }
+}
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/ConditionalExpressionWithIdenticalBranchesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/ConditionalExpressionWithIdenticalBranchesInspection.java
index fa06c963926a..7c8c7ec237af 100644
--- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/ConditionalExpressionWithIdenticalBranchesInspection.java
+++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/ConditionalExpressionWithIdenticalBranchesInspection.java
@@ -17,7 +17,6 @@ package com.siyeh.ig.controlflow;
import com.intellij.codeInspection.CleanupLocalInspectionTool;
import com.intellij.codeInspection.ProblemDescriptor;
-import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
@@ -26,20 +25,9 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.EquivalenceChecker;
-import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import javax.swing.*;
public class ConditionalExpressionWithIdenticalBranchesInspection extends BaseInspection implements CleanupLocalInspectionTool {
- public boolean myReportOnlyExactlyIdentical;
-
- @Nullable
- @Override
- public JComponent createOptionsPanel() {
- return new SingleCheckboxOptionsPanel("Report only exactly identical branches", this, "myReportOnlyExactlyIdentical");
- }
@Override
@NotNull
@@ -50,70 +38,34 @@ public class ConditionalExpressionWithIdenticalBranchesInspection extends BaseIn
@Override
@NotNull
protected String buildErrorString(Object... infos) {
- final EquivalenceChecker.Match decision = (EquivalenceChecker.Match)infos[1];
- return InspectionGadgetsBundle.message(decision.isPartialMatch()
- ? "conditional.expression.with.similar.branches.problem.descriptor"
- : "conditional.expression.with.identical.branches.problem.descriptor");
+ return InspectionGadgetsBundle.message("conditional.expression.with.identical.branches.problem.descriptor");
}
@Override
public InspectionGadgetsFix buildFix(Object... infos) {
- return new CollapseConditional((PsiConditionalExpression)infos[0]);
+ return new CollapseConditionalFix();
}
- private static class CollapseConditional extends InspectionGadgetsFix {
- private final SmartPsiElementPointerFor example the following conditional expression: +
condition ? message("value: " + 1) : message("value: " + 2)
+Can be pushed inside and transformed into:
+ message("value: " + (condition ? 1 : 2))
+
++ New in 2017.2 + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ConditionalExpressionWithIdenticalBranches.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ConditionalExpressionWithIdenticalBranches.html index 84661085ecef..9ce40e84fa0f 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ConditionalExpressionWithIdenticalBranches.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ConditionalExpressionWithIdenticalBranches.html @@ -1,8 +1,7 @@
-Reports conditional expressions -with identical or similar "then" and "else" branches. Such expressions are almost certainly -programmer error. +Reports conditional expressions with identical then and else branches. +Such expressions are almost certainly a mistake.
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/conditional_can_be_pushed_inside_expression/ConditionalCanBePushedInsideExpression.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/conditional_can_be_pushed_inside_expression/ConditionalCanBePushedInsideExpression.java
new file mode 100644
index 000000000000..d866ee29c11c
--- /dev/null
+++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/conditional_can_be_pushed_inside_expression/ConditionalCanBePushedInsideExpression.java
@@ -0,0 +1,56 @@
+import java.util.Random;
+
+class ConditionalCanBePushedInsideExpression {
+
+ void fuzzy() {
+ String someString =