Skip to content

Commit b956dcd

Browse files
committed
[SR-1752] Fix warning about unused result if return type is Void?
1 parent 817b8e3 commit b956dcd

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,9 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
10411041
return;
10421042
}
10431043

1044-
// If the result of this expression is of type "()", then it is safe to
1045-
// ignore.
1046-
if (valueE->getType()->isVoid() ||
1044+
// If the result of this expression is of type "()" potentially wrapped in
1045+
// optionals, then it is safe to ignore.
1046+
if (valueE->getType()->lookThroughAllAnyOptionalTypes()->isVoid() ||
10471047
valueE->getType()->is<ErrorType>())
10481048
return;
10491049

test/Constraints/diagnostics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,3 +822,12 @@ func rdar24202058(a : Int) {
822822
return a <= 480 // expected-error {{'<=' produces 'Bool', not the expected contextual result type '()'}}
823823
}
824824

825+
// SR-1752: Warning about unused result with ternary operator
826+
827+
struct SR1752 {
828+
func foo() {}
829+
}
830+
831+
let sr1752: SR1752? = nil
832+
833+
true ? nil : sr1752?.foo() // don't generate a warning about unused result since foo returns Void

test/Parse/try.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ let _: String = doubleOptional // expected-error {{cannot convert value of type
110110
func maybeThrow() throws {}
111111
try maybeThrow() // okay
112112
try! maybeThrow() // okay
113-
try? maybeThrow() // expected-warning {{result of 'try?' is unused}}
113+
try? maybeThrow() // okay since return type of maybeThrow is Void
114114
_ = try? maybeThrow() // okay
115115

116116
let _: () -> Void = { try! maybeThrow() } // okay
117-
let _: () -> Void = { try? maybeThrow() } // expected-warning {{result of 'try?' is unused}}
117+
let _: () -> Void = { try? maybeThrow() } // okay since return type of maybeThrow is Void
118118

119119

120120
if try? maybeThrow() { // expected-error {{cannot be used as a boolean}} {{4-4=((}} {{21-21=) != nil)}}

0 commit comments

Comments
 (0)