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 positive: print_literal and env! #2657

Closed
bjgill opened this issue Apr 11, 2018 · 5 comments
Closed

False positive: print_literal and env! #2657

bjgill opened this issue Apr 11, 2018 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing T-macros Type: Issues with macros and macro expansion

Comments

@bjgill
Copy link
Contributor

bjgill commented Apr 11, 2018

Found with clippy 0.0.193

warning: printing a literal with an empty format string
  --> src/bin/rm/main.rs:92:41
   |
92 |         println!("cargo-rm version {}", env!("CARGO_PKG_VERSION"));
   |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: #[warn(print_literal)] on by default
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.193/index.html#print_literal

I'm fairly certain that there isn't a way to shorten this as the docs suggest.

It looks as if this was discussed in #2617 and #2608. Whilst it's true that println!(env!("FOO")) is better than println!("{}", env!("FOO")), I don't think there is an equivalent for println!("bar: {}", env!("FOO")).

@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label Apr 13, 2018
@mrecachinas
Copy link

Yeah this is a tricky one... using a late pass converts all macros, so env!(“FOO”) looks like a string by the time the print_literal check happens.

@mrecachinas
Copy link

mrecachinas commented Apr 23, 2018

@oli-obk @phansch So I looked a bit more into this. I added a test for env!("FOO") where I've defined "$FOO" to be "test" (export FOO=test). As expected, the test yielded a false positive:

error: printing a literal with an empty format string
  --> tests/ui/print_literal.rs:40:25
   |
40 |     println!("ENV: {}", env!("FOO"));
   |                         ^^^^^^^^^^^

I then tried to inspect what's going on as well as use utils::{in_macro}. Unfortunately, in_macro is false for the exact span that gets warned in the lint.

Looking into the args at the time it's linted in write.rs, you can see expr(896: &match (&"test",) ...)

[expr(885: &["ENV: ", "\n"]), expr(896: &match (&"test",) {
     (__arg0,) => [<::fmt::ArgumentV1>::new(__arg0, ::fmt::Display::fmt)],
 }), expr(908: &[::fmt::rt::v1::Argument{position: ::fmt::rt::v1::Position::At(0usize),
                          format:
                              ::fmt::rt::v1::FormatSpec{fill: ' ',
                                                        align:
                                                            ::fmt::rt::v1::Alignment::Unknown,
                                                        flags: 0u32,
                                                        precision:
                                                            ::fmt::rt::v1::Count::Implied,
                                                        width:
                                                            ::fmt::rt::v1::Count::Implied,},}])]

I'm very curious what the way to proceed here would be. I'm thinking this might require an EarlyLintPass, but I'm not entirely sure how to chain both together.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 24, 2018

I assumed I fixed this with rust-lang/rust#44248

Maybe it regressed again in rustc?

@mbr
Copy link

mbr commented Jul 15, 2018

Still present on rustc 1.29.0-nightly (64f7de9 2018-07-12), clipp 0.0.211.

@phansch phansch added the T-macros Type: Issues with macros and macro expansion label Jul 16, 2018
@phansch
Copy link
Member

phansch commented Oct 6, 2018

I can't reproduce this on the playground anymore, so I'm going ahead and will close this issue. Feel free to re-open if this still happens with a recent Clippy version.

@phansch phansch closed this as completed Oct 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

No branches or pull requests

5 participants