aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_backtrace.rs43
-rw-r--r--tests/test_display.rs4
-rw-r--r--tests/test_expr.rs6
-rw-r--r--tests/test_from.rs27
-rw-r--r--tests/test_generics.rs161
-rw-r--r--tests/test_lints.rs5
-rw-r--r--tests/test_path.rs4
-rw-r--r--tests/test_source.rs4
-rw-r--r--tests/test_transparent.rs4
9 files changed, 222 insertions, 36 deletions
diff --git a/tests/test_backtrace.rs b/tests/test_backtrace.rs
index c7ba3c4..42e37ca 100644
--- a/tests/test_backtrace.rs
+++ b/tests/test_backtrace.rs
@@ -1,8 +1,4 @@
#![cfg_attr(thiserror_nightly_testing, feature(backtrace))]
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
-)]
use thiserror::Error;
@@ -11,8 +7,15 @@ use thiserror::Error;
pub struct Inner;
#[cfg(thiserror_nightly_testing)]
+#[derive(Error, Debug)]
+#[error("...")]
+pub struct InnerBacktrace {
+ backtrace: std::backtrace::Backtrace,
+}
+
+#[cfg(thiserror_nightly_testing)]
pub mod structs {
- use super::Inner;
+ use super::{Inner, InnerBacktrace};
use std::backtrace::Backtrace;
use std::error::Error;
use std::sync::Arc;
@@ -56,6 +59,14 @@ pub mod structs {
#[derive(Error, Debug)]
#[error("...")]
+ pub struct CombinedBacktraceFrom {
+ #[from]
+ #[backtrace]
+ source: InnerBacktrace,
+ }
+
+ #[derive(Error, Debug)]
+ #[error("...")]
pub struct OptBacktraceFrom {
#[from]
source: Inner,
@@ -97,6 +108,11 @@ pub mod structs {
let error = BacktraceFrom::from(Inner);
assert!(error.backtrace().is_some());
+ let error = CombinedBacktraceFrom::from(InnerBacktrace {
+ backtrace: Backtrace::capture(),
+ });
+ assert!(error.backtrace().is_some());
+
let error = OptBacktraceFrom::from(Inner);
assert!(error.backtrace().is_some());
@@ -107,7 +123,7 @@ pub mod structs {
#[cfg(thiserror_nightly_testing)]
pub mod enums {
- use super::Inner;
+ use super::{Inner, InnerBacktrace};
use std::backtrace::Backtrace;
use std::error::Error;
use std::sync::Arc;
@@ -158,6 +174,16 @@ pub mod enums {
}
#[derive(Error, Debug)]
+ pub enum CombinedBacktraceFrom {
+ #[error("...")]
+ Test {
+ #[from]
+ #[backtrace]
+ source: InnerBacktrace,
+ },
+ }
+
+ #[derive(Error, Debug)]
pub enum OptBacktraceFrom {
#[error("...")]
Test {
@@ -204,6 +230,11 @@ pub mod enums {
let error = BacktraceFrom::from(Inner);
assert!(error.backtrace().is_some());
+ let error = CombinedBacktraceFrom::from(InnerBacktrace {
+ backtrace: Backtrace::capture(),
+ });
+ assert!(error.backtrace().is_some());
+
let error = OptBacktraceFrom::from(Inner);
assert!(error.backtrace().is_some());
diff --git a/tests/test_display.rs b/tests/test_display.rs
index 8a9ebf7..949d9ed 100644
--- a/tests/test_display.rs
+++ b/tests/test_display.rs
@@ -1,8 +1,4 @@
#![deny(clippy::all, clippy::pedantic)]
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
-)]
use std::fmt::Display;
use thiserror::Error;
diff --git a/tests/test_expr.rs b/tests/test_expr.rs
index 50835a5..87a56cf 100644
--- a/tests/test_expr.rs
+++ b/tests/test_expr.rs
@@ -1,9 +1,5 @@
#![deny(clippy::all, clippy::pedantic)]
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
- clippy::option_if_let_else,
-)]
+#![allow(clippy::option_if_let_else)]
use std::fmt::Display;
use thiserror::Error;
diff --git a/tests/test_from.rs b/tests/test_from.rs
index b6a3c0c..a25ce35 100644
--- a/tests/test_from.rs
+++ b/tests/test_from.rs
@@ -1,8 +1,4 @@
#![deny(clippy::all, clippy::pedantic)]
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
-)]
use std::io;
use thiserror::Error;
@@ -16,10 +12,21 @@ pub struct ErrorStruct {
#[derive(Error, Debug)]
#[error("...")]
+pub struct ErrorStructOptional {
+ #[from]
+ source: Option<io::Error>,
+}
+
+#[derive(Error, Debug)]
+#[error("...")]
pub struct ErrorTuple(#[from] io::Error);
#[derive(Error, Debug)]
#[error("...")]
+pub struct ErrorTupleOptional(#[from] Option<io::Error>);
+
+#[derive(Error, Debug)]
+#[error("...")]
pub enum ErrorEnum {
Test {
#[from]
@@ -29,6 +36,15 @@ pub enum ErrorEnum {
#[derive(Error, Debug)]
#[error("...")]
+pub enum ErrorEnumOptional {
+ Test {
+ #[from]
+ source: Option<io::Error>,
+ },
+}
+
+#[derive(Error, Debug)]
+#[error("...")]
pub enum Many {
Any(#[from] anyhow::Error),
Io(#[from] io::Error),
@@ -39,7 +55,10 @@ fn assert_impl<T: From<io::Error>>() {}
#[test]
fn test_from() {
assert_impl::<ErrorStruct>();
+ assert_impl::<ErrorStructOptional>();
assert_impl::<ErrorTuple>();
+ assert_impl::<ErrorTupleOptional>();
assert_impl::<ErrorEnum>();
+ assert_impl::<ErrorEnumOptional>();
assert_impl::<Many>();
}
diff --git a/tests/test_generics.rs b/tests/test_generics.rs
new file mode 100644
index 0000000..f5e1de2
--- /dev/null
+++ b/tests/test_generics.rs
@@ -0,0 +1,161 @@
+#![deny(clippy::all, clippy::pedantic)]
+
+use std::fmt::{self, Debug, Display};
+use thiserror::Error;
+
+pub struct NoFormat;
+
+#[derive(Debug)]
+pub struct DebugOnly;
+
+pub struct DisplayOnly;
+
+impl Display for DisplayOnly {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("display only")
+ }
+}
+
+#[derive(Debug)]
+pub struct DebugAndDisplay;
+
+impl Display for DebugAndDisplay {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("debug and display")
+ }
+}
+
+// Should expand to:
+//
+// impl<E> Display for EnumDebugField<E>
+// where
+// E: Debug;
+//
+// impl<E> Error for EnumDebugField<E>
+// where
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub enum EnumDebugGeneric<E> {
+ #[error("{0:?}")]
+ FatalError(E),
+}
+
+// Should expand to:
+//
+// impl<E> Display for EnumFromGeneric<E>;
+//
+// impl<E> Error for EnumFromGeneric<E>
+// where
+// EnumDebugGeneric<E>: Error + 'static,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub enum EnumFromGeneric<E> {
+ #[error("enum from generic")]
+ Source(#[from] EnumDebugGeneric<E>),
+}
+
+// Should expand to:
+//
+// impl<HasDisplay, HasDebug, HasNeither> Display
+// for EnumCompound<HasDisplay, HasDebug, HasNeither>
+// where
+// HasDisplay: Display,
+// HasDebug: Debug;
+//
+// impl<HasDisplay, HasDebug, HasNeither> Error
+// for EnumCompound<HasDisplay, HasDebug, HasNeither>
+// where
+// Self: Debug + Display;
+//
+#[derive(Error)]
+pub enum EnumCompound<HasDisplay, HasDebug, HasNeither> {
+ #[error("{0} {1:?}")]
+ DisplayDebug(HasDisplay, HasDebug),
+ #[error("{0}")]
+ Display(HasDisplay, HasNeither),
+ #[error("{1:?}")]
+ Debug(HasNeither, HasDebug),
+}
+
+impl<HasDisplay, HasDebug, HasNeither> Debug for EnumCompound<HasDisplay, HasDebug, HasNeither> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("EnumCompound")
+ }
+}
+
+#[test]
+fn test_display_enum_compound() {
+ let mut instance: EnumCompound<DisplayOnly, DebugOnly, NoFormat>;
+
+ instance = EnumCompound::DisplayDebug(DisplayOnly, DebugOnly);
+ assert_eq!(format!("{}", instance), "display only DebugOnly");
+
+ instance = EnumCompound::Display(DisplayOnly, NoFormat);
+ assert_eq!(format!("{}", instance), "display only");
+
+ instance = EnumCompound::Debug(NoFormat, DebugOnly);
+ assert_eq!(format!("{}", instance), "DebugOnly");
+}
+
+// Should expand to:
+//
+// impl<E> Display for EnumTransparentGeneric<E>
+// where
+// E: Display;
+//
+// impl<E> Error for EnumTransparentGeneric<E>
+// where
+// E: Error,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub enum EnumTransparentGeneric<E> {
+ #[error(transparent)]
+ Other(E),
+}
+
+// Should expand to:
+//
+// impl<E> Display for StructDebugGeneric<E>
+// where
+// E: Debug;
+//
+// impl<E> Error for StructDebugGeneric<E>
+// where
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+#[error("{underlying:?}")]
+pub struct StructDebugGeneric<E> {
+ pub underlying: E,
+}
+
+// Should expand to:
+//
+// impl<E> Error for StructFromGeneric<E>
+// where
+// StructDebugGeneric<E>: Error + 'static,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+pub struct StructFromGeneric<E> {
+ #[from]
+ pub source: StructDebugGeneric<E>,
+}
+
+// Should expand to:
+//
+// impl<E> Display for StructTransparentGeneric<E>
+// where
+// E: Display;
+//
+// impl<E> Error for StructTransparentGeneric<E>
+// where
+// E: Error,
+// Self: Debug + Display;
+//
+#[derive(Error, Debug)]
+#[error(transparent)]
+pub struct StructTransparentGeneric<E>(E);
diff --git a/tests/test_lints.rs b/tests/test_lints.rs
index 457b799..59699a4 100644
--- a/tests/test_lints.rs
+++ b/tests/test_lints.rs
@@ -1,8 +1,3 @@
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
-)]
-
use thiserror::Error;
pub use std::error::Error;
diff --git a/tests/test_path.rs b/tests/test_path.rs
index 777b5de..a10b1f3 100644
--- a/tests/test_path.rs
+++ b/tests/test_path.rs
@@ -1,8 +1,4 @@
#![deny(clippy::all, clippy::pedantic)]
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
-)]
use ref_cast::RefCast;
use std::fmt::Display;
diff --git a/tests/test_source.rs b/tests/test_source.rs
index c7ddda8..ab16097 100644
--- a/tests/test_source.rs
+++ b/tests/test_source.rs
@@ -1,8 +1,4 @@
#![deny(clippy::all, clippy::pedantic)]
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
-)]
use std::error::Error as StdError;
use std::io;
diff --git a/tests/test_transparent.rs b/tests/test_transparent.rs
index 7d02826..84d7c91 100644
--- a/tests/test_transparent.rs
+++ b/tests/test_transparent.rs
@@ -1,8 +1,4 @@
#![deny(clippy::all, clippy::pedantic)]
-#![allow(
- // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
- clippy::nonstandard_macro_braces,
-)]
use anyhow::anyhow;
use std::error::Error as _;