Skip to content

Commit c31d473

Browse files
committed
update example to be more idiomatic
1 parent 2387f68 commit c31d473

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

clippy_lints/src/map_err_ignore.rs

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,88 @@ declare_clippy_lint! {
1414
/// **Example:**
1515
/// Before:
1616
/// ```rust
17-
/// use std::convert::TryFrom;
17+
/// use std::fmt;
1818
///
1919
/// #[derive(Debug)]
20-
/// enum Errors {
21-
/// Ignored
20+
/// enum Error {
21+
/// Indivisible,
22+
/// Remainder(u8),
23+
/// }
24+
///
25+
/// impl fmt::Display for Error {
26+
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
/// match self {
28+
/// Error::Indivisible => write!(f, "could not divide input by three"),
29+
/// Error::Remainder(remainder) => write!(
30+
/// f,
31+
/// "input is not divisible by three, remainder = {}",
32+
/// remainder
33+
/// ),
34+
/// }
35+
/// }
2236
/// }
2337
///
24-
/// fn divisible_by_3(inp: i32) -> Result<u32, Errors> {
25-
/// let i = u32::try_from(inp).map_err(|_| Errors::Ignored)?;
38+
/// impl std::error::Error for Error {}
2639
///
27-
/// Ok(i)
40+
/// fn divisible_by_3(input: &str) -> Result<(), Error> {
41+
/// input
42+
/// .parse::<i32>()
43+
/// .map_err(|_| Error::Indivisible)
44+
/// .map(|v| v % 3)
45+
/// .and_then(|remainder| {
46+
/// if remainder == 0 {
47+
/// Ok(())
48+
/// } else {
49+
/// Err(Error::Remainder(remainder as u8))
50+
/// }
51+
/// })
2852
/// }
2953
/// ```
3054
///
3155
/// After:
3256
/// ```rust
33-
/// use std::convert::TryFrom;
34-
/// use std::num::TryFromIntError;
35-
/// use std::fmt;
36-
/// use std::error::Error;
57+
/// use std::{fmt, num::ParseIntError};
3758
///
3859
/// #[derive(Debug)]
39-
/// enum ParseError {
40-
/// Indivisible {
41-
/// source: TryFromIntError,
42-
/// input: String,
43-
/// }
60+
/// enum Error {
61+
/// Indivisible(ParseIntError),
62+
/// Remainder(u8),
4463
/// }
4564
///
46-
/// impl fmt::Display for ParseError {
65+
/// impl fmt::Display for Error {
4766
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48-
/// match &self {
49-
/// ParseError::Indivisible{source: _, input} => write!(f, "Error: {}", input)
67+
/// match self {
68+
/// Error::Indivisible(_) => write!(f, "could not divide input by three"),
69+
/// Error::Remainder(remainder) => write!(
70+
/// f,
71+
/// "input is not divisible by three, remainder = {}",
72+
/// remainder
73+
/// ),
5074
/// }
5175
/// }
5276
/// }
5377
///
54-
/// impl Error for ParseError {}
55-
///
56-
/// impl ParseError {
57-
/// fn new(source: TryFromIntError, input: String) -> ParseError {
58-
/// ParseError::Indivisible{source, input}
78+
/// impl std::error::Error for Error {
79+
/// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
80+
/// match self {
81+
/// Error::Indivisible(source) => Some(source),
82+
/// _ => None,
83+
/// }
5984
/// }
6085
/// }
6186
///
62-
/// fn divisible_by_3(inp: i32) -> Result<u32, ParseError> {
63-
/// let i = u32::try_from(inp).map_err(|e| ParseError::new(e, e.to_string()))?;
64-
///
65-
/// Ok(i)
87+
/// fn divisible_by_3(input: &str) -> Result<(), Error> {
88+
/// input
89+
/// .parse::<i32>()
90+
/// .map_err(Error::Indivisible)
91+
/// .map(|v| v % 3)
92+
/// .and_then(|remainder| {
93+
/// if remainder == 0 {
94+
/// Ok(())
95+
/// } else {
96+
/// Err(Error::Remainder(remainder as u8))
97+
/// }
98+
/// })
6699
/// }
67100
/// ```
68101
pub MAP_ERR_IGNORE,

0 commit comments

Comments
 (0)