Skip to content

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

Open
@lukaslueg

Description

@lukaslueg

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-result-optionArea: Result and Option combinatorsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions