-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add new literal_string_with_formatting_args
lint
#13410
Add new literal_string_with_formatting_args
lint
#13410
Conversation
literal_string_with_formatting_arg
lint
}) | ||
.collect::<Vec<_>>(); | ||
if spans.len() == 1 { | ||
span_lint( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could emit a suggestion for both cases by wrapping the literal into &format!()
. Not sure if it's a good idea since it could be wrong in a lot of cases so for now I didn't do it.
Would like to see a test for malformed fmt attemps such as |
I did a little bit of testing, and it seems to work like it should! ❤️ A few more unit tests would be nice though |
I just found a much better idea: using the rustc format parser directly. :3 |
d720088
to
2356854
Compare
So it now uses the rustc format parser, making things much better overall: no more regex, less false positive, simpler code. I also added all suggested test cases and some more. |
What does the lint do for the empty JSON object |
cx, | ||
LITERAL_STRING_WITH_FORMATTING_ARG, | ||
spans, | ||
"this is a formatting argument but it is not part of a formatting macro", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a less strong wording would be good, e.g. "this looks like a formatting argument…" ? Just in case of false positives
2356854
to
c662397
Compare
It lints (as expected 😄). There was a test for that with other characters but created a new one with just
Good idea! I changed the wording. |
6a3f320
to
c027b98
Compare
Realistically speaking, I feel like
If someone writes |
Good point. Should I only lint against non-empty |
2d7c05d
to
852f1e3
Compare
I removed the check for |
☔ The latest upstream changes (presumably #13322) made this pull request unmergeable. Please resolve the merge conflicts. |
852f1e3
to
d9c9ad4
Compare
ffce1c1
to
076d9ca
Compare
Implemented the limitation as asked. The ui test file is a good summary of what's emitting the lint or not. Please tell me if you need more restrictions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Superficial NITs the rest looks good :D
| | ||
LL | x.expect(r##" {y:?} {y:?} "##); | ||
| ^^^^^ ^^^^^ | ||
|
||
error: this looks like a formatting argument but it is not part of a formatting macro | ||
--> tests/ui/literal_string_with_formatting_args.rs:17:18 | ||
--> tests/ui/literal_string_with_formatting_arg.rs:18:18 | ||
| | ||
LL | x.expect("———{:?}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this still being linted after restricting the lint to idents in the current scope?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as there is a valid formatter with :
, then it's highly likely that it's actually something the user intended. I don't know any language where {:?}
or {:#}
or {:.2}
would make sense (doesn't mean they don't exist) so I think it's pretty safe to keep these cases. I can remove them if preferred but I think it's not cases that could trigger issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, I'm happy to keep it like this :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeay! \o/
} | ||
|
||
if fmt_str[start + 1..].trim_start().starts_with('}') { | ||
// For now, we ignore `{}`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: The "For now" sounds like an open TODO
// For now, we ignore `{}`. | |
We ignore `{}` and `{:?}` since they can't refer to anything |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -1,5 +1,5 @@ | |||
#![warn(clippy::single_component_path_imports)] | |||
#![allow(unused_imports)] | |||
#![allow(unused_imports, clippy::literal_string_with_formatting_args)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is no longer needed since 4
in {4}
isn't a valid identifier
Same for other tests. Could you remove them to make sure those FPs are resolved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let me go through them. 👍
ade93b2
to
3c30d61
Compare
@@ -3,6 +3,7 @@ | |||
//@[edition2021] edition:2021 | |||
|
|||
#![warn(clippy::uninlined_format_args)] | |||
#![allow(clippy::literal_string_with_formatting_args)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one uses panic!("p4 {var}");
, so we need to allow the lint.
3c30d61
to
0930c94
Compare
…formatting argument is passed
0930c94
to
cfc6444
Compare
Removed the |
literal_string_with_formatting_arg
lintliteral_string_with_formatting_args
lint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version looks good to me. I've created an FCP: https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/FCP.20.60literal_string_with_formatting_args.60.20rust-clippy.2313410
Thanks! |
Alright, let's get this merged. Roses are {:#?} |
Fixes #10195.
changelog: Added new [
literal_string_with_formatting_args
]pedantic
lint#13410