Skip to content

Check for literals as println! args #2608

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

Merged
merged 5 commits into from
Apr 2, 2018

Conversation

mrecachinas
Copy link

Addresses #2480

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very clean, nice. Just a nit due to recent changes in our system.

Could you also add a test that ensures that println!("foo: {}", env!("BAR")); does not lint?

/// ```rust
/// println!("{}", "foo");
/// ```
declare_lint! {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're deprecating the use of this macro. Please use declare_clippy_lint together with the nursery group

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fixed in 511aa65.

@mrecachinas
Copy link
Author

Good catch on the println!("foo: {}", env!("BAR")); -- it does throw the warning. I'll take a look at that and modify to use the declare_clippy_lint.

Currently running into some weirdness when running update_all_references.sh. It's modifying tests/ui/print.rs, tests/ui/print_with_newline.rs, and tests/ui/format.rs even with #![allow(print_literal)].

@oli-obk
Copy link
Contributor

oli-obk commented Apr 1, 2018

Sometimes you need to rerun all tests, run the script, run tests again, run the script before the update script catches on. This also happens inside rustc... no clue what's exactly happening.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 1, 2018

You can probably fix the env! issue with very targetted in_macro calls. Not sure how that'll work, since println is also a macro. But technically the args should not be inside a macro expansion

@mrecachinas mrecachinas force-pushed the feature/print-string-literal branch from 16a88a6 to 511aa65 Compare April 1, 2018 23:27
@mrecachinas
Copy link
Author

Yeah, looked like things were out of sync. A cargo clean seemed to do the trick.

@mrecachinas
Copy link
Author

mrecachinas commented Apr 1, 2018

Do you think this should be pulled out of print.rs or run as an EarlyLintPass? I'm not seeing any indicator of a macro call (in this case to env!). On second thought, I don't think an EarlyLintPass would expand print! or println!.

In the test println!("foo: {}", env!("BAR")); (after setting BAR=test), I'm just seeing the value of $BAR (which in my case is the string "test")

@oli-obk
Copy link
Contributor

oli-obk commented Apr 2, 2018

Ah, thanks for investigating. Can you just list this issue under the downsides of the lint? It's fine to have this weird corner case imo

@mrecachinas
Copy link
Author

mrecachinas commented Apr 2, 2018

Sounds good. I added the test-case and added the issue to "Known Problems."

Let me know if there's anything else you'd like to see in this.

@mrecachinas
Copy link
Author

To make the tests reproducible (on all platforms) (and without explicitly setting an environment var), I removed the test with the env!. I suppose a format! would accomplish the same thing. Let me know if you want it added.

@oli-obk oli-obk merged commit 90fcdaf into rust-lang:master Apr 2, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Apr 2, 2018

Nope, this is fine as it is. Thanks

@mrecachinas mrecachinas deleted the feature/print-string-literal branch April 2, 2018 21:35
@ghost ghost mentioned this pull request Apr 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants