-
Notifications
You must be signed in to change notification settings - Fork 1.7k
needless_pass_by_value
false positive when value used in async move
block
#13321
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
Comments
Did you apply both parts of the suggestion?
When I do, I don't get an error: use std::future::Future;
#[derive(Clone, Copy)]
struct MyStruct {
a: i32,
b: i32,
}
fn main() {
let my = MyStruct { a: 1, b: 2 };
let _ = async_use(&my);
}
fn async_use(my: &MyStruct) -> impl Future<Output=i32> + '_ {
async move {
println!("a: {}, b: {}", my.a, my.b);
10i32
}
} |
Yes, i noticed the hint about Also, without |
What do you mean by "without |
I was wrong in my last message. If not for the async, it would be correct to flag this as needless_pass_by_value. With async, the clippy suggests to use reference and make the struct Copy. Using reference requires lifetime hint If using Copy and capturing reference lifetime with @samueltardieu what happens if you modify your example from #13321 (comment) and remove |
Yes, it does work without Maybe you should also close this issue (I can't do it myself). |
Sure, I can close. I didn't do that, because I still can't explain why it's not a false-negative. Consider slightly more complex example
To make clippy happy i will have to make my type Copy this time, since there is no lifetime such as What I am getting at is -- I would expect that the code either conforms with clippy triggering no warnings (in particular please let me know if this makes sense |
The suggestion to implement This is a false positive on the lint since it returns the input by value meaning it's consumed by the function. You could do |
Independently as this FP, I'll open a PR to add a "or" before "consider marking this type as |
Make it clearer that the suggestion is an alternative one `needless_pass_by_value` sometimes suggest marking the concerned type as `Copy`. Adding a `or` before this suggestion makes it clearer that this is not the second part of the original suggestion, but an alternative one. Inspired by a misunderstanding in #13321
Make it clearer that the suggestion is an alternative one `needless_pass_by_value` sometimes suggest marking the concerned type as `Copy`. Adding a `or` before this suggestion makes it clearer that this is not the second part of the original suggestion, but an alternative one. Inspired by a misunderstanding in #13321 changelog: none
Uh oh!
There was an error while loading. Please reload this page.
Summary
needless_pass_by_value
lint is triggered when a value is moved into a function and referenced in async move block.If not for the async, it would be correct to flag this as
needless_pass_by_value
.Lint Name
needless_pass_by_value
Reproducer
I tried this code:
i run
cargo clippy -- -Aclippy::all -Wclippy::needless_pass_by_value -D warnings
I saw this happen:
however, if i follow the advice and make
async_use
take the reference,cargo build
fails withVersion
Additional Labels
No response
The text was updated successfully, but these errors were encountered: