Skip to content

Improve needless_pass_by_value suggestion #13880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
};
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ struct Obj(String);

fn prefix_test(_unused_with_prefix: Obj) {}

// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13744>.
// It's more idiomatic to write `Option<&T>` rather than `&Option<T>`.
fn option_inner_ref(x: Option<String>) {
//~^ 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
Expand Down
10 changes: 8 additions & 2 deletions tests/ui/needless_pass_by_value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>>, y: Option<Option<String>>) {
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&Option<Option<String>>`
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&Option<String>>`

error: this argument is passed by value, but not consumed in the function body
--> tests/ui/needless_pass_by_value.rs:73:24
Expand Down Expand Up @@ -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:194:24
|
LL | fn option_inner_ref(x: Option<String>) {
| ^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&String>`

error: aborting due to 23 previous errors