Skip to content

Commit

Permalink
Add #[must_use] annotation to derive(IsVariant) (#350, #349)
Browse files Browse the repository at this point in the history
## Synopsis & Solution

Adds a `#[must_use]` annotation to the static methods generated by
`IsVariant`. I also refactored the derive to use `matches!(...)` instead
of a manual `match` block with bools, but that's just a style tweak.
  • Loading branch information
TheLostLambda committed Apr 19, 2024
1 parent 42d5251 commit e0d1698
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions impl/doc/is_variant.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
# Just(T),
# Nothing
# }
impl <T> Maybe<T>{
impl<T> Maybe<T>{
#[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)
}
}
```
6 changes: 2 additions & 4 deletions impl/src/is_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
#[doc = #variant_name]
#[doc = "`. Returns `false` otherwise"]
#[inline]
#[must_use]
pub const fn #fn_name(&self) -> 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);
Expand Down
10 changes: 10 additions & 0 deletions tests/compile_fail/is_variant/must_use.rs
Original file line number Diff line number Diff line change
@@ -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();
}
15 changes: 15 additions & 0 deletions tests/compile_fail/is_variant/must_use.stderr
Original file line number Diff line number Diff line change
@@ -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();
| +++++++

0 comments on commit e0d1698

Please sign in to comment.