Skip to content
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

[SR-1681] spurious "unused result" warning in Swift 3 #44290

Closed
mattneub opened this issue Jun 4, 2016 · 20 comments
Closed

[SR-1681] spurious "unused result" warning in Swift 3 #44290

mattneub opened this issue Jun 4, 2016 · 20 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers

Comments

@mattneub
Copy link

mattneub commented Jun 4, 2016

Previous ID SR-1681
Radar None
Original Reporter @mattneub
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 7.3.1, Toolchain: Swift Development Snapshot 2016-05-31 (a)

Additional Detail from JIRA
Votes 3
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug
Assignee timbodeit (JIRA)
Priority Medium

md5: bba0c7a78fcf8a1425ed628000ec8cc7

is duplicated by:

relates to:

  • SR-1052 Implement SE-0047 - Defaulting non-Void functions so they warn on unused results
  • SR-3082 Throwing constructors with a method immediately called on them result in warning

Issue Description:

Consider the following code:

class ViewController: UIViewController {
    func test() {
        // no warning
        self.navigationController?.pushViewController(UIViewController(), animated:false)
        // warning
        self.navigationController?.popViewController(animated:false)
    }
}

The first line causes no warning. The second line causes a warning, "Expression of type 'UIViewController?' is unused."

Of course I understand why this is, in theory. The second call returns a value, and I am ignoring it. But from a practical point of view, to ignore this value is normal behavior, so the warning is just annoying (i.e. it isn't helpful). I can work around the problem by assigning the second line to an unnamed variable:

        _ = self.navigationController?.popViewController(animated:false)

But this seems like a bit much. I'm ending up with my code peppered with that sort of expression; it's kind of ugly.

@belkadan
Copy link
Contributor

belkadan commented Jun 4, 2016

@DougGregor or @milseman, I thought we were marking imported functions with @discardableResult? Is it the optional binding that's throwing that off?

@DougGregor
Copy link
Member

We are marking imported functions with `@discardableResult` by default. You're probably right about the optional binding.

@mattneub
Copy link
Author

mattneub commented Jun 7, 2016

@DougGregor try? might be another example. So, for instance:

try? AVAudioSession.sharedInstance().setActive(true)

That warns, but I'm not clear on whether it should. Again, it's not a problem, as I know how to silence the warning, but it seems unnecessary, as the whole point of try? is to say, Hey, if it works it works and if it doesn't it doesn't.

@mattneub
Copy link
Author

mattneub commented Jun 7, 2016

@DougGregor Here's one that doesn't involve any optional binding:

        var options = NSStringCompareOptions.caseInsensitiveSearch
        options.insert(.anchoredSearch) // warns

@belkadan
Copy link
Contributor

belkadan commented Jun 7, 2016

That latest is different; it's just a place where the standard library needs @discardableResult added. Can you file a separate bug for that?

I'm okay with try? because it's just translating the error (which you must handle) to an Optional (which you should handle). An extra step to say "and I'm ignoring it" seems fair.

@mattneub
Copy link
Author

mattneub commented Jun 7, 2016

@belkadan Excellent, thanks on both points. Perfectly reasonable, just checking. — Separate bug is SR-1695.

@swift-ci
Copy link
Collaborator

Comment by Eric (JIRA)

I think the default for @warn_unused_result was flipped, but I can't find much except for this: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/003527.html

I think we just need a way to annotate that it the ignored return value is intentional, either at the call site or the method definition. Maybe both.

EDIT: just noticed @discardableResult in the comments tab. Excellent!

@pcantrell
Copy link

I think this is a language bug, not a lib issue. Here’s a self-contained case to reproduce (as of XC 8b6):

extension Int {
    @discardableResult
    public func foo() -> Int { return self }

    @discardableResult
    public func bar() -> Int? { return self }
}

func foo() {
    let x: Int? = 3

    x?.bar() // incorrect warning

    x!.bar() // no warning
    x?.foo() // no warning
}

(Note that this is not a dup of SR-1929, which does indeed appear to be fixed.)

@swift-ci
Copy link
Collaborator

swift-ci commented Sep 4, 2016

Comment by Tim Bodeit (JIRA)

See #4625

Thanks @pcantrell, your example helped a lot.

@pcantrell
Copy link

timbodeit (JIRA User) Sure thing. I’m glad it was useful!

@swift-ci
Copy link
Collaborator

swift-ci commented Sep 9, 2016

Comment by Fabian Ehrentraud (JIRA)

This bug is still present in Xcode 8 GM, right?

@mattneub
Copy link
Author

mattneub commented Oct 5, 2016

I'd like to reopen this issue. It is not fixed in my copy of Xcode 8.

self.navigationController!.popViewController(animated:true)

does not warn, but

self.navigationController?.popViewController(animated:true)

does warn. I regard that as a bug.

@belkadan
Copy link
Contributor

belkadan commented Oct 5, 2016

The fix did not make it into Xcode 8 (or 8.1, which is mostly taking targeted fixes).

@mattneub
Copy link
Author

mattneub commented Oct 5, 2016

OK, thanks @belkadan.

@keith
Copy link
Collaborator

keith commented Oct 10, 2016

Should this fix be in Xcode 8.1 beta 3?

@swift-ci
Copy link
Collaborator

Comment by Tim Bodeit (JIRA)

@keith No, as @belkadan pointed out, this fix will not be a part of Xcode 8.1.
The GitHub page of the commit lists branches and tags containing this commit. So far it has only been included in development snapshots. As far as I can tell, Xcode and its betas are based on the `swift release` and `swift preview` tags. c97ebb0

@keith
Copy link
Collaborator

keith commented Oct 11, 2016

I saw that comment, but I was hoping to clarify whether or not fixes like this would make it in to subsequent Xcode betas.

@swift-ci
Copy link
Collaborator

Comment by Michael Taverne (JIRA)

I'm still seeing this issue in Xcode 8.2.1. Should it have been resolved in this version?

@belkadan
Copy link
Contributor

No, sorry, Xcode 8.2 only contained minor updates to Xcode 8.1, not a rebranch from master. It will be resolved in Xcode 8.3 with Swift 3.1.

@mattneub
Copy link
Author

@belkadan Thanks so much for clarifying. Truly appreciated.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

6 participants