mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
+18
@@ -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) {
|
||||
|
||||
+22
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -14,4 +14,11 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement could be replaced with simple conditional expression</problem_class>
|
||||
<description><code>if</code> 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><code>if</code> could be replaced with conditional expression #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
Reference in New Issue
Block a user