Skip to content
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

Improve needless_pass_by_value suggestion #13880

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -275,10 +275,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 @@ -182,6 +182,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:56: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>>`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you run this several times?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that. Is it better to propose for the inner most T to be behind a reference or just the direct top child?


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

error: aborting due to 23 previous errors

Loading