Re-add false positive check

This commit is contained in:
Lzu Tao 2019-08-23 15:46:23 +07:00 committed by flip1995
parent 413eb5b946
commit 652840b4f1
No known key found for this signature in database
GPG key ID: 693086869D506637
4 changed files with 21 additions and 1 deletions

View file

@ -90,6 +90,10 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
if let PatKind::Tuple(ref pats, None) = arms[0].pats[0].node; if let PatKind::Tuple(ref pats, None) = arms[0].pats[0].node;
if pats.len() == 1; if pats.len() == 1;
then { then {
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pats[0]));
if ty.sty != rustc::ty::Str && !match_type(cx, ty, &paths::STRING) {
return None;
}
if let ExprKind::Lit(ref lit) = format_args.node { if let ExprKind::Lit(ref lit) = format_args.node {
if let LitKind::Str(ref s, _) = lit.node { if let LitKind::Str(ref s, _) = lit.node {
return Some(format!("{:?}.to_string()", s.as_str())); 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
if path.ident.name == sym!(to_string) { if path.ident.name == sym!(to_string) {
return Some(format!("{}", snip)); return Some(format!("{}", snip));
} }
} else if let ExprKind::Binary(..) = format_args.node {
return Some(format!("{}", snip));
} }
return Some(format!("{}.to_string()", snip)); return Some(format!("{}.to_string()", snip));
} }

View file

@ -60,4 +60,8 @@ fn main() {
42.to_string(); 42.to_string();
let x = std::path::PathBuf::from("/bar/foo/qux"); let x = std::path::PathBuf::from("/bar/foo/qux");
x.display().to_string(); x.display().to_string();
// False positive
let a = "foo".to_string();
let _ = Some(a + "bar");
} }

View file

@ -63,4 +63,8 @@ fn main() {
format!("{}", 42.to_string()); format!("{}", 42.to_string());
let x = std::path::PathBuf::from("/bar/foo/qux"); let x = std::path::PathBuf::from("/bar/foo/qux");
format!("{}", x.display().to_string()); format!("{}", x.display().to_string());
// False positive
let a = "foo".to_string();
let _ = Some(format!("{}", a + "bar"));
} }

View file

@ -75,5 +75,11 @@ error: useless use of `format!`
LL | format!("{}", x.display().to_string()); LL | format!("{}", x.display().to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `x.display().to_string();` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `x.display().to_string();`
error: aborting due to 12 previous errors error: useless use of `format!`
--> $DIR/format.rs:69:18
|
LL | let _ = Some(format!("{}", a + "bar"));
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `a + "bar"`
error: aborting due to 13 previous errors