Skip to content

Commit 20e4204

Browse files
committed
Auto merge of rust-lang#9237 - Alexendoo:useless-format-spans, r=Jarcho
Fix `useless_format` spans for `format!("{foo}")` Fixes rust-lang#9234 changelog: [`useless_format`]: Fix spans for `format!("{foo}")` rust-lang#94030 made our workaround unneeded, but by coincidence our test still passed [because `Span::new()` swaps `hi` & `lo` if needed](https://github.com/rust-lang/rust/blob/c32dcbba187d1ee0dbe92dc152cb9c2f3f42900c/compiler/rustc_span/src/span_encoding.rs#L86-L88). So with a single character variable like `format!("{x}")` it still worked
2 parents 4a5b7e2 + d60d5e8 commit 20e4204

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

clippy_lints/src/format.rs

+2-17
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, BytePos, Span};
12+
use rustc_span::{sym, Span};
1313

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

tests/ui/format.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,10 @@ fn main() {
8484
let _ = x.to_string();
8585
let _ = format!("{x:?}"); // Don't lint on debug
8686
let _ = x.to_string();
87+
88+
// Issue #9234
89+
let abc = "abc";
90+
let _ = abc.to_string();
91+
let xx = "xx";
92+
let _ = xx.to_string();
8793
}

tests/ui/format.rs

+6
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ fn main() {
8686
let _ = format!("{x}");
8787
let _ = format!("{x:?}"); // Don't lint on debug
8888
let _ = format!("{y}", y = x);
89+
90+
// Issue #9234
91+
let abc = "abc";
92+
let _ = format!("{abc}");
93+
let xx = "xx";
94+
let _ = format!("{xx}");
8995
}

tests/ui/format.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,17 @@ error: useless use of `format!`
111111
LL | let _ = format!("{y}", y = x);
112112
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.to_string()`
113113

114-
error: aborting due to 17 previous errors
114+
error: useless use of `format!`
115+
--> $DIR/format.rs:92:13
116+
|
117+
LL | let _ = format!("{abc}");
118+
| ^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `abc.to_string()`
119+
120+
error: useless use of `format!`
121+
--> $DIR/format.rs:94:13
122+
|
123+
LL | let _ = format!("{xx}");
124+
| ^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `xx.to_string()`
125+
126+
error: aborting due to 19 previous errors
115127

0 commit comments

Comments
 (0)