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 inspect_err instead of map_err with a closure that returns the original error #12250

Closed
eric-seppanen opened this issue Feb 9, 2024 · 1 comment · Fixed by #12287
Closed
Labels
A-lint Area: New lints

Comments

@eric-seppanen
Copy link

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");
})?;
@eric-seppanen eric-seppanen added the A-lint Area: New lints label Feb 9, 2024
@eric-seppanen eric-seppanen changed the title Suggest inspect_err instead of map_err with a closure that returns Suggest inspect_err instead of map_err with a closure that returns the original error Feb 9, 2024
@y21 y21 self-assigned this Feb 12, 2024
@y21
Copy link
Member

y21 commented Feb 12, 2024

Looks like I was too slow :) #12287

@y21 y21 removed their assignment Feb 12, 2024
bors added a commit that referenced this issue Jun 15, 2024
Add lint `manual_inspect`

fixes #12250

A great example of a lint that sounds super simple, but has a pile of edge cases.

changelog: Add lint `manual_inspect`
bors added a commit that referenced this issue Jun 16, 2024
Add lint `manual_inspect`

fixes #12250

A great example of a lint that sounds super simple, but has a pile of edge cases.

----

changelog: Add lint `manual_inspect`
@bors bors closed this as completed in 16efa56 Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants