diff --git a/.travis.yml b/.travis.yml index 61d8965..f52b5cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,6 @@ rust: - nightly cache: cargo script: - - cargo test + - cargo test --all - cargo test --features backtrace - cargo check --no-default-features diff --git a/failure_derive/src/lib.rs b/failure_derive/src/lib.rs index 230d2ee..e22f1c0 100644 --- a/failure_derive/src/lib.rs +++ b/failure_derive/src/lib.rs @@ -69,14 +69,13 @@ fn fail_derive(s: synstructure::Structure) -> TokenStream { } fn display_body(s: &synstructure::Structure) -> Option { - let mut msgs = s.variants().iter().map(|v| find_error_msg(&v.ast().attrs)); - if msgs.all(|msg| msg.is_none()) { - return None; - } - Some(s.each_variant(|v| { - let msg = - find_error_msg(&v.ast().attrs).expect("All variants must have display attribute."); + let msg = match find_error_msg(&v.ast().attrs) { + Some(msg) => msg, + None => { + return quote!(return write!(f, "{:?}", self)); + } + }; if msg.nested.is_empty() { panic!("Expected at least one argument to fail attribute"); } diff --git a/failure_derive/tests/tests.rs b/failure_derive/tests/tests.rs index 4e73255..a632276 100644 --- a/failure_derive/tests/tests.rs +++ b/failure_derive/tests/tests.rs @@ -53,3 +53,27 @@ fn enum_error() { let s = format!("{}", EnumError::UnitVariant); assert_eq!(&s[..], "An error has occurred."); } + +#[derive(Debug, Fail)] +enum EnumWithNoAttr { + #[fail(display = "Error: {}", _0)] + TupleVariant(usize), + UnitVariant, +} + +#[test] +fn enum_with_no_attr() { + let s = format!("{}", EnumWithNoAttr::TupleVariant(4)); + assert_eq!(&s[..], "Error: 4"); + let s = format!("{}", EnumWithNoAttr::UnitVariant); + assert_eq!(&s[..], "UnitVariant"); +} + +#[derive(Debug, Fail)] +struct StructWithNoAttr; + +#[test] +fn struct_with_no_attr() { + let s = format!("{}", StructWithNoAttr {}); + assert_eq!(&s[..], "StructWithNoAttr"); +}