From 79013f813dde402c9d032dbf189f33221ed33c64 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 26 Dec 2024 12:00:57 +0100 Subject: [PATCH 1/2] Improve `needless_pass_by_value` suggestion --- clippy_lints/src/needless_pass_by_value.rs | 10 +++++++++- tests/ui/needless_pass_by_value.rs | 7 +++++++ tests/ui/needless_pass_by_value.stderr | 10 ++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 380cc380ad0f..f5652e7b832d 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -279,10 +279,18 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { } } + let suggestion = if is_type_diagnostic_item(cx, ty, sym::Option) + && let snip = snippet(cx, input.span, "_") + && let Some((first, rest)) = snip.split_once('<') + { + format!("{first}<&{rest}") + } else { + format!("&{}", snippet(cx, input.span, "_")) + }; diag.span_suggestion( input.span, "consider taking a reference instead", - format!("&{}", snippet(cx, input.span, "_")), + suggestion, Applicability::MaybeIncorrect, ); }; diff --git a/tests/ui/needless_pass_by_value.rs b/tests/ui/needless_pass_by_value.rs index 885fb409417b..adea373fd55a 100644 --- a/tests/ui/needless_pass_by_value.rs +++ b/tests/ui/needless_pass_by_value.rs @@ -189,6 +189,13 @@ struct Obj(String); fn prefix_test(_unused_with_prefix: Obj) {} +// Regression test for . +// It's more idiomatic to write `Option<&T>` rather than `&Option`. +fn option_inner_ref(x: Option) { + //~^ ERROR: this argument is passed by value, but not consumed in the function body + assert!(x.is_some()); +} + fn main() { // This should not cause an ICE either // https://github.com/rust-lang/rust-clippy/issues/3144 diff --git a/tests/ui/needless_pass_by_value.stderr b/tests/ui/needless_pass_by_value.stderr index 4ac4fdce972d..7aea725d50ba 100644 --- a/tests/ui/needless_pass_by_value.stderr +++ b/tests/ui/needless_pass_by_value.stderr @@ -29,7 +29,7 @@ error: this argument is passed by value, but not consumed in the function body --> tests/ui/needless_pass_by_value.rs:58:18 | LL | fn test_match(x: Option>, y: Option>) { - | ^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&Option>` + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&Option>` error: this argument is passed by value, but not consumed in the function body --> tests/ui/needless_pass_by_value.rs:73:24 @@ -179,5 +179,11 @@ error: this argument is passed by value, but not consumed in the function body LL | fn more_fun(items: impl Club<'static, i32>) {} | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>` -error: aborting due to 22 previous errors +error: this argument is passed by value, but not consumed in the function body + --> tests/ui/needless_pass_by_value.rs:187:24 + | +LL | fn option_inner_ref(x: Option) { + | ^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&String>` + +error: aborting due to 23 previous errors From c8e47f9a72b0c3009e17b4fb66667931ac335026 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 8 Mar 2025 21:14:11 +0100 Subject: [PATCH 2/2] Update `needless_pass_by_value` stderr --- tests/ui/needless_pass_by_value.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/needless_pass_by_value.stderr b/tests/ui/needless_pass_by_value.stderr index 7aea725d50ba..987bfc4affc5 100644 --- a/tests/ui/needless_pass_by_value.stderr +++ b/tests/ui/needless_pass_by_value.stderr @@ -180,7 +180,7 @@ LL | fn more_fun(items: impl Club<'static, i32>) {} | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>` error: this argument is passed by value, but not consumed in the function body - --> tests/ui/needless_pass_by_value.rs:187:24 + --> tests/ui/needless_pass_by_value.rs:194:24 | LL | fn option_inner_ref(x: Option) { | ^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&String>`