Skip to content

[Sema] Fix SR-1681 spurious "unused result" warning #4625

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 2 commits into from
Sep 6, 2016
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
5 changes: 4 additions & 1 deletion lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,12 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
// If we have an OptionalEvaluationExpr at the top level, then someone is
// "optional chaining" and ignoring the result. Produce a diagnostic if it
// doesn't make sense to ignore it.
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(valueE))
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(valueE)) {
if (auto *IIO = dyn_cast<InjectIntoOptionalExpr>(OEE->getSubExpr()))
return checkIgnoredExpr(IIO->getSubExpr());
if (auto *C = dyn_cast<CallExpr>(OEE->getSubExpr()))
return checkIgnoredExpr(C);
}

// Check if we have a call to a function not marked with
// '@discardableResult'.
Expand Down
38 changes: 38 additions & 0 deletions test/attr/attr_discardableResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class C1 {

func f2() -> Int { }

@discardableResult
func f1Optional() -> Int? { }

func f2Optional() -> Int? { }

@discardableResult
func me() -> Self { return self }

Expand All @@ -68,6 +73,10 @@ func testFunctionsInClass(c1 : C1, c2: C2) {
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

Expand All @@ -91,6 +100,11 @@ struct S1 {
func f1() -> Int { }

func f2() -> Int { }

@discardableResult
func f1Optional() -> Int? { }

func f2Optional() -> Int? { }
}

func testFunctionsInStruct(s1 : S1) {
Expand All @@ -106,6 +120,10 @@ func testFunctionsInStruct(s1 : S1) {
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 {
Expand Down Expand Up @@ -138,3 +156,23 @@ class X {
@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}}
}