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

[needless_late_init] suggestion doesn't compile when the initializing takes place inside macro #13776

Open
J-ZhengLi opened this issue Dec 4, 2024 · 0 comments
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion

Comments

@J-ZhengLi
Copy link
Member

Summary

playground

Lint Name

needless_late_init

Reproducer

I tried this code:

macro_rules! need_late_init {
    ($var:expr, $val:literal) => {
        $var = $val;
    }
}

fn main() {
    let x;
    need_late_init!(x, 10);
    assert_eq!(x, 10);
}

I saw this happen:

error: unneeded late initialization
  --> src/main.rs:6:9
   |
6  |         $var = $val;
   |         ^^^^ initialised here
...
12 |     let x;
   |     ^^^^^^ created here
13 |     
14 |     need_late_init!(x, 10);
   |     ---------------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_late_init
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![deny(clippy::needless_late_init)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `need_late_init` (in Nightly builds, run with -Z macro-backtrace for more info)
help: move the declaration `x` here
   |
12 ~     
13 |     
14 ~     need_late_init!(let x, 10);
   |

I expected to see this happen:

No warning, since let x is not an expr

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-pc-windows-gnu
release: 1.83.0
LLVM version: 19.1.1

Additional Labels

No response

@J-ZhengLi J-ZhengLi 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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion labels Dec 4, 2024
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

No branches or pull requests

1 participant