Skip to content

Commit 408709d

Browse files
Jarchopietroalbini
authored andcommitted
Handle implicit named arguments in useless_format
1 parent a14ae6f commit 408709d

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

Diff for: src/tools/clippy/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
@@ -80,7 +80,22 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
8080
ExprKind::MethodCall(path, ..) => path.ident.name.as_str() == "to_string",
8181
_ => false,
8282
};
83-
let sugg = if is_new_string {
83+
let sugg = if format_args.format_string_span.contains(value.span) {
84+
// Implicit argument. e.g. `format!("{x}")` span points to `{x}`
85+
let spdata = value.span.data();
86+
let span = Span::new(
87+
spdata.lo + BytePos(1),
88+
spdata.hi - BytePos(1),
89+
spdata.ctxt,
90+
spdata.parent
91+
);
92+
let snip = snippet_with_applicability(cx, span, "..", &mut applicability);
93+
if is_new_string {
94+
snip.into()
95+
} else {
96+
format!("{}.to_string()", snip)
97+
}
98+
} else if is_new_string {
8499
snippet_with_applicability(cx, value.span, "..", &mut applicability).into_owned()
85100
} else {
86101
let sugg = Sugg::hir_with_applicability(cx, value, "<arg>", &mut applicability);

Diff for: src/tools/clippy/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
}

Diff for: src/tools/clippy/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
}

Diff for: src/tools/clippy/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)