mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-21 11:23:03 +00:00
Auto merge of #6151 - bofh69:master, r=ebroto
Preserve raw strs for: format!(s) to s.to_string() lint fixes #6142 clippy::useless_format will keep the source's string (after converting {{ and }} to { and }) when suggesting a change from format!() to .to_string() usage. Ie: | let s = format!(r#""hello {{}}""#); | ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `r#""hello {}""#.to_string()` changelog: [`useless_format`]: preserve raw string literals when no arguments to `format!()` are provided.
This commit is contained in:
commit
2bdadd8e7d
@ -1,6 +1,6 @@
|
|||||||
use crate::utils::paths;
|
use crate::utils::paths;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
is_expn_of, is_type_diagnostic_item, last_path_segment, match_def_path, match_function_call, snippet,
|
is_expn_of, is_type_diagnostic_item, last_path_segment, match_def_path, match_function_call, snippet, snippet_opt,
|
||||||
span_lint_and_then,
|
span_lint_and_then,
|
||||||
};
|
};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
@ -132,7 +132,11 @@ fn on_new_v1<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Strin
|
|||||||
then {
|
then {
|
||||||
// `format!("foo")` expansion contains `match () { () => [], }`
|
// `format!("foo")` expansion contains `match () { () => [], }`
|
||||||
if tup.is_empty() {
|
if tup.is_empty() {
|
||||||
return Some(format!("{:?}.to_string()", s.as_str()));
|
if let Some(s_src) = snippet_opt(cx, lit.span) {
|
||||||
|
// Simulate macro expansion, converting {{ and }} to { and }.
|
||||||
|
let s_expand = s_src.replace("{{", "{").replace("}}", "}");
|
||||||
|
return Some(format!("{}.to_string()", s_expand))
|
||||||
|
}
|
||||||
} else if s.as_str().is_empty() {
|
} else if s.as_str().is_empty() {
|
||||||
return on_argumentv1_new(cx, &tup[0], arms);
|
return on_argumentv1_new(cx, &tup[0], arms);
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,8 @@ fn main() {
|
|||||||
"foo".to_string();
|
"foo".to_string();
|
||||||
"{}".to_string();
|
"{}".to_string();
|
||||||
"{} abc {}".to_string();
|
"{} abc {}".to_string();
|
||||||
"foo {}\n\" bar".to_string();
|
r##"foo {}
|
||||||
|
" bar"##.to_string();
|
||||||
|
|
||||||
"foo".to_string();
|
"foo".to_string();
|
||||||
format!("{:?}", "foo"); // Don't warn about `Debug`.
|
format!("{:?}", "foo"); // Don't warn about `Debug`.
|
||||||
|
@ -25,7 +25,13 @@ LL | / format!(
|
|||||||
LL | | r##"foo {{}}
|
LL | | r##"foo {{}}
|
||||||
LL | | " bar"##
|
LL | | " bar"##
|
||||||
LL | | );
|
LL | | );
|
||||||
| |______^ help: consider using `.to_string()`: `"foo {}/n/" bar".to_string();`
|
| |______^
|
||||||
|
|
|
||||||
|
help: consider using `.to_string()`
|
||||||
|
|
|
||||||
|
LL | r##"foo {}
|
||||||
|
LL | " bar"##.to_string();
|
||||||
|
|
|
||||||
|
|
||||||
error: useless use of `format!`
|
error: useless use of `format!`
|
||||||
--> $DIR/format.rs:21:5
|
--> $DIR/format.rs:21:5
|
||||||
|
Loading…
Reference in New Issue
Block a user