rust-lang / rust-lang/rust-clippy
False positive/invalid suggestion from needless_pass_by_value when capturing Copy fields in 2021
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Lint name: needless_pass_by_value
This example is minimized from a real codebase where "Make Options Copy" is not possible. A non-Copy field makes the help message go away, but isn't required to reproduce this bug.
struct Options {
a: i32,
}
fn main() {
let options = Options { a: 1 };
launch_thread(options);
}
fn launch_thread(options: Options) {
std::thread::spawn(move || {
println!("{}", options.a);
});
}
error: this argument is passed by value, but not consumed in the function body
--> src/main.rs:10:27
|
10 | fn launch_thread(options: Options) {
| ^^^^^^^ help: consider taking a reference instead: `&Options`
|
= note: `-D clippy::needless-pass-by-value` implied by `-D clippy::pedantic`
help: consider marking this type as `Copy`
--> src/main.rs:1:1
|
1 | / struct Options {
2 | | a: i32,
3 | | }
| |_^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
Following the suggestion leads to
error[E0621]: explicit lifetime required in the type of `options`
--> src/main.rs:11:5
|
11 | std::thread::spawn(move || {
| ^^^^^^^^^^^^^^^^^^ lifetime `'static` required
Meta
Rust version (rustc -Vv):
rustc 1.56.0 (09c42c458 2021-10-18)
binary: rustc
commit-hash: 09c42c45858d5f3aedfa670698275303a3d19afa
commit-date: 2021-10-18
host: x86_64-unknown-linux-gnu
release: 1.56.0
LLVM version: 13.0.0
@rustbot label +I-suggestion-causes-error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report using the example in src/main.rs and the needless_pass_by_value lint. Check how the lint handles values captured by a spawned closure; done when its suggestion no longer leads to the reported lifetime error and the regression is covered by the relevant lint test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100