RS.CLIPPY.MAP_ERR_IGNORE
`map_err` should not ignore the original error
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: map_err_ignore. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for instances of map_err(|_| Some::Enum)
Why restrict this?
This map_err throws away the original error rather than allowing the enum to
contain and report the cause of the error.
Example
Before:
use std::fmt;
#[derive(Debug)]
enum Error {
Indivisible,
Remainder(u8),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<\'_>) -> fmt::Result {
match self {
Error::Indivisible => write!(f, "could not divide input by three"),
Error::Remainder(remainder) => write!(
f,
"input is not divisible by three, remainder = {}",
remainder
),
}
}
}
impl std::error::Error for Error {}
fn divisible_by_3(input: &str) -> Result<(), Error> {
input
.parse::<i32>()
.map_err(|_| Error::Indivisible)
.map(|v| v % 3)
.and_then(|remainder| {
if remainder == 0 {
Ok(())
} else {
Err(Error::Remainder(remainder as u8))
}
})
}
After:
use std::{fmt, num::ParseIntError};
#[derive(Debug)]
enum Error {
Indivisible(ParseIntError),
Remainder(u8),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<\'_>) -> fmt::Result {
match self {
Error::Indivisible(_) => write!(f, "could not divide input by three"),
Error::Remainder(remainder) => write!(
f,
"input is not divisible by three, remainder = {}",
remainder
),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + \'static)> {
match self {
Error::Indivisible(source) => Some(source),
_ => None,
}
}
}
fn divisible_by_3(input: &str) -> Result<(), Error> {
input
.parse::<i32>()
.map_err(Error::Indivisible)
.map(|v| v % 3)
.and_then(|remainder| {
if remainder == 0 {
Ok(())
} else {
Err(Error::Remainder(remainder as u8))
}
})
}