Skip to content

Commit a262532

Browse files
authored
Merge pull request #4625 from timbodeit/SR-1681
[Sema] Fix SR-1681 spurious "unused result" warning
2 parents 1ac5919 + 7c0f9ae commit a262532

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,9 +1080,12 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
10801080
// If we have an OptionalEvaluationExpr at the top level, then someone is
10811081
// "optional chaining" and ignoring the result. Produce a diagnostic if it
10821082
// doesn't make sense to ignore it.
1083-
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(valueE))
1083+
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(valueE)) {
10841084
if (auto *IIO = dyn_cast<InjectIntoOptionalExpr>(OEE->getSubExpr()))
10851085
return checkIgnoredExpr(IIO->getSubExpr());
1086+
if (auto *C = dyn_cast<CallExpr>(OEE->getSubExpr()))
1087+
return checkIgnoredExpr(C);
1088+
}
10861089

10871090
// Check if we have a call to a function not marked with
10881091
// '@discardableResult'.

test/attr/attr_discardableResult.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class C1 {
4242

4343
func f2() -> Int { }
4444

45+
@discardableResult
46+
func f1Optional() -> Int? { }
47+
48+
func f2Optional() -> Int? { }
49+
4550
@discardableResult
4651
func me() -> Self { return self }
4752

@@ -68,6 +73,10 @@ func testFunctionsInClass(c1 : C1, c2: C2) {
6873
c1.f2() // expected-warning {{result of call to 'f2()' is unused}}
6974
_ = c1.f2() // okay
7075

76+
c1.f1Optional() // okay
77+
c1.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
78+
_ = c1.f2Optional() // okay
79+
7180
c1.me() // okay
7281
c2.me() // okay
7382

@@ -91,6 +100,11 @@ struct S1 {
91100
func f1() -> Int { }
92101

93102
func f2() -> Int { }
103+
104+
@discardableResult
105+
func f1Optional() -> Int? { }
106+
107+
func f2Optional() -> Int? { }
94108
}
95109

96110
func testFunctionsInStruct(s1 : S1) {
@@ -106,6 +120,10 @@ func testFunctionsInStruct(s1 : S1) {
106120
s1.f1() // okay
107121
s1.f2() // expected-warning {{result of call to 'f2()' is unused}}
108122
_ = s1.f2() // okay
123+
124+
s1.f1Optional() // okay
125+
s1.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
126+
_ = s1.f2Optional() // okay
109127
}
110128

111129
protocol P1 {
@@ -138,3 +156,23 @@ class X {
138156
@objc
139157
func h() -> Int { }
140158
}
159+
160+
func testOptionalChaining(c1: C1?, s1: S1?) {
161+
c1?.f1() // okay
162+
c1!.f1() // okay
163+
c1?.f1Optional() // okay
164+
c1!.f1Optional() // okay
165+
c1?.f2() // expected-warning {{result of call to 'f2()' is unused}}
166+
c1!.f2() // expected-warning {{result of call to 'f2()' is unused}}
167+
c1?.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
168+
c1!.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
169+
170+
s1?.f1() // okay
171+
s1!.f1() // okay
172+
s1?.f1Optional() // okay
173+
s1!.f1Optional() // okay
174+
s1?.f2() // expected-warning {{result of call to 'f2()' is unused}}
175+
s1!.f2() // expected-warning {{result of call to 'f2()' is unused}}
176+
s1?.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
177+
s1!.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
178+
}

0 commit comments

Comments
 (0)