rust-lang / rust-lang/rust-clippy

False positive/invalid suggestion from needless_pass_by_value when capturing Copy fields in 2021

Open
#7,879 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.