From e8e6a02ab12d4cf9ede0b6a62acdf2fa961c6eda Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 15 Jun 2016 15:17:28 +0200 Subject: [PATCH] Fix Clippy warnings --- src/fmtstr.rs | 37 +++++++++++++++++-------------------- src/formatter.rs | 12 ++---------- src/lib.rs | 2 +- src/types.rs | 21 ++++++++++----------- 4 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/fmtstr.rs b/src/fmtstr.rs index 58c79cd..db16e93 100644 --- a/src/fmtstr.rs +++ b/src/fmtstr.rs @@ -21,7 +21,7 @@ fn test_write_char() { assert!(s == "h fff"); } -fn write_from<'a, I>(fmt: &mut Formatter, f: I, n: usize) -> usize +fn write_from(fmt: &mut Formatter, f: I, n: usize) -> usize where I: Iterator { // eexaust f or run out of n, return chars written @@ -91,7 +91,7 @@ impl<'a, 'b> Formatter<'a, 'b> { /// This gives a moderate performance boost. /// This isn't exactly unsafe, it just ends up ignoring extranious format /// specifiers - // For example, {x:<-#10} should technically be formatting an int, but ignoring the + /// For example, {x:<-#10} should technically be formatting an int, but ignoring the /// integer specific formatting is probably not the end of the world /// This can also be used by the `u64` etc methods to finish their formatting while /// still using the str formatter for width and alignment @@ -111,27 +111,24 @@ impl<'a, 'b> Formatter<'a, 'b> { let mut chars = s.chars(); let mut pad: usize = 0; - match width { - Some(mut width) => { - if width > len { - let align = self.align(); - match align { - Alignment::Left => pad = width - len, - Alignment::Center => { - width = width - len; - pad = width / 2; - write_char(self, fill, pad); - pad += width % 2; - } - Alignment::Right => { - write_char(self, fill, width - len); - } - Alignment::Equal => return Err(FmtError::Invalid( - "sign aware zero padding and Align '=' not yet supported".to_string())), + if let Some(mut width) = width { + if width > len { + let align = self.align(); + match align { + Alignment::Left => pad = width - len, + Alignment::Center => { + width -= len; + pad = width / 2; + write_char(self, fill, pad); + pad += width % 2; } + Alignment::Right => { + write_char(self, fill, width - len); + } + Alignment::Equal => return Err(FmtError::Invalid( + "sign aware zero padding and Align '=' not yet supported".to_string())), } } - None => {} } write_from(self, &mut chars, len); write_char(self, fill, pad); diff --git a/src/formatter.rs b/src/formatter.rs index 7ac9c4c..7a0c425 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -364,21 +364,13 @@ impl<'a, 'b> Formatter<'a, 'b> { /// sign plus getter /// here because it is in fmt::Formatter pub fn sign_plus(&self) -> bool { - if self.sign == Sign::Plus { - true - } else { - false - } + self.sign == Sign::Plus } /// sign minus getter /// here because it is in fmt::Formatter pub fn sign_minus(&self) -> bool { - if self.sign == Sign::Plus { - true - } else { - false - } + self.sign == Sign::Minus } /// alternate getter diff --git a/src/lib.rs b/src/lib.rs index be2b79e..d169046 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ pub use fmtstr::strfmt_map; pub use formatter::Formatter; -/// rust-style format a string given a HashMap of the variables +/// Rust-style format a string given a `HashMap` of the variables. pub fn strfmt(fmtstr: &str, vars: &HashMap) -> Result { let formatter = |mut fmt: Formatter| { let v = match vars.get(fmt.key) { diff --git a/src/types.rs b/src/types.rs index c055ad1..9fa43bb 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,5 @@ use std::fmt; use std::error; -use std::fmt::Write; use std::string::String; use std::result; @@ -23,8 +22,8 @@ pub enum Sign { impl Sign { pub fn is_unspecified(&self) -> bool { - match self { - &Sign::Unspecified => false, + match *self { + Sign::Unspecified => false, _ => true, } } @@ -42,20 +41,20 @@ pub enum FmtError { impl fmt::Display for FmtError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &FmtError::Invalid(ref s) => write!(f, "Invalid({})", s), - &FmtError::KeyError(ref s) => write!(f, "KeyError({})", s), - &FmtError::TypeError(ref s) => write!(f, "TypeError({})", s), + match *self { + FmtError::Invalid(ref s) => write!(f, "Invalid({})", s), + FmtError::KeyError(ref s) => write!(f, "KeyError({})", s), + FmtError::TypeError(ref s) => write!(f, "TypeError({})", s), } } } impl error::Error for FmtError { fn description(&self) -> &str { - match self { - &FmtError::Invalid(_) => "invalid format string", - &FmtError::KeyError(_) => "invalid key", - &FmtError::TypeError(_) => "error during type resolution", + match *self { + FmtError::Invalid(_) => "invalid format string", + FmtError::KeyError(_) => "invalid key", + FmtError::TypeError(_) => "error during type resolution", } }