diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-03-11 20:59:03 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2025-03-12 06:57:25 +0100 |
| commit | 35e6057e715b47fa8ceed67fbf53e459c86d571c (patch) | |
| tree | 2e6c5e02d6dd40f68b7cc965f8d6d91aee974033 /tests | |
| parent | 8f280ff813ba638b6121d5f4e0874d38e90eb3b6 (diff) | |
| download | rust-35e6057e715b47fa8ceed67fbf53e459c86d571c.tar.gz rust-35e6057e715b47fa8ceed67fbf53e459c86d571c.zip | |
`needless_pass_by_value`: reference the innermost `Option` content
If types such as `Option<Option<String>>` are not used by value, then `Option<Option<&String>>` will be suggested, instead of `Option<&Option<String>>`.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/needless_pass_by_value.rs | 29 | ||||
| -rw-r--r-- | tests/ui/needless_pass_by_value.stderr | 28 |
2 files changed, 55 insertions, 2 deletions
diff --git a/tests/ui/needless_pass_by_value.rs b/tests/ui/needless_pass_by_value.rs index adea373fd55..aef7fff2870 100644 --- a/tests/ui/needless_pass_by_value.rs +++ b/tests/ui/needless_pass_by_value.rs @@ -196,6 +196,35 @@ fn option_inner_ref(x: Option<String>) { assert!(x.is_some()); } +mod non_standard { + #[derive(Debug)] + pub struct Option<T>(T); +} + +fn non_standard_option(x: non_standard::Option<String>) { + //~^ needless_pass_by_value + dbg!(&x); +} + +fn option_by_name(x: Option<std::option::Option<core::option::Option<non_standard::Option<String>>>>) { + //~^ needless_pass_by_value + dbg!(&x); +} + +type OptStr = Option<String>; + +fn non_option(x: OptStr) { + //~^ needless_pass_by_value + dbg!(&x); +} + +type Opt<T> = Option<T>; + +fn non_option_either(x: Opt<String>) { + //~^ needless_pass_by_value + dbg!(&x); +} + fn main() { // This should not cause an ICE either // https://github.com/rust-lang/rust-clippy/issues/3144 diff --git a/tests/ui/needless_pass_by_value.stderr b/tests/ui/needless_pass_by_value.stderr index 987bfc4affc..fc67e06ca42 100644 --- a/tests/ui/needless_pass_by_value.stderr +++ b/tests/ui/needless_pass_by_value.stderr @@ -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 @@ -185,5 +185,29 @@ error: this argument is passed by value, but not consumed in the function body LL | fn option_inner_ref(x: Option<String>) { | ^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<&String>` -error: aborting due to 23 previous errors +error: this argument is passed by value, but not consumed in the function body + --> tests/ui/needless_pass_by_value.rs:204:27 + | +LL | fn non_standard_option(x: non_standard::Option<String>) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&non_standard::Option<String>` + +error: this argument is passed by value, but not consumed in the function body + --> tests/ui/needless_pass_by_value.rs:209:22 + | +LL | fn option_by_name(x: Option<std::option::Option<core::option::Option<non_standard::Option<String>>>>) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `Option<std::option::Option<core::option::Option<&non_standard::Option<String>>>>` + +error: this argument is passed by value, but not consumed in the function body + --> tests/ui/needless_pass_by_value.rs:216:18 + | +LL | fn non_option(x: OptStr) { + | ^^^^^^ help: consider taking a reference instead: `&OptStr` + +error: this argument is passed by value, but not consumed in the function body + --> tests/ui/needless_pass_by_value.rs:223:25 + | +LL | fn non_option_either(x: Opt<String>) { + | ^^^^^^^^^^^ help: consider taking a reference instead: `&Opt<String>` + +error: aborting due to 27 previous errors |
