From 2a30c8a194deedde2ff2326180cfbd38303c2bdb Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 18 Jan 2018 14:15:41 +0530 Subject: [PATCH] needless_pass_by_value: Add suggestion for implementing Copy (fixes #2222) --- clippy_lints/src/needless_pass_by_value.rs | 7 +++++++ tests/ui/needless_pass_by_value.stderr | 24 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index f404d343439..92dc330779f 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -202,6 +202,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { // Dereference suggestion let sugg = |db: &mut DiagnosticBuilder| { + if let ty::TypeVariants::TyAdt(ref def, ..) = ty.sty { + if let Some(span) = cx.tcx.hir.span_if_local(def.did) { + // FIXME (#2374) Restrict this to types which can impl Copy + db.span_help(span, "consider marking this type as Copy if possible"); + } + } + let deref_span = spans_need_deref.get(&canonical_id); if_chain! { if match_type(cx, ty, &paths::VEC); diff --git a/tests/ui/needless_pass_by_value.stderr b/tests/ui/needless_pass_by_value.stderr index 33bda7d9872..441a9095b6a 100644 --- a/tests/ui/needless_pass_by_value.stderr +++ b/tests/ui/needless_pass_by_value.stderr @@ -17,6 +17,12 @@ error: this argument is passed by value, but not consumed in the function body | 26 | fn bar(x: String, y: Wrapper) { | ^^^^^^^ help: consider taking a reference instead: `&Wrapper` + | +help: consider marking this type as Copy if possible + --> $DIR/needless_pass_by_value.rs:24:1 + | +24 | struct Wrapper(String); + | ^^^^^^^^^^^^^^^^^^^^^^^ error: this argument is passed by value, but not consumed in the function body --> $DIR/needless_pass_by_value.rs:32:71 @@ -40,12 +46,24 @@ error: this argument is passed by value, but not consumed in the function body | 57 | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) { | ^^^^^^^ help: consider taking a reference instead: `&Wrapper` + | +help: consider marking this type as Copy if possible + --> $DIR/needless_pass_by_value.rs:24:1 + | +24 | struct Wrapper(String); + | ^^^^^^^^^^^^^^^^^^^^^^^ error: this argument is passed by value, but not consumed in the function body --> $DIR/needless_pass_by_value.rs:57:36 | 57 | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) { | ^^^^^^^ + | +help: consider marking this type as Copy if possible + --> $DIR/needless_pass_by_value.rs:24:1 + | +24 | struct Wrapper(String); + | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead | 57 | fn test_destructure(x: Wrapper, y: &Wrapper, z: Wrapper) { @@ -123,6 +141,12 @@ error: this argument is passed by value, but not consumed in the function body | 101 | _s: Self, | ^^^^ help: consider taking a reference instead: `&Self` + | +help: consider marking this type as Copy if possible + --> $DIR/needless_pass_by_value.rs:82:1 + | +82 | struct S(T, U); + | ^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 16 previous errors