Skip to content

An unused @discardableResult should not throw an error #6785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,9 +1082,22 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
// TODO: What about tuples which contain functions by-value that are
// dead?
if (E->getType()->is<AnyFunctionType>()) {
diagnose(E->getLoc(), diag::expression_unused_function)
.highlight(E->getSourceRange());
return;
bool isDiscardable = false;
if (auto *Fn = dyn_cast<ApplyExpr>(E)) {
if (auto *declRef = dyn_cast<DeclRefExpr>(Fn->getFn())) {
if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(declRef->getDecl())) {
if (funcDecl->getAttrs().hasAttribute<DiscardableResultAttr>()) {
isDiscardable = true;
}
}
}
}

if (!isDiscardable) {
diagnose(E->getLoc(), diag::expression_unused_function)
.highlight(E->getSourceRange());
return;
}
}

// If the result of this expression is of type "Never" or "()"
Expand Down
6 changes: 6 additions & 0 deletions test/attr/attr_discardableResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,9 @@ func testOptionalChaining(c1: C1?, s1: S1?) {
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