Skip to content

Commit

Permalink
Auto merge of #93377 - flip1995:beta-backport, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
[beta] Clippy: Handle implicit named arguments in `useless_format`

Closes #92938

This backports a Clippy fix to beta, that was already backported for 1.58.1 in #93110
  • Loading branch information
bors committed Jan 27, 2022
2 parents 436b81f + b57d411 commit 28c8a34
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/tools/clippy/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, Span};
use rustc_span::{sym, BytePos, Span};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -81,7 +81,22 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
ExprKind::MethodCall(path, ..) => path.ident.name.as_str() == "to_string",
_ => false,
};
let sugg = if is_new_string {
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 {
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 src/tools/clippy/tests/ui/format.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ fn main() {
let _s: String = (&*v.join("\n")).to_string();

format!("prepend {:+}", "s");

// Issue #8290
let x = "foo";
let _ = x.to_string();
let _ = format!("{x:?}"); // Don't lint on debug
let _ = x.to_string();
}
6 changes: 6 additions & 0 deletions src/tools/clippy/tests/ui/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ fn main() {
let _s: String = format!("{}", &*v.join("\n"));

format!("prepend {:+}", "s");

// Issue #8290
let x = "foo";
let _ = format!("{x}");
let _ = format!("{x:?}"); // Don't lint on debug
let _ = format!("{y}", y = x);
}
14 changes: 13 additions & 1 deletion src/tools/clippy/tests/ui/format.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,17 @@ error: useless use of `format!`
LL | let _s: String = format!("{}", &*v.join("/n"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `(&*v.join("/n")).to_string()`

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

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

error: aborting due to 17 previous errors

0 comments on commit 28c8a34

Please sign in to comment.