mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
Improve needless_pass_by_value suggestion
This commit is contained in:
@@ -279,10 +279,18 @@ fn check_fn(
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -189,6 +189,13 @@ fn is_sync<T>(_: T)
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
@@ -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<String>) {
|
||||
| ^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&String>`
|
||||
|
||||
error: aborting due to 23 previous errors
|
||||
|
||||
|
||||
Reference in New Issue
Block a user