From cb86c57c5f9c0fc5e5f736bbd57f95df204e00e1 Mon Sep 17 00:00:00 2001 From: sinkuu Date: Tue, 21 Feb 2017 19:03:50 +0900 Subject: [PATCH] Integrate suggestion spans --- clippy_lints/src/needless_pass_by_value.rs | 20 +++++++++----------- tests/ui/needless_pass_by_value.stderr | 3 +-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 4666cb575efd..bc5ecd653c1e 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -132,27 +132,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { db.span_suggestion(input.span, &format!("consider changing the type to `{}`", slice_ty), slice_ty); - return; + return; // `Vec` and `String` cannot be destructured - no need for `*` suggestion }} if match_type(cx, ty, &paths::STRING) { db.span_suggestion(input.span, "consider changing the type to `&str`", "&str".to_string()); - } else { - db.span_suggestion(input.span, - "consider taking a reference instead", - format!("&{}", snippet(cx, input.span, "_"))); + return; } - // Suggests adding `*` to dereference the added reference if needed. - if let Some(spans) = spans_need_deref.get(&defid) { - let mut spans: Vec<_> = spans.iter().cloned() - .map(|span| (span, format!("*{}", snippet(cx, span, "")))) - .collect(); + let mut spans = vec![(input.span, format!("&{}", snippet(cx, input.span, "_")))]; + + // Suggests adding `*` to dereference the added reference. + if let Some(deref_span) = spans_need_deref.get(&defid) { + spans.extend(deref_span.iter().cloned() + .map(|span| (span, format!("*{}", snippet(cx, span, ""))))); spans.sort_by_key(|&(span, _)| span); - multispan_sugg(db, "...and dereference it here".to_string(), spans); } + multispan_sugg(db, "consider taking a reference instead".to_string(), spans); }; span_lint_and_then(cx, diff --git a/tests/ui/needless_pass_by_value.stderr b/tests/ui/needless_pass_by_value.stderr index ded9e0aa731d..88a424a9696a 100644 --- a/tests/ui/needless_pass_by_value.stderr +++ b/tests/ui/needless_pass_by_value.stderr @@ -47,7 +47,6 @@ error: this argument is passed by value, but not consumed in the function body | help: consider taking a reference instead | fn test_match(x: &Option>, y: Option>) { -help: ...and dereference it here | match *x { error: this argument is passed by value, but not consumed in the function body @@ -67,7 +66,7 @@ error: this argument is passed by value, but not consumed in the function body | help: consider taking a reference instead | fn test_destructure(x: Wrapper, y: &Wrapper, z: Wrapper) { -help: ...and dereference it here + | let Wrapper(s) = z; // moved | let Wrapper(ref t) = *y; // not moved | let Wrapper(_) = *y; // still not moved