Skip to content

Commit 7c0f9ae

Browse files
committed
[test] Add test cases for SR-1681 - spurious "unused result" warning
1 parent c97ebb0 commit 7c0f9ae

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

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)