IDEA-91079 (If statement could be replaced with conditional expression shall have an option to be ignored when a nested conditional expression exists.)

This commit is contained in:
Bas Leijdekkers
2012-09-05 21:48:42 +02:00
parent 317b69a819
commit 4965e5378f
3 changed files with 47 additions and 0 deletions
@@ -196,6 +196,16 @@ public class IfMayBeConditionalInspection extends BaseInspection {
if (!(elseStatement instanceof PsiReturnStatement)) {
return;
}
final PsiReturnStatement thenReturnStatement = (PsiReturnStatement)thenStatement;
final PsiExpression thenReturnValue = ParenthesesUtils.stripParentheses(thenReturnStatement.getReturnValue());
if (thenReturnValue instanceof PsiConditionalExpression) {
return;
}
final PsiReturnStatement elseReturnStatement = (PsiReturnStatement)elseStatement;
final PsiExpression elseReturnValue = ParenthesesUtils.stripParentheses(elseReturnStatement.getReturnValue());
if (elseReturnValue instanceof PsiConditionalExpression) {
return;
}
registerStatementError(statement);
}
else if (thenStatement instanceof PsiExpressionStatement) {
@@ -220,6 +230,14 @@ public class IfMayBeConditionalInspection extends BaseInspection {
if (!EquivalenceChecker.expressionsAreEquivalent(thenLhs, elseLhs)) {
return;
}
final PsiExpression thenRhs = ParenthesesUtils.stripParentheses(thenAssignmentExpression.getRExpression());
if (thenRhs instanceof PsiConditionalExpression) {
return;
}
final PsiExpression elseRhs = ParenthesesUtils.stripParentheses(elseAssignmentExpression.getRExpression());
if (elseRhs instanceof PsiConditionalExpression) {
return;
}
registerStatementError(statement);
}
else if (reportMethodCalls && thenExpression instanceof PsiMethodCallExpression) {
@@ -20,4 +20,26 @@ public class IfMayBeConditional {
sb.append("DescriptorUtils.getFQName(cd)",0, 1);
}
}
int foo4(int a, int b) {
if (a < b) return a;
else return b;
}
int foo5(int a, int b, int c) {
if (a < b) {
return a;
} else {
return b < c ? b : c;
}
}
void foo6(int a, int b, int c) {
int i;
if (a < b) {
i = a;
} else {
i = b < c ? b : c;
}
}
}
@@ -14,4 +14,11 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement could be replaced with simple conditional expression</problem_class>
<description>&lt;code&gt;if&lt;/code&gt; could be replaced with simple conditional expression #loc</description>
</problem>
<problem>
<file>IfMayBeConditional.java</file>
<line>25</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement could be replaced with conditional expression</problem_class>
<description>&lt;code&gt;if&lt;/code&gt; could be replaced with conditional expression #loc</description>
</problem>
</problems>