Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(is_variant): add #[must_use] annotation #350

Merged
merged 10 commits into from
Apr 19, 2024
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]`.
tyranron marked this conversation as resolved.
Show resolved Hide resolved
([#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 {
tyranron marked this conversation as resolved.
Show resolved Hide resolved
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();
| +++++++