File tree Expand file tree Collapse file tree 2 files changed +32
-3
lines changed Expand file tree Collapse file tree 2 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -1261,8 +1261,8 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
1261
1261
}
1262
1262
return ;
1263
1263
}
1264
-
1265
- // Skip checking if there is no type, which presumably means there was a
1264
+
1265
+ // Skip checking if there is no type, which presumably means there was a
1266
1266
// type error.
1267
1267
if (!E->getType ()) {
1268
1268
return ;
@@ -1308,7 +1308,25 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
1308
1308
// dead?
1309
1309
if (E->getType ()->is <AnyFunctionType>()) {
1310
1310
bool isDiscardable = false ;
1311
- if (auto *Fn = dyn_cast<ApplyExpr>(E)) {
1311
+
1312
+ // The called function could be wrapped inside a `dot_syntax_call_expr`
1313
+ // node, for example:
1314
+ //
1315
+ // class Bar {
1316
+ // @discardableResult
1317
+ // func foo() -> Int { return 0 }
1318
+ //
1319
+ // func baz() {
1320
+ // self.foo
1321
+ // foo
1322
+ // }
1323
+ // }
1324
+ //
1325
+ // So look through the DSCE and get the function being called.
1326
+ auto expr =
1327
+ isa<DotSyntaxCallExpr>(E) ? cast<DotSyntaxCallExpr>(E)->getFn () : E;
1328
+
1329
+ if (auto *Fn = dyn_cast<ApplyExpr>(expr)) {
1312
1330
if (auto *declRef = dyn_cast<DeclRefExpr>(Fn->getFn ())) {
1313
1331
if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(declRef->getDecl ())) {
1314
1332
if (funcDecl->getAttrs ().hasAttribute <DiscardableResultAttr>()) {
Original file line number Diff line number Diff line change @@ -211,3 +211,14 @@ class Foo {
211
211
myOptionalFooProtocol? . returnSomething ( ) // okay
212
212
}
213
213
}
214
+
215
+ class Discard {
216
+ @discardableResult func bar( ) -> Int {
217
+ return 0
218
+ }
219
+
220
+ func baz( ) {
221
+ self . bar // expected-error {{expression resolves to an unused function}}
222
+ bar // expected-error {{expression resolves to an unused function}}
223
+ }
224
+ }
You can’t perform that action at this time.
0 commit comments