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

False positive box_default inside macro #11868

Closed
synek317 opened this issue Nov 25, 2023 · 0 comments · Fixed by #11875
Closed

False positive box_default inside macro #11868

synek317 opened this issue Nov 25, 2023 · 0 comments · Fixed by #11875
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@synek317
Copy link

Summary

If Box::new is used in a macro and then this macro is called with a default value at least once, then the lint is triggered even if in other macro invocations, default could not be used - see code below.

Link to the playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9e737f21f166c1cb8ad0cce037fb5649

Lint Name

box_default

Reproducer

I tried this code:

fn foo(_: &mut Vec<usize>) {}

macro_rules! bar {
    ($baz:expr) => {
        Box::leak(Box::new($baz))
    };
}

fn main() {
    foo(bar!(vec![]));
    foo(bar!(vec![1]));
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: `Box::new(_)` of default value
  --> src/main.rs:5:19
   |
5  |         Box::leak(Box::new($baz))
   |                   ^^^^^^^^^^^^^^ help: try: `Box::default()`
...
10 |     foo(bar!(vec![]));
   |         ------------ in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#box_default
   = note: `#[warn(clippy::box_default)]` on by default
   = note: this warning originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.64s

I expected to see this happen:

    Checking playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 0.35s

Version

1.76.0-nightly (2023-11-24 37b2813a7be580ef5904)

Additional Labels

No response

@synek317 synek317 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 25, 2023
@bors bors closed this as completed in 2c56b4f Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant