Skip to content

Commit

Permalink
Add compatibility with 'deny(rust_2018_idioms)'
Browse files Browse the repository at this point in the history
In crates with
 #![forbid|deny(rust_2018_idioms)]
the code generated by the quick_error macro will now cause linter
errors.

This commit turns these on in the crate itself and fixes them in two
ways:

* blanket #[allow(unused)] was replaced with a more specific version
* the lifetime in std::fmt::Formatter types is declareed
  • Loading branch information
Byron committed Apr 27, 2021
1 parent 3b09136 commit bdad05b
Showing 1 changed file with 24 additions and 66 deletions.
90 changes: 24 additions & 66 deletions src/lib.rs
@@ -1,4 +1,4 @@
#![warn(missing_docs)]
#![warn(missing_docs, rust_2018_idioms)]
//! A macro which makes errors easy to write
//!
//! Minimum type is like this:
Expand Down Expand Up @@ -356,7 +356,7 @@ macro_rules! quick_error {
$($strdef)* $strname ( $internal );

impl ::std::fmt::Display for $strname {
fn fmt(&self, f: &mut ::std::fmt::Formatter)
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>)
-> ::std::fmt::Result
{
::std::fmt::Display::fmt(&self.0, f)
Expand Down Expand Up @@ -623,13 +623,13 @@ macro_rules! quick_error {
$item:ident: $imode:tt [$(#[$imeta:meta])*] [$( $var:ident: $typ:ty ),*] {$( $funcs:tt )*}
)*}
) => {
#[allow(unused)]
#[allow(unused_variables)]
#[allow(unknown_lints)] // no unused_doc_comments in older rust
#[allow(renamed_and_removed_lints)]
#[allow(unused_doc_comment)]
#[allow(unused_doc_comments)]
impl ::std::fmt::Display for $name {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter)
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>)
-> ::std::fmt::Result
{
match *self {
Expand All @@ -648,7 +648,7 @@ macro_rules! quick_error {
}
}
}
#[allow(unused)]
#[allow(unused_variables)]
#[allow(unknown_lints)] // no unused_doc_comments in older rust
#[allow(renamed_and_removed_lints)]
#[allow(unused_doc_comment)]
Expand Down Expand Up @@ -683,17 +683,17 @@ macro_rules! quick_error {
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
{ display($self_:tt) -> ($( $exprs:tt )*) $( $tail:tt )*}
) => {
|quick_error!(IDENT $self_): &$name, f: &mut ::std::fmt::Formatter| { write!(f, $( $exprs )*) }
|quick_error!(IDENT $self_): &$name, f: &mut ::std::fmt::Formatter<'_>| { write!(f, $( $exprs )*) }
};
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
{ display($pattern:expr) $( $tail:tt )*}
) => {
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern) }
|_, f: &mut ::std::fmt::Formatter<'_>| { write!(f, $pattern) }
};
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
{ display($pattern:expr, $( $exprs:tt )*) $( $tail:tt )*}
) => {
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern, $( $exprs )*) }
|_, f: &mut ::std::fmt::Formatter<'_>| { write!(f, $pattern, $( $exprs )*) }
};
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
{ $t:tt $( $tail:tt )*}
Expand All @@ -705,7 +705,7 @@ macro_rules! quick_error {
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
{ }
) => {
|self_: &$name, f: &mut ::std::fmt::Formatter| {
|self_: &$name, f: &mut ::std::fmt::Formatter<'_>| {
write!(f, "{:?}", self_)
}
};
Expand Down Expand Up @@ -1016,18 +1016,12 @@ mod test {

#[test]
fn wrapper() {
assert_eq!(
format!("{}", Wrapper::from(Wrapped::One)),
"One".to_string()
);
assert_eq!(format!("{}", Wrapper::from(Wrapped::One)), "One".to_string());
assert_eq!(
format!("{}", Wrapper::from(Wrapped::from(String::from("hello")))),
"two: hello".to_string()
);
assert_eq!(
format!("{:?}", Wrapper::from(Wrapped::One)),
"Wrapper(One)".to_string()
);
assert_eq!(format!("{:?}", Wrapper::from(Wrapped::One)), "Wrapper(One)".to_string());
}

quick_error! {
Expand Down Expand Up @@ -1062,14 +1056,8 @@ mod test {
let source = "one and a half times pi".parse::<f32>().unwrap_err();
let err = TupleWrapper::ParseFloatError(source.clone());
assert_eq!(format!("{}", err), format!("parse float error: {}", source));
assert_eq!(
format!("{:?}", err),
format!("ParseFloatError({:?})", source)
);
assert_eq!(
format!("{:?}", err.source().unwrap()),
format!("{:?}", source)
);
assert_eq!(format!("{:?}", err), format!("ParseFloatError({:?})", source));
assert_eq!(format!("{:?}", err.source().unwrap()), format!("{:?}", source));
}

#[test]
Expand All @@ -1084,9 +1072,7 @@ mod test {
#[test]
fn tuple_wrapper_trait_two_fields() {
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
let source = String::from_utf8(invalid_utf8.clone())
.unwrap_err()
.utf8_error();
let source = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
let err: &dyn Error = &TupleWrapper::FromUtf8Error(source.clone(), invalid_utf8.clone());
assert_eq!(
format!("{}", err),
Expand All @@ -1101,10 +1087,7 @@ mod test {
format!("{:?}", err),
format!("FromUtf8Error({:?}, {:?})", source, invalid_utf8)
);
assert_eq!(
format!("{:?}", err.source().unwrap()),
format!("{:?}", source)
);
assert_eq!(format!("{:?}", err.source().unwrap()), format!("{:?}", source));
}

#[test]
Expand Down Expand Up @@ -1159,9 +1142,7 @@ mod test {
#[test]
fn struct_wrapper_err() {
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
let source = String::from_utf8(invalid_utf8.clone())
.unwrap_err()
.utf8_error();
let source = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
let err: &dyn Error = &StructWrapper::Utf8Error {
err: source.clone(),
hint: Some("nonsense"),
Expand All @@ -1177,24 +1158,15 @@ mod test {
);
assert_eq!(
format!("{:?}", err),
format!(
"Utf8Error {{ err: {:?}, hint: {:?} }}",
source,
Some("nonsense")
)
);
assert_eq!(
format!("{:?}", err.source().unwrap()),
format!("{:?}", source)
format!("Utf8Error {{ err: {:?}, hint: {:?} }}", source, Some("nonsense"))
);
assert_eq!(format!("{:?}", err.source().unwrap()), format!("{:?}", source));
}

#[test]
fn struct_wrapper_struct_from() {
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
let source = String::from_utf8(invalid_utf8.clone())
.unwrap_err()
.utf8_error();
let source = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
let err = StructWrapper::Utf8Error {
err: source.clone(),
hint: None,
Expand All @@ -1208,10 +1180,7 @@ mod test {
let descr = "hello";
let err = StructWrapper::ExcessComma { descr: descr };
assert_eq!(format!("{}", err), format!("Error: {}", descr));
assert_eq!(
format!("{:?}", err),
format!("ExcessComma {{ descr: {:?} }}", descr)
);
assert_eq!(format!("{:?}", err), format!("ExcessComma {{ descr: {:?} }}", descr));
assert!(err.source().is_none());
}

Expand Down Expand Up @@ -1282,9 +1251,7 @@ mod test {
}
let etext = parse_utf(b"a\x80\x80", "/etc").unwrap_err().to_string();
assert!(etext.starts_with("Path error at \"/etc\": invalid utf-8"));
let etext = parse_utf(b"\x80\x80", PathBuf::from("/tmp"))
.unwrap_err()
.to_string();
let etext = parse_utf(b"\x80\x80", PathBuf::from("/tmp")).unwrap_err().to_string();
assert!(etext.starts_with("Path error at \"/tmp\": invalid utf-8"));
}

Expand All @@ -1304,9 +1271,7 @@ mod test {
#[allow(deprecated)]
fn cause_struct_wrapper_err() {
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
let cause = String::from_utf8(invalid_utf8.clone())
.unwrap_err()
.utf8_error();
let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
let err: &dyn Error = &StructWrapper::Utf8Error {
err: cause.clone(),
hint: Some("nonsense"),
Expand All @@ -1322,15 +1287,8 @@ mod test {
);
assert_eq!(
format!("{:?}", err),
format!(
"Utf8Error {{ err: {:?}, hint: {:?} }}",
cause,
Some("nonsense")
)
);
assert_eq!(
format!("{:?}", err.cause().unwrap()),
format!("{:?}", cause)
format!("Utf8Error {{ err: {:?}, hint: {:?} }}", cause, Some("nonsense"))
);
assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause));
}
}

0 comments on commit bdad05b

Please sign in to comment.