Skip to content

Suggest inspect_err instead of map_err with a closure that returns the original error #12250

Closed
@eric-seppanen

Description

@eric-seppanen

What it does

Suggest that map_err isn't a good way to inspect the error, and that inspect_err should be used instead.

Advantage

The resulting code is a little bit simpler, and doesn't imply things that aren't true ("map" implies we are changing the error).

Drawbacks

inspect_err was stabilized in 1.76.0. Other than that this seems pretty safe?

Example

let data = std::fs::read("some_file").map_err(|e| {
    println!("failed to read from file");
    e
})?;

Could be written as:

let data = std::fs::read("some_file").inspect_err(|e| {
    println!("failed to read from file");
})?;

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions