Skip to content

Commit a49353b

Browse files
committed
[Diagnostics] Add a tailored fix when ternary is associated with return
If it has been detected that there is an attempt to return a value from a function without result type, let's diagnose that specifically in cases when ternary operator is involved.
1 parent c3cc968 commit a49353b

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,6 +4224,12 @@ bool ConstraintSystem::repairFailures(
42244224
if (hasFixFor(loc, FixKind::ContextualMismatch))
42254225
return true;
42264226

4227+
if (contextualType->isVoid() &&
4228+
getContextualTypePurpose(anchor) == CTP_ReturnStmt) {
4229+
conversionsOrFixes.push_back(RemoveReturn::create(*this, lhs, loc));
4230+
break;
4231+
}
4232+
42274233
conversionsOrFixes.push_back(
42284234
ContextualMismatch::create(*this, lhs, rhs, loc));
42294235
break;

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func r17020197(_ x : Int?, y : Int) {
207207

208208
// <rdar://problem/20714480> QoI: Boolean expr not treated as Bool type when function return type is different
209209
func validateSaveButton(_ text: String) {
210-
return (text.count > 0) ? true : false // expected-error {{unexpected non-void return value in void function}}
210+
return (text.count > 0) ? true : false // expected-error {{unexpected non-void return value in void function}} expected-note {{did you mean to add a return type?}}
211211
}
212212

213213
// <rdar://problem/20201968> QoI: poor diagnostic when calling a class method via a metatype

0 commit comments

Comments
 (0)