Skip to content

Commit 0d27bd8

Browse files
committed
Auto merge of rust-lang#8295 - Jarcho:useless_format_8290, r=giraffate
Handle implicit named arguments in `useless_format` fixes rust-lang#8290 Ideally this would fix the macro parsing code to handle this, but this is a smaller change and easier to back port. changelog: Handle implicit named arguments in `useless_format`
2 parents d61f798 + cb384ff commit 0d27bd8

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

clippy_lints/src/format.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty;
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
1111
use rustc_span::symbol::kw;
12-
use rustc_span::{sym, Span};
12+
use rustc_span::{sym, BytePos, Span};
1313

1414
declare_clippy_lint! {
1515
/// ### What it does
@@ -84,7 +84,22 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
8484
ExprKind::MethodCall(path, ..) => path.ident.name.as_str() == "to_string",
8585
_ => false,
8686
};
87-
let sugg = if is_new_string {
87+
let sugg = if format_args.format_string_span.contains(value.span) {
88+
// Implicit argument. e.g. `format!("{x}")` span points to `{x}`
89+
let spdata = value.span.data();
90+
let span = Span::new(
91+
spdata.lo + BytePos(1),
92+
spdata.hi - BytePos(1),
93+
spdata.ctxt,
94+
spdata.parent
95+
);
96+
let snip = snippet_with_applicability(cx, span, "..", &mut applicability);
97+
if is_new_string {
98+
snip.into()
99+
} else {
100+
format!("{snip}.to_string()")
101+
}
102+
} else if is_new_string {
88103
snippet_with_applicability(cx, value.span, "..", &mut applicability).into_owned()
89104
} else {
90105
let sugg = Sugg::hir_with_applicability(cx, value, "<arg>", &mut applicability);

tests/ui/format.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,10 @@ fn main() {
7373
let _s: String = (&*v.join("\n")).to_string();
7474

7575
format!("prepend {:+}", "s");
76+
77+
// Issue #8290
78+
let x = "foo";
79+
let _ = x.to_string();
80+
let _ = format!("{x:?}"); // Don't lint on debug
81+
let _ = x.to_string();
7682
}

tests/ui/format.rs

+6
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,10 @@ fn main() {
7575
let _s: String = format!("{}", &*v.join("\n"));
7676

7777
format!("prepend {:+}", "s");
78+
79+
// Issue #8290
80+
let x = "foo";
81+
let _ = format!("{x}");
82+
let _ = format!("{x:?}"); // Don't lint on debug
83+
let _ = format!("{y}", y = x);
7884
}

tests/ui/format.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,17 @@ error: useless use of `format!`
9999
LL | let _s: String = format!("{}", &*v.join("/n"));
100100
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `(&*v.join("/n")).to_string()`
101101

102-
error: aborting due to 15 previous errors
102+
error: useless use of `format!`
103+
--> $DIR/format.rs:81:13
104+
|
105+
LL | let _ = format!("{x}");
106+
| ^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.to_string()`
107+
108+
error: useless use of `format!`
109+
--> $DIR/format.rs:83:13
110+
|
111+
LL | let _ = format!("{y}", y = x);
112+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.to_string()`
113+
114+
error: aborting due to 17 previous errors
103115

0 commit comments

Comments
 (0)