Skip to content

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

@J-ZhengLi

Description

@J-ZhengLi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions