Skip to content

#71870 Respect @discardableResult when calling with try? #72380

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 28 additions & 9 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace {

TypeChecker::computeCaptures(CE);
return Action::SkipNode(E);
}
}

if (auto CapE = dyn_cast<CaptureListExpr>(E)) {
// Capture lists need to be reparented to enclosing autoclosures
Expand Down Expand Up @@ -1920,22 +1920,34 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
.highlight(E->getSourceRange());
return;
}

// Always complain about 'try?'.
if (auto *OTE = dyn_cast<OptionalTryExpr>(valueE)) {
DE.diagnose(OTE->getTryLoc(), diag::expression_unused_optional_try)
.highlight(E->getSourceRange());
return;
}

if (auto *LE = dyn_cast<LiteralExpr>(valueE)) {
diagnoseIgnoredLiteral(Context, LE);
return;
}

ApplyExpr *call = nullptr;

// Unwrap optional try to get inner function calls.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function call expr of try? foo() is wrapped in OptionalTryExpr and InjectIntoOptionalExpr. It need to unwrap them.

(optional_try_expr type="Int?" location=issue.swift:5:8 range=[issue.swift:5:3 - line:5:12] thrown_error="<null>"
  (inject_into_optional implicit type="Int?" location=issue.swift:5:8 range=[issue.swift:5:8 - line:5:12]
    (call_expr type="Int" location=issue.swift:5:8 range=[issue.swift:5:8 - line:5:12] isolation_crossing="none"
      (declref_expr type="() throws -> Int" location=issue.swift:5:8 range=[issue.swift:5:8 - line:5:8] decl="issue.(file).foo()@issue.swift:2:6" function_ref=single)
      (argument_list))))

{
auto nextExpr = valueE;
while(nextExpr != nullptr) {
if (auto applyExpr = dyn_cast<ApplyExpr>(nextExpr)) {
call = applyExpr;
break;
} else if (auto optionalTry = dyn_cast<OptionalTryExpr>(nextExpr)) {
nextExpr = optionalTry->getSubExpr();
} else if (auto implicitConversion = dyn_cast<InjectIntoOptionalExpr>(nextExpr)) {
nextExpr = implicitConversion->getSubExpr();
} else {
break;
}
}
}

// Check if we have a call to a function not marked with
// '@discardableResult'.
if (auto call = dyn_cast<ApplyExpr>(valueE)) {
if (call != nullptr) {
// Dig through all levels of calls.
Expr *fn = call->getFn();
while (true) {
Expand Down Expand Up @@ -1971,6 +1983,13 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
return;

// Otherwise, complain. Start with more specific diagnostics.

// Complain about 'try?'.
if (auto *OTE = dyn_cast<OptionalTryExpr>(valueE)) {
DE.diagnose(OTE->getTryLoc(), diag::expression_unused_optional_try)
.highlight(E->getSourceRange());
return;
}

// Diagnose unused constructor calls.
if (isa_and_nonnull<ConstructorDecl>(callee) && !call->isImplicit()) {
Expand Down
4 changes: 2 additions & 2 deletions test/Parse/omit_return.swift
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ var fv_optionalTryUnusedExplicit: () {
}

var fv_optionalTryUnusedImplicit: () {
try? failableLogAndReturn("oh") //expected-warning {{result of 'try?' is unused}}
try? failableLogAndReturn("oh")
}

var fv_optionalTryExplicit: String? {
Expand Down Expand Up @@ -1694,7 +1694,7 @@ class D_optionalTryUnusedExplicit {

class D_optionalTryUnusedImplicit {
deinit {
try? failableLogAndReturn("oh") // expected-warning {{result of 'try?' is unused}}
try? failableLogAndReturn("oh")
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/Parse/omit_return_ifdecl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ var fv_optionalTryUnusedExplicit: () {

var fv_optionalTryUnusedImplicit: () {
#if true
try? failableLogAndReturn("oh") //expected-warning {{result of 'try?' is unused}}
try? failableLogAndReturn("oh")
#endif
}

Expand Down Expand Up @@ -2414,7 +2414,7 @@ class D_optionalTryUnusedExplicit {
class D_optionalTryUnusedImplicit {
deinit {
#if true
try? failableLogAndReturn("oh") // expected-warning {{result of 'try?' is unused}}
try? failableLogAndReturn("oh")
#endif
}
}
Expand Down