Skip to content

Commit 652840b

Browse files
tesujiflip1995
authored andcommitted
Re-add false positive check
1 parent 413eb5b commit 652840b

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

clippy_lints/src/format.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
9090
if let PatKind::Tuple(ref pats, None) = arms[0].pats[0].node;
9191
if pats.len() == 1;
9292
then {
93+
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pats[0]));
94+
if ty.sty != rustc::ty::Str && !match_type(cx, ty, &paths::STRING) {
95+
return None;
96+
}
9397
if let ExprKind::Lit(ref lit) = format_args.node {
9498
if let LitKind::Str(ref s, _) = lit.node {
9599
return Some(format!("{:?}.to_string()", s.as_str()));
@@ -100,6 +104,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
100104
if path.ident.name == sym!(to_string) {
101105
return Some(format!("{}", snip));
102106
}
107+
} else if let ExprKind::Binary(..) = format_args.node {
108+
return Some(format!("{}", snip));
103109
}
104110
return Some(format!("{}.to_string()", snip));
105111
}

tests/ui/format.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@ fn main() {
6060
42.to_string();
6161
let x = std::path::PathBuf::from("/bar/foo/qux");
6262
x.display().to_string();
63+
64+
// False positive
65+
let a = "foo".to_string();
66+
let _ = Some(a + "bar");
6367
}

tests/ui/format.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ fn main() {
6363
format!("{}", 42.to_string());
6464
let x = std::path::PathBuf::from("/bar/foo/qux");
6565
format!("{}", x.display().to_string());
66+
67+
// False positive
68+
let a = "foo".to_string();
69+
let _ = Some(format!("{}", a + "bar"));
6670
}

tests/ui/format.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,11 @@ error: useless use of `format!`
7575
LL | format!("{}", x.display().to_string());
7676
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `x.display().to_string();`
7777

78-
error: aborting due to 12 previous errors
78+
error: useless use of `format!`
79+
--> $DIR/format.rs:69:18
80+
|
81+
LL | let _ = Some(format!("{}", a + "bar"));
82+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `a + "bar"`
83+
84+
error: aborting due to 13 previous errors
7985

0 commit comments

Comments
 (0)