diff --git a/CHANGELOG.md b/CHANGELOG.md index 270eee3a..4aeca7ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - The `Constructor` and `IsVariant` derives now generate `const fn` functions. +- Static methods derived by `IsVariant` are now marked `#[must_use]`. + ([#350](https://github.com/JelteF/derive_more/pull/350)) - The `Unwrap` and `IsVariant` derives now generate doc comments. - `#[automatically_derived]` is now emitted from all macro expansions. This should prevent code style linters from attempting to modify the generated diff --git a/Cargo.toml b/Cargo.toml index 303f7358..5d2d4f62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -234,7 +234,7 @@ required-features = ["try_unwrap"] [[test]] name = "compile_fail" path = "tests/compile_fail/mod.rs" -required-features = ["as_ref", "debug", "display", "from", "into"] +required-features = ["as_ref", "debug", "display", "from", "into", "is_variant", "try_from"] [[test]] name = "no_std" diff --git a/impl/doc/is_variant.md b/impl/doc/is_variant.md index 9e549dc1..f5bf42ae 100644 --- a/impl/doc/is_variant.md +++ b/impl/doc/is_variant.md @@ -26,18 +26,20 @@ assert!(!Maybe::<()>::Nothing.is_just()); ### What is generated? -The derive in the above example code generates the following code: +The derive in the above example generates code like this: ```rust # enum Maybe { # Just(T), # Nothing # } -impl Maybe{ +impl Maybe{ + #[must_use] pub const fn is_just(&self) -> bool { - match self {Self::Just(..) => true, _ => false} + matches!(self, Self::Just(..)) } + #[must_use] pub const fn is_nothing(&self) -> bool { - match self {Self::Nothing => true, _ => false} + matches!(self, Self::Nothing) } } ``` diff --git a/impl/src/is_variant.rs b/impl/src/is_variant.rs index d667fc2e..d6fab316 100644 --- a/impl/src/is_variant.rs +++ b/impl/src/is_variant.rs @@ -45,11 +45,9 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result bool { - match self { - #enum_name ::#variant_ident #data_pattern => true, - _ => false - } + derive_more::core::matches!(self, #enum_name ::#variant_ident #data_pattern) } }; funcs.push(func); diff --git a/tests/compile_fail/is_variant/must_use.rs b/tests/compile_fail/is_variant/must_use.rs new file mode 100644 index 00000000..234daf47 --- /dev/null +++ b/tests/compile_fail/is_variant/must_use.rs @@ -0,0 +1,10 @@ +#[derive(derive_more::IsVariant)] +enum MustUse { + Yes, +} + +#[forbid(unused_must_use)] +fn main() { + let must_use = MustUse::Yes; + must_use.is_yes(); +} diff --git a/tests/compile_fail/is_variant/must_use.stderr b/tests/compile_fail/is_variant/must_use.stderr new file mode 100644 index 00000000..db6c5d13 --- /dev/null +++ b/tests/compile_fail/is_variant/must_use.stderr @@ -0,0 +1,15 @@ +error: unused return value of `MustUse::is_yes` that must be used + --> tests/compile_fail/is_variant/must_use.rs:9:5 + | +9 | must_use.is_yes(); + | ^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> tests/compile_fail/is_variant/must_use.rs:6:10 + | +6 | #[forbid(unused_must_use)] + | ^^^^^^^^^^^^^^^ +help: use `let _ = ...` to ignore the resulting value + | +9 | let _ = must_use.is_yes(); + | +++++++