Skip to content

[len_zero] suggestion causing compilation fail when macros involved #11992

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

Closed
J-ZhengLi opened this issue Dec 21, 2023 · 0 comments · Fixed by #11996
Closed

[len_zero] suggestion causing compilation fail when macros involved #11992

J-ZhengLi opened this issue Dec 21, 2023 · 0 comments · Fixed by #11996
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

Comments

@J-ZhengLi
Copy link
Member

J-ZhengLi commented Dec 21, 2023

Summary

Somewhat related to PR: #11991

The issue was that the given suggestion span was inside of a macro, rather than the macro callsite. See reproduce code.

I think the lint should skip warnings when the code was expanded from macro.

Lint Name

len_zero

Reproducer

I tried this code:

fn main() {
    macro_rules! len {
        ($seq:ident) => {
            $seq.len()
        }
    }
    
    let s = [1, 2, 3];
    
    if len!(s) == 0 {
        println!("oops");
    }
    (len!(s) == 0).then(|| println!("oops"));

I saw this happen:

warning: length comparison to zero
  --> src/main.rs:7:13
   |
7  | /             $seq.len()
8  | |         }
9  | |     }
10 | |     
11 | |     let s = [1, 2, 3];
12 | |     
13 | |     if len!(s) == 0 {
   | |________-------____^ help: using `is_empty` is clearer and more explicit: `$seq.is_empty()`
   |          |
   |          in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
...

I expected to see this happen:

no warnings, because we might not be able to change the content inside of macros.

Version

playground: clippy 0.1.74 (2023-12-04 a28077b)

Additional Labels

@rustbot label +I-suggestion-causes-error

@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 labels Dec 21, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 21, 2023
bors added a commit that referenced this issue Apr 1, 2024
fix suggestion for [`len_zero`] with macros

fixes: #11992

changelog: fix suggestion for [`len_zero`] with macros
@bors bors closed this as completed in 2b30a59 Apr 1, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants