-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathattr_discardableResult.swift
244 lines (181 loc) · 6.2 KB
/
attr_discardableResult.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// RUN: %target-typecheck-verify-swift -enable-objc-interop
// ---------------------------------------------------------------------------
// Mark function's return value as discardable and silence warning
// ---------------------------------------------------------------------------
@discardableResult
func f1() -> [Int] { }
func f2() -> [Int] { }
func f3() { }
func f4<R>(blah: () -> R) -> R { return blah() }
func testGlobalFunctions() -> [Int] {
f1() // okay
f2() // expected-warning {{result of call to 'f2()' is unused}}
_ = f2() // okay
f3() // okay
f4 { 5 } // expected-warning {{result of call to 'f4(blah:)' is unused}}
f4 { } // okay
return f2() // okay
}
attr_discardableResult.f1()
attr_discardableResult.f2() // expected-warning {{result of call to 'f2()' is unused}}
class C1 {
@discardableResult
static func f1Static() -> Int { }
static func f2Static() -> Int { }
@discardableResult
class func f1Class() -> Int { }
class func f2Class() -> Int { }
@discardableResult init() { }
init(foo: Int) { }
@discardableResult
func f1() -> Int { }
func f2() -> Int { }
@discardableResult
func f1Optional() -> Int? { }
func f2Optional() -> Int? { }
@discardableResult
func me() -> Self { return self }
func reallyMe() -> Self { return self }
}
class C2 : C1 {}
func testFunctionsInClass(c1 : C1, c2: C2) {
C1.f1Static() // okay
C1.f2Static() // expected-warning {{result of call to 'f2Static()' is unused}}
_ = C1.f2Static() // okay
C1.f1Class() // okay
C1.f2Class() // expected-warning {{result of call to 'f2Class()' is unused}}
_ = C1.f2Class() // okay
C1() // okay, marked @discardableResult
_ = C1() // okay
C1(foo: 5) // expected-warning {{result of 'C1' initializer is unused}}
_ = C1(foo: 5) // okay
c1.f1() // okay
c1.f2() // expected-warning {{result of call to 'f2()' is unused}}
_ = c1.f2() // okay
c1.f1Optional() // okay
c1.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
_ = c1.f2Optional() // okay
c1.me() // okay
c2.me() // okay
c1.reallyMe() // expected-warning {{result of call to 'reallyMe()' is unused}}
c2.reallyMe() // expected-warning {{result of call to 'reallyMe()' is unused}}
_ = c1.reallyMe() // okay
_ = c2.reallyMe() // okay
}
struct S1 {
@discardableResult
static func f1Static() -> Int { }
static func f2Static() -> Int { }
@discardableResult init() { }
init(foo: Int) { }
@discardableResult
func f1() -> Int { }
func f2() -> Int { }
@discardableResult
func f1Optional() -> Int? { }
func f2Optional() -> Int? { }
}
func testFunctionsInStruct(s1 : S1) {
S1.f1Static() // okay
S1.f2Static() // expected-warning {{result of call to 'f2Static()' is unused}}
_ = S1.f2Static() // okay
S1() // okay, marked @discardableResult
_ = S1() // okay
S1(foo: 5) // expected-warning {{result of 'S1' initializer is unused}}
_ = S1(foo: 5) // okay
s1.f1() // okay
s1.f2() // expected-warning {{result of call to 'f2()' is unused}}
_ = s1.f2() // okay
s1.f1Optional() // okay
s1.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
_ = s1.f2Optional() // okay
}
protocol P1 {
@discardableResult
func me() -> Self
func reallyMe() -> Self
}
func testFunctionsInExistential(p1 : P1) {
p1.me() // okay
p1.reallyMe() // expected-warning {{result of call to 'reallyMe()' is unused}}
_ = p1.reallyMe() // okay
}
let x = 4
"Hello \(x+1) world" // expected-warning {{string literal is unused}}
func f(a : () -> Int) {
42 // expected-warning {{integer literal is unused}}
4 + 5 // expected-warning {{result of operator '+' is unused}}
a() // expected-warning {{result of call to function returning 'Int' is unused}}
}
@warn_unused_result func g() -> Int { } // expected-warning {{'warn_unused_result' attribute behavior is now the default}} {{1-21=}}
class X {
@warn_unused_result // expected-warning {{'warn_unused_result' attribute behavior is now the default}} {{3-23=}}
@objc
func h() -> Int { }
}
func testOptionalChaining(c1: C1?, s1: S1?) {
c1?.f1() // okay
c1!.f1() // okay
c1?.f1Optional() // okay
c1!.f1Optional() // okay
c1?.f2() // expected-warning {{result of call to 'f2()' is unused}}
c1!.f2() // expected-warning {{result of call to 'f2()' is unused}}
c1?.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
c1!.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
s1?.f1() // okay
s1!.f1() // okay
s1?.f1Optional() // okay
s1!.f1Optional() // okay
s1?.f2() // expected-warning {{result of call to 'f2()' is unused}}
s1!.f2() // expected-warning {{result of call to 'f2()' is unused}}
s1?.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
s1!.f2Optional() // expected-warning {{result of call to 'f2Optional()' is unused}}
}
@discardableResult func SR2948 (_ closure: @escaping ()->()) -> (()->()) {
closure()
return closure
}
SR2948({}) // okay
class SR7562_A {
@discardableResult required init(input: Int) { }
}
class SR7562_B : SR7562_A {}
SR7562_A(input: 10) // okay
SR7562_B(input: 10) // okay
protocol FooProtocol {}
extension FooProtocol {
@discardableResult
static func returnSomething() -> Bool? {
return true
}
}
class Foo {
var myOptionalFooProtocol: FooProtocol.Type?
func doSomething() {
myOptionalFooProtocol?.returnSomething() // okay
}
}
class Discard {
@discardableResult func bar() -> Int {
return 0
}
func baz() {
self.bar // expected-error {{function is unused}}
bar // expected-error {{function is unused}}
}
}
// SR-12271
struct SR_12271_S {
@discardableResult
func bar1() -> () -> Void {
return {}
}
@discardableResult
static func bar2() -> () -> Void {
return {}
}
}
SR_12271_S().bar1() // Okay
SR_12271_S.bar2() // Okay
SR_12271_S().bar1 // expected-error {{function is unused}}
SR_12271_S.bar2 // expected-error {{function is unused}}