Error on 'if' without an 'else' branch when used as an expression
This commit is contained in:
@@ -610,6 +610,8 @@ public interface Errors {
|
||||
|
||||
DiagnosticFactory0<JetElement> SENSELESS_NULL_IN_WHEN = DiagnosticFactory0.create(WARNING);
|
||||
|
||||
DiagnosticFactory0<JetIfExpression> INVALID_IF_AS_EXPRESSION = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
// Nullability
|
||||
|
||||
DiagnosticFactory1<PsiElement, JetType> UNSAFE_CALL = DiagnosticFactory1.create(ERROR);
|
||||
|
||||
+2
@@ -537,6 +537,8 @@ public class DefaultErrorMessages {
|
||||
MAP.put(SENSELESS_COMPARISON, "Condition ''{0}'' is always ''{1}''", ELEMENT_TEXT, TO_STRING);
|
||||
MAP.put(SENSELESS_NULL_IN_WHEN, "Expression under 'when' is never equal to null");
|
||||
|
||||
MAP.put(INVALID_IF_AS_EXPRESSION, "'if' must have both main and 'else' branches if used as an expression");
|
||||
|
||||
MAP.put(OVERRIDING_FINAL_MEMBER, "''{0}'' in ''{1}'' is final and cannot be overridden", NAME, NAME);
|
||||
MAP.put(CANNOT_WEAKEN_ACCESS_PRIVILEGE, "Cannot weaken access privilege ''{0}'' for ''{1}'' in ''{2}''", TO_STRING, NAME, NAME);
|
||||
MAP.put(CANNOT_CHANGE_ACCESS_PRIVILEGE, "Cannot change access privilege ''{0}'' for ''{1}'' in ''{2}''", TO_STRING, NAME, NAME);
|
||||
|
||||
@@ -504,8 +504,11 @@ public class CandidateResolver(
|
||||
private fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.shouldContinue() =
|
||||
candidateResolveMode == CandidateResolveMode.FULLY || candidateCall.getStatus().possibleTransformToSuccess()
|
||||
|
||||
private inline fun <D : CallableDescriptor> CallCandidateResolutionContext<D>
|
||||
.check(checker: CallCandidateResolutionContext<D>.() -> Unit): Unit = if (shouldContinue()) checker()
|
||||
private inline fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.check(
|
||||
checker: CallCandidateResolutionContext<D>.() -> Unit
|
||||
) {
|
||||
if (shouldContinue()) checker()
|
||||
}
|
||||
|
||||
private inline fun <D : CallableDescriptor> CallCandidateResolutionContext<D>.
|
||||
checkAndReport(checker: CallCandidateResolutionContext<D>.() -> ResolutionStatus) {
|
||||
|
||||
@@ -256,10 +256,21 @@ public class DataFlowAnalyzer {
|
||||
|
||||
@Nullable
|
||||
public JetType checkImplicitCast(@Nullable JetType expressionType, @NotNull JetExpression expression, @NotNull ResolutionContext context, boolean isStatement) {
|
||||
if (expressionType != null && context.expectedType == NO_EXPECTED_TYPE && context.contextDependency == INDEPENDENT && !isStatement
|
||||
boolean isIfExpression = expression instanceof JetIfExpression;
|
||||
if (expressionType != null && (context.expectedType == NO_EXPECTED_TYPE || isIfExpression)
|
||||
&& context.contextDependency == INDEPENDENT && !isStatement
|
||||
&& (KotlinBuiltIns.isUnit(expressionType) || KotlinBuiltIns.isAnyOrNullableAny(expressionType))
|
||||
&& !DynamicTypesKt.isDynamic(expressionType)) {
|
||||
context.trace.report(IMPLICIT_CAST_TO_UNIT_OR_ANY.on(expression, expressionType));
|
||||
if (isIfExpression && KotlinBuiltIns.isUnit(expressionType)) {
|
||||
JetIfExpression ifExpression = (JetIfExpression) expression;
|
||||
if (ifExpression.getThen() == null || ifExpression.getElse() == null) {
|
||||
context.trace.report(INVALID_IF_AS_EXPRESSION.on((JetIfExpression) expression));
|
||||
return expressionType;
|
||||
}
|
||||
}
|
||||
else {
|
||||
context.trace.report(IMPLICIT_CAST_TO_UNIT_OR_ANY.on(expression, expressionType));
|
||||
}
|
||||
}
|
||||
return expressionType;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user