mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 11:50:54 +07:00
IDEA-187474 Ternary condition is not boolean: add a quick-fix for method call
This commit is contained in:
@@ -1154,23 +1154,27 @@ public class HighlightUtil extends HighlightUtilBase {
|
||||
if (expr.getNextSibling() instanceof PsiErrorElement) return null;
|
||||
|
||||
if (!TypeConversionUtil.isBooleanType(type)) {
|
||||
final HighlightInfo info = createIncompatibleTypeHighlightInfo(PsiType.BOOLEAN, type, expr.getTextRange(), 0);
|
||||
if (expr instanceof PsiMethodCallExpression) {
|
||||
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expr;
|
||||
final PsiMethod method = methodCall.resolveMethod();
|
||||
if (method != null && PsiType.VOID.equals(method.getReturnType())) {
|
||||
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createMethodReturnFix(method, PsiType.BOOLEAN, true));
|
||||
}
|
||||
}
|
||||
else if (expr instanceof PsiAssignmentExpression && ((PsiAssignmentExpression)expr).getOperationTokenType() == JavaTokenType.EQ) {
|
||||
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAssignmentToComparisonFix((PsiAssignmentExpression)expr));
|
||||
}
|
||||
return info;
|
||||
return createMustBeBooleanInfo(expr, type);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static HighlightInfo createMustBeBooleanInfo(@NotNull PsiExpression expr, PsiType type) {
|
||||
final HighlightInfo info = createIncompatibleTypeHighlightInfo(PsiType.BOOLEAN, type, expr.getTextRange(), 0);
|
||||
if (expr instanceof PsiMethodCallExpression) {
|
||||
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expr;
|
||||
final PsiMethod method = methodCall.resolveMethod();
|
||||
if (method != null) {
|
||||
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createMethodReturnFix(method, PsiType.BOOLEAN, true));
|
||||
}
|
||||
}
|
||||
else if (expr instanceof PsiAssignmentExpression && ((PsiAssignmentExpression)expr).getOperationTokenType() == JavaTokenType.EQ) {
|
||||
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAssignmentToComparisonFix((PsiAssignmentExpression)expr));
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
static Set<PsiClassType> collectUnhandledExceptions(@NotNull final PsiTryStatement statement) {
|
||||
@@ -2401,7 +2405,7 @@ public class HighlightUtil extends HighlightUtilBase {
|
||||
static HighlightInfo checkTernaryOperatorConditionIsBoolean(@NotNull PsiExpression expression, PsiType type) {
|
||||
if (expression.getParent() instanceof PsiConditionalExpression &&
|
||||
((PsiConditionalExpression)expression.getParent()).getCondition() == expression && !TypeConversionUtil.isBooleanType(type)) {
|
||||
return createIncompatibleTypeHighlightInfo(PsiType.BOOLEAN, type, expression.getTextRange(), 0);
|
||||
return createMustBeBooleanInfo(expression, type);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Make 'foo' return 'boolean'" "true"
|
||||
class Test {
|
||||
void bar() {
|
||||
System.out.println(foo() ? "x" : "y");
|
||||
}
|
||||
|
||||
boolean foo() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Make 'foo' return 'boolean'" "true"
|
||||
class Test {
|
||||
void bar() {
|
||||
System.out.println(fo<caret>o() ? "x" : "y");
|
||||
}
|
||||
|
||||
int foo() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user