Skip to content

Specifying lint levels does not work on macros #87391

@asquared31415

Description

@asquared31415

Given the following code:

fn main() {
    #[allow(irrefutable_let_patterns)]
    println!("{}", if let _ = 1 { 42 } else { 0 });
    
    // The macro doesn't matter, even macros like `asm!` exhibit this behavior
    #[allow(irrefutable_let_patterns)]
    dbg!("{}", if let _ = 1 { 42 } else { 0 });
    
    #[allow(irrefutable_let_patterns)]
    {
        println!("{}", if let _ = 2 { 84 } else { 0 });
    }
    
    #[allow(irrefutable_let_patterns)]
    if let _ = 3 { 
        println!("{}", 168);
    } else { 
        println!("{}", 0);
    }
}

playground

The current output is:

warning: irrefutable `if let` pattern
 --> src/main.rs:3:20
  |
3 |     println!("{}", if let _ = 1 { 42 } else { 0 });
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(irrefutable_let_patterns)]` on by default
  = note: this pattern will always match, so the `if let` is useless
  = help: consider replacing the `if let` with a `let`

warning: irrefutable `if let` pattern
 --> src/main.rs:7:16
  |
7 |     dbg!("{}", if let _ = 1 { 42 } else { 0 });
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this pattern will always match, so the `if let` is useless
  = help: consider replacing the `if let` with a `let`

warning: 2 warnings emitted

Ideally in this case, the compilation should succeed without warning, as the lint appears to have been explicitly allowed everywhere it is used.

This likely applies for all lints and all lint levels and all macros

  • (only tested println!, dbg!, and asm! with allow, warn, and deny for irrefutable_let_patterns as well as in in my in progress lint named_asm_labels)

A current workaround is to create a scope to put the macro in, and apply the lint level to that scope, as demonstrated in the example.

Applying the lint level to the crate with #![level(lint)] works as expected, however this is often not desirable.

Possibly related: #59306

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions