Skip to content

Commit

Permalink
update example to be more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
yaahc committed Sep 4, 2020
1 parent 2387f68 commit c31d473
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions clippy_lints/src/map_err_ignore.rs
Expand Up @@ -14,55 +14,88 @@ declare_clippy_lint! {
/// **Example:**
/// Before:
/// ```rust
/// use std::convert::TryFrom;
/// use std::fmt;
///
/// #[derive(Debug)]
/// enum Errors {
/// Ignored
/// 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
/// ),
/// }
/// }
/// }
///
/// fn divisible_by_3(inp: i32) -> Result<u32, Errors> {
/// let i = u32::try_from(inp).map_err(|_| Errors::Ignored)?;
/// impl std::error::Error for Error {}
///
/// Ok(i)
/// 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:
/// ```rust
/// use std::convert::TryFrom;
/// use std::num::TryFromIntError;
/// use std::fmt;
/// use std::error::Error;
/// use std::{fmt, num::ParseIntError};
///
/// #[derive(Debug)]
/// enum ParseError {
/// Indivisible {
/// source: TryFromIntError,
/// input: String,
/// }
/// enum Error {
/// Indivisible(ParseIntError),
/// Remainder(u8),
/// }
///
/// impl fmt::Display for ParseError {
/// impl fmt::Display for Error {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// match &self {
/// ParseError::Indivisible{source: _, input} => write!(f, "Error: {}", input)
/// 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 Error for ParseError {}
///
/// impl ParseError {
/// fn new(source: TryFromIntError, input: String) -> ParseError {
/// ParseError::Indivisible{source, input}
/// 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(inp: i32) -> Result<u32, ParseError> {
/// let i = u32::try_from(inp).map_err(|e| ParseError::new(e, e.to_string()))?;
///
/// Ok(i)
/// 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))
/// }
/// })
/// }
/// ```
pub MAP_ERR_IGNORE,
Expand Down

0 comments on commit c31d473

Please sign in to comment.