Skip to content

Commit 2874288

Browse files
Improve needless_pass_by_value suggestion
1 parent 968669b commit 2874288

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,18 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
275275
}
276276
}
277277

278+
let suggestion = if is_type_diagnostic_item(cx, ty, sym::Option)
279+
&& let snip = snippet(cx, input.span, "_")
280+
&& let Some((first, rest)) = snip.split_once('<')
281+
{
282+
format!("{first}<&{rest}")
283+
} else {
284+
format!("&{}", snippet(cx, input.span, "_"))
285+
};
278286
diag.span_suggestion(
279287
input.span,
280288
"consider taking a reference instead",
281-
format!("&{}", snippet(cx, input.span, "_")),
289+
suggestion,
282290
Applicability::MaybeIncorrect,
283291
);
284292
};

tests/ui/needless_pass_by_value.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ struct Obj(String);
182182

183183
fn prefix_test(_unused_with_prefix: Obj) {}
184184

185+
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13744>.
186+
// It's more idiomatic to write `Option<&T>` rather than `&Option<T>`.
187+
fn option_inner_ref(x: Option<String>) {
188+
//~^ ERROR: this argument is passed by value, but not consumed in the function body
189+
assert!(x.is_some());
190+
}
191+
185192
fn main() {
186193
// This should not cause an ICE either
187194
// https://github.com/rust-lang/rust-clippy/issues/3144

tests/ui/needless_pass_by_value.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ error: this argument is passed by value, but not consumed in the function body
2929
--> tests/ui/needless_pass_by_value.rs:56:18
3030
|
3131
LL | fn test_match(x: Option<Option<String>>, y: Option<Option<String>>) {
32-
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&Option<Option<String>>`
32+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&Option<String>>`
3333

3434
error: this argument is passed by value, but not consumed in the function body
3535
--> tests/ui/needless_pass_by_value.rs:70:24
@@ -175,5 +175,11 @@ error: this argument is passed by value, but not consumed in the function body
175175
LL | fn more_fun(items: impl Club<'static, i32>) {}
176176
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>`
177177

178-
error: aborting due to 22 previous errors
178+
error: this argument is passed by value, but not consumed in the function body
179+
--> tests/ui/needless_pass_by_value.rs:187:24
180+
|
181+
LL | fn option_inner_ref(x: Option<String>) {
182+
| ^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&String>`
183+
184+
error: aborting due to 23 previous errors
179185

0 commit comments

Comments
 (0)