rust-lang / rust-lang/rust-clippy

False literal with empty format string warning when printing two macros

Open
#2,768 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-macros
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust reproducer in the issue and inspect Clippy's print_literal lint, which emits the warning for the two clap macros. Confirm whether the warnings are false positives and add or update a regression test for this case; done means the valid two-macro println no longer produces the warning without weakening valid diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.