Skip to content

Commit a25cbd5

Browse files
authored
Improve needless_pass_by_value suggestion (#13880)
Fixes #13744. A simple check to check if the type is an `Option` allows to improve the suggestion. changelog: Improve `needless_pass_by_value` suggestion
2 parents 819f3c7 + c8e47f9 commit a25cbd5

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
@@ -279,10 +279,18 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
279279
}
280280
}
281281

282+
let suggestion = if is_type_diagnostic_item(cx, ty, sym::Option)
283+
&& let snip = snippet(cx, input.span, "_")
284+
&& let Some((first, rest)) = snip.split_once('<')
285+
{
286+
format!("{first}<&{rest}")
287+
} else {
288+
format!("&{}", snippet(cx, input.span, "_"))
289+
};
282290
diag.span_suggestion(
283291
input.span,
284292
"consider taking a reference instead",
285-
format!("&{}", snippet(cx, input.span, "_")),
293+
suggestion,
286294
Applicability::MaybeIncorrect,
287295
);
288296
};

tests/ui/needless_pass_by_value.rs

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

190190
fn prefix_test(_unused_with_prefix: Obj) {}
191191

192+
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13744>.
193+
// It's more idiomatic to write `Option<&T>` rather than `&Option<T>`.
194+
fn option_inner_ref(x: Option<String>) {
195+
//~^ ERROR: this argument is passed by value, but not consumed in the function body
196+
assert!(x.is_some());
197+
}
198+
192199
fn main() {
193200
// This should not cause an ICE either
194201
// 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:58: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:73:24
@@ -179,5 +179,11 @@ error: this argument is passed by value, but not consumed in the function body
179179
LL | fn more_fun(items: impl Club<'static, i32>) {}
180180
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>`
181181

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

0 commit comments

Comments
 (0)