Skip to content

Commit

Permalink
Auto merge of #9237 - Alexendoo:useless-format-spans, r=Jarcho
Browse files Browse the repository at this point in the history
Fix `useless_format` spans for `format!("{foo}")`

Fixes #9234

changelog: [`useless_format`]: Fix spans for `format!("{foo}")`

rust-lang/rust#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
  • Loading branch information
bors committed Jul 24, 2022
2 parents 4a5b7e2 + d60d5e8 commit 20e4204
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
19 changes: 2 additions & 17 deletions clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::kw;
use rustc_span::{sym, BytePos, Span};
use rustc_span::{sym, Span};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -85,22 +85,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
ExprKind::MethodCall(path, ..) => path.ident.name.as_str() == "to_string",
_ => false,
};
let sugg = if format_args.format_string_span.contains(value.span) {
// Implicit argument. e.g. `format!("{x}")` span points to `{x}`
let spdata = value.span.data();
let span = Span::new(
spdata.lo + BytePos(1),
spdata.hi - BytePos(1),
spdata.ctxt,
spdata.parent
);
let snip = snippet_with_applicability(cx, span, "..", &mut applicability);
if is_new_string {
snip.into()
} else {
format!("{snip}.to_string()")
}
} else if is_new_string {
let sugg = if is_new_string {
snippet_with_applicability(cx, value.span, "..", &mut applicability).into_owned()
} else {
let sugg = Sugg::hir_with_applicability(cx, value, "<arg>", &mut applicability);
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/format.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ fn main() {
let _ = x.to_string();
let _ = format!("{x:?}"); // Don't lint on debug
let _ = x.to_string();

// Issue #9234
let abc = "abc";
let _ = abc.to_string();
let xx = "xx";
let _ = xx.to_string();
}
6 changes: 6 additions & 0 deletions tests/ui/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@ fn main() {
let _ = format!("{x}");
let _ = format!("{x:?}"); // Don't lint on debug
let _ = format!("{y}", y = x);

// Issue #9234
let abc = "abc";
let _ = format!("{abc}");
let xx = "xx";
let _ = format!("{xx}");
}
14 changes: 13 additions & 1 deletion tests/ui/format.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,17 @@ error: useless use of `format!`
LL | let _ = format!("{y}", y = x);
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `x.to_string()`

error: aborting due to 17 previous errors
error: useless use of `format!`
--> $DIR/format.rs:92:13
|
LL | let _ = format!("{abc}");
| ^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `abc.to_string()`

error: useless use of `format!`
--> $DIR/format.rs:94:13
|
LL | let _ = format!("{xx}");
| ^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `xx.to_string()`

error: aborting due to 19 previous errors

0 comments on commit 20e4204

Please sign in to comment.