Compiler should suggest expect/unwrap before the suggestion to remove the last method call #117669
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Current output
Desired output
Rationale and extra context
#105872 in Dec '22 introduced a cool new feature to suggest removing the last method call if it would alleviate a type mismatch error. This is particularly using for people wrestling with
my_str.to_string()
issues.#116841 in Oct '23 introduced another useful feature to suggest to users that they might consider
expect
ing anOption
orResult
type in order to solve a type mismatch. Also very cool, and conveniently by the same author @chenyukang.Unfortunately, in cases where a method
T -> Option<T>
is called, the previous check takes precedence, and the compiler suggests removing that trailing method to get the code to compile. This isn't generally that useful as the expect suggestion, and I can't think of an example where it's a better one. If you can think of one though, please put it below for me to test!#116738 surfaced this with
Path.parent()
but it's more easily demonstrated with ints.I can see two ways to fix this:
Result
andOption
typesexpect
check has to be one of the firstI am going to work on solving it via method 2, as it feels cleaner that the method check doesn't have to know about the
Option
andResult
check.If anyone has strong feelings on this, please let me know!
Playground here
Other cases
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: