-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix literal_string_with_formatting_args
lint emitted when it should not
#13953
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
Changes from 2 commits
17f9344
a7fb37c
00104a4
e35330c
277bf08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,24 @@ | ||||||||
#![warn(clippy::literal_string_with_formatting_args)] | ||||||||
#![allow(clippy::unnecessary_literal_unwrap)] | ||||||||
|
||||||||
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13885>. | ||||||||
// It's not supposed to emit the lint in this case (in `assert!` expansion). | ||||||||
fn compiler_macro() { | ||||||||
fn parse(_: &str) -> Result<(), i32> { | ||||||||
unimplemented!() | ||||||||
} | ||||||||
|
||||||||
assert!( | ||||||||
parse( | ||||||||
#[allow(clippy::literal_string_with_formatting_args)] | ||||||||
"foo {:}" | ||||||||
) | ||||||||
.is_err() | ||||||||
); | ||||||||
let value = 0; | ||||||||
assert!(format!("{value}").is_ascii()); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also add another true positive test such as
Suggested change
so that we can check that the lint properly triggers in non-formatting-macro arguments? |
||||||||
|
||||||||
fn main() { | ||||||||
let x: Option<usize> = None; | ||||||||
let y = "hello"; | ||||||||
|
@@ -13,6 +31,7 @@ fn main() { | |||||||
x.expect(r"{y:?} {y:?} "); //~ literal_string_with_formatting_args | ||||||||
x.expect(r"{y:?} y:?}"); //~ literal_string_with_formatting_args | ||||||||
x.expect(r##" {y:?} {y:?} "##); //~ literal_string_with_formatting_args | ||||||||
assert!("{y}".is_ascii()); //~ literal_string_with_formatting_args | ||||||||
// Ensure that it doesn't try to go in the middle of a unicode character. | ||||||||
x.expect("———{:?}"); //~ literal_string_with_formatting_args | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13885>. | ||
// The `dbg` macro generates a literal with the name of the current file, so | ||
// we need to ensure the lint is not emitted in this case. | ||
|
||
#![crate_name = "foo"] | ||
#![allow(unused)] | ||
#![warn(clippy::literal_string_with_formatting_args)] | ||
|
||
fn another_bad() { | ||
let literal_string_with_formatting_args = 0; | ||
dbg!("something"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised that the
Odd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see what's happening. Clippy sets
Maybe add to the comment that clippy sets this flag and only with that flag does it have a non-macro span which gets around the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into it but couldn't figure out why it didn't work. Thanks a lot for the detailed explanations! Adding it as a comment. |
||
} | ||
|
||
fn main() {} |
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 will add false negatives for escaped strings (eg
"{foo} \n bar"
) and similar, but as a temporary solution to fix FPs it's probably fineThere 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.
It might be worth trying to use
is_from_proc_macro
here instead, because that also solves the same issue of a node not matching up with its snippet. It seems like it should also fix the FP while not running into this FN since it doesn't actually check the string contents.(If we do go with the current approach, a small nit: it might be a good use for the newer
SpanRange
API withif !expr.span.check_source_text(|snippet| snippet.contains(fmt_str)) { return; }
. Slightly simpler and avoids allocating a temporary string)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.
Not sure why
is_from_proc_macro
didn't work last time I tried but this time it behaves as expected, so switching back to it.