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-3082] Throwing constructors with a method immediately called on them result in warning #45672
Comments
This is expected behavior. @rjmccall, anything to add? |
So it looks like this problem can be simplified to this, then: struct Throwing {
@discardableResult
func method() throws -> String {
return "test"
}
}
let throwing = Throwing()
try? throwing.method() // Warning: Result of 'try?' is unused I still have an issue with it, which is that since `method()` is marked as having a discardableResult, `try?` should inherit that behavior. Note that regular `try` does appropriately inherit the behavior: let throwing = Throwing()
do {
try throwing.method() // No warning
} catch _ {
} |
This bug looks related: https://bugs.swift.org/browse/SR-1681 |
The result you're allowed to discard is the result of |
(@mattneub and I discussed |
I agree with Soroush that the result of try? should be understood to be ignorable if the operand is. People do use try? idiomatically to ignore errors, and I think that has to be okay; specifically, if we weren't okay with it, we shouldn't have added try? at all. |
Comment by Tim Bodeit (JIRA) Im with @belkadan on regarding ignoring the error and discarding a potential result as two different things. @@belkadan: Your reasoning in the comments of SR-3082 and SR-1681 matches your implementation of the unused `try?` warnings in bd031d. Since then this logic has been changed in regards to Void methods when SR-1752 was fixed in b956dc. I have only glanced over the swift-evolution Draft Tuple-Based Compound Optional Binding thread referred to in the comments of PR#3057. However, when taking the above mentioned aspects into account, I do not think it gives a reason for the change to the warnings on unused results of `try?` expressions. If no action is to be taken on this issue, I think we should reintroduce the warnings in regards to void expressions. Speaking in test behavior, this would mean reversing the changes to `test/Parse/try.swift` in b956dc. Either way (addressing this issue for @discardableResult or reintroducing warnings for void): I'd be happy to write a PR for this. cc @ahoppen: What is your opinion on this? |
I'm in no position to give a fully qualified opinion on this but since you directly ask me, I'm currently in the camp to say that no warning should be issued in the above code. |
Reduced code: @discardableResult
func discardMe() throws -> Int { 0 }
try discardMe() // no warning
try! discardMe() // no warning
try? discardMe() // warning: result of 'try?' is unused
func returnsVoid() throws {}
try returnsVoid() // no warning
try! returnsVoid() // no warning
try? returnsVoid() // no warning This behavior has not changed still in Swift 5.3. |
Hopefully, this can be fixed soon |
Environment
Swift 3, Xcode 8.1
Additional Detail from JIRA
md5: 2b4e024ed59312e8b6a1f66407e28d1e
relates to:
Issue Description:
A throwing constructor that has a method with a `discardableResult` immediately called on it results in a warning:
This issue can't be reproduced in a playground, I think because playgrounds don't have unused result errors turned on.
The warning can be silenced by using `_ =`. It can also be silenced with parentheses: `(try? ThrowingConstructor())?.method()`
The text was updated successfully, but these errors were encountered: