diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index b329bc3d099..f99dd335fd2 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -514,8 +514,7 @@ impl LateLintPass for Pass { let self_ty = cx.tcx.expr_ty_adjusted(&args[0]); if args.len() == 1 && name.node.as_str() == "clone" { - lint_clone_on_copy(cx, expr); - lint_clone_double_ref(cx, expr, &args[0], self_ty); + lint_clone_on_copy(cx, expr, &args[0], self_ty); } match self_ty.sty { @@ -703,19 +702,11 @@ fn lint_or_fun_call(cx: &LateContext, expr: &hir::Expr, name: &str, args: &[P Sugg<'a> { pub fn maybe_par(self) -> Self { match self { Sugg::NonParen(..) => self, - Sugg::MaybeParen(sugg) | Sugg::BinOp(_, sugg) => Sugg::NonParen(format!("({})", sugg).into()), + // (x) and (x).y() both don't need additional parens + Sugg::MaybeParen(sugg) => if sugg.starts_with('(') && sugg.ends_with(')') { + Sugg::MaybeParen(sugg) + } else { + Sugg::NonParen(format!("({})", sugg).into()) + }, + Sugg::BinOp(_, sugg) => Sugg::NonParen(format!("({})", sugg).into()), } } } diff --git a/tests/compile-fail/methods.rs b/tests/compile-fail/methods.rs index c3f18bef462..7235bad11bf 100644 --- a/tests/compile-fail/methods.rs +++ b/tests/compile-fail/methods.rs @@ -435,13 +435,22 @@ fn use_extend_from_slice() { fn clone_on_copy() { 42.clone(); //~ERROR using `clone` on a `Copy` type + //~| HELP try removing the `clone` call + //~| SUGGESTION 42 vec![1].clone(); // ok, not a Copy type Some(vec![1]).clone(); // ok, not a Copy type + (&42).clone(); //~ERROR using `clone` on a `Copy` type + //~| HELP try dereferencing it + //~| SUGGESTION *(&42) } fn clone_on_copy_generic(t: T) { t.clone(); //~ERROR using `clone` on a `Copy` type + //~| HELP try removing the `clone` call + //~| SUGGESTION t Some(t).clone(); //~ERROR using `clone` on a `Copy` type + //~| HELP try removing the `clone` call + //~| SUGGESTION Some(t) } fn clone_on_double_ref() { @@ -450,7 +459,6 @@ fn clone_on_double_ref() { let z: &Vec<_> = y.clone(); //~ERROR using `clone` on a double //~| HELP try dereferencing it //~| SUGGESTION let z: &Vec<_> = (*y).clone(); - //~^^^ERROR using `clone` on a `Copy` type println!("{:p} {:p}",*y, z); }