Skip to content

False positive for file!() macro in println #10544

Closed
@hellow554

Description

@hellow554

Summary

When using file!() as a macro inside of println!, it suggest to replace it.

The suggestion leads to invalid code.

The error was probably introduces in 6fc6d87 @Alexendoo

As shown in the code below, other macro calls work fine, it seems to be a problem with the file!() macro in particular.

Maybe checking && value_string != "file!()" would be a valid solution here?!

&& let Some(value_string) = snippet_opt(cx, arg.expr.span)

Lint Name

print_literal

Reproducer

I tried this code:

macro_rules! static_str {
    () => { "Hello World" }
}

fn main() {
    println!("{}", file!());
    println!("{}", line!());
    println!("{}", static_str!());
    println!("{}", "Hello World");
}

I saw this happen:

warning: literal with an empty format string
 --> src/main.rs:6:20
  |
6 |     println!("{}", file!());
  |                    ^^^^^^^
  |
  = note: `#[warn(clippy::print_literal)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
help: try this
  |
6 -     println!("{}", file!());
6 +     println!("ile!(");
  |

warning: literal with an empty format string
 --> src/main.rs:9:20
  |
9 |     println!("{}", "Hello World");
  |                    ^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
help: try this
  |
9 -     println!("{}", "Hello World");
9 +     println!("Hello World");
  |

warning: `abc` (bin "abc") generated 2 warnings

I expected to see this happen:

Not issuing the first warning, but only the second

Version

rustc 1.66.0-nightly (f5193a9fc 2022-09-25)
binary: rustc
commit-hash: f5193a9fcc73dc09e41a90c5a2c97fc9acc16032
commit-date: 2022-09-25
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.0

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