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

Suggest .err().expect() if .expect_err() is not implemented #65019

Open
lukaslueg opened this issue Oct 2, 2019 · 0 comments
Open

Suggest .err().expect() if .expect_err() is not implemented #65019

lukaslueg opened this issue Oct 2, 2019 · 0 comments
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lukaslueg
Copy link
Contributor

If T in Result<T, _> is not fmt::Debug, the following will fail to compile. The compiler rightfully blames fmt::Debug not being implemented for Result<Foo, ()>.

struct Foo;

Result::<Foo, _>::Err(()).expect_err("Foo!");

However, users may get stuck if they can't - for whatever reason - add fmt::Debug. A simple workaround is

Result::<Foo, _>::Err(()).err().expect("Foo!");

which has a somewhat degraded error message in the panic case, yet does what it's being asked for. Maybe the compiler could suggest that?

@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants