Skip to content

Commit f717a77

Browse files
committed
Re-add false positive check
1 parent ce2d292 commit f717a77

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

clippy_lints/src/format.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::utils::paths;
2-
use crate::utils::{is_expn_of, last_path_segment, match_def_path, resolve_node, snippet, span_lint_and_then};
2+
use crate::utils::{
3+
is_expn_of, last_path_segment, match_def_path, match_type, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty,
4+
};
35
use if_chain::if_chain;
46
use rustc::hir::*;
57
use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
@@ -87,6 +89,10 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
8789
if let PatKind::Tuple(ref pats, None) = arms[0].pats[0].node;
8890
if pats.len() == 1;
8991
then {
92+
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pats[0]));
93+
if ty.sty != rustc::ty::Str && !match_type(cx, ty, &paths::STRING) {
94+
return None;
95+
}
9096
if let ExprKind::Lit(ref lit) = format_args.node {
9197
if let LitKind::Str(ref s, _) = lit.node {
9298
return Some(format!("{:?}.to_string()", s.as_str()));
@@ -97,6 +103,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
97103
if path.ident.name == sym!(to_string) {
98104
return Some(format!("{}", snip));
99105
}
106+
} else if let ExprKind::Binary(..) = format_args.node {
107+
return Some(format!("{}", snip));
100108
}
101109
return Some(format!("{}.to_string()", snip));
102110
}

tests/ui/format.fixed

+4
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

+4
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

+7-1
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)