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

False literal with empty format string warning when printing two macros #2768

Open
timvisee opened this issue May 16, 2018 · 6 comments
Open
Labels
T-macros Type: Issues with macros and macro expansion

Comments

@timvisee
Copy link
Contributor

Look at the following snippet:

println!("{} {}", crate_name!(), crate_description!());

In this code, crate_name!() and crate_description!() are macros provided by the clap crate. These two macros return a &str.

This snippet produces the following clippy warnings:

warning: printing a literal with an empty format string
 --> src/main.rs:5:23
  |
5 |     println!("{} {}", crate_name!(), crate_description!());
  |                       ^^^^^^^^^^^^^
  |
  = note: #[warn(print_literal)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.198/index.html#print_literal
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: printing a literal with an empty format string
 --> src/main.rs:5:38
  |
5 |     println!("{} {}", crate_name!(), crate_description!());
  |                                      ^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.198/index.html#print_literal
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

I believe these warnings are incorrect in this situation, and I don't see a way of improving this statement.
Is this some edge case for which the warning shouldn't be triggered, thus requiring a lint change? Or am I thinking wrong?

Of course, when only a single macro is used it can be printed directly:

println!(crate_name!());

However, this snippet uses two macros and one of these implementations wouldn't work as the first parameter isn't a valid format string:

println!(crate_name!(), " ", crate_description!());
println!(crate_name!(), crate_description!()); // Also missing a space

A code example that produces this clippy warning can be found on the playground.

The warnings might be correct, and I might have missed something. In that case; sorry.

@SpaceManiac
Copy link

println!(concat!(crate_name!(), " ", crate_description!()));

@phansch phansch added the T-macros Type: Issues with macros and macro expansion label May 23, 2018
@mrecachinas
Copy link

mrecachinas commented Jun 30, 2018

@timvisee yeah you're correct that it's an issue. See #2657 for some context.

@ThibsG
Copy link
Contributor

ThibsG commented Oct 24, 2020

@phansch I think we can close this, as like #2657, we can't reproduce this anymore.

@timvisee
Copy link
Contributor Author

timvisee commented Oct 25, 2020

we can't reproduce this anymore.

You seem to be right. Tested it on playground.

@alxiord
Copy link

alxiord commented Feb 2, 2023

Hi! This is acting up again - see playground:

println!("{} blah {}", file!(), line!());

causes

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

@koutheir
Copy link

Can someone reopen this issue? It still is not fixed.

@Jarcho Jarcho reopened this Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

No branches or pull requests

8 participants