Skip to content

arc_with_non_send_sync false positive in generic code #11076

Closed
@dtolnay

Description

@dtolnay

Summary

I believe there is nothing wrong with the code in the repro below.

Seems like Clippy wants a T: Send bound but I don't see a reason why that bound would need to be written.

Lint Name

arc_with_non_send_sync

Reproducer

use std::sync::{Arc, Mutex};
use std::thread;

pub struct Collector<T> {
    out: Arc<Mutex<Vec<T>>>,
}

impl<T> Collector<T> {
    pub fn new() -> Self {
        let out = Arc::new(Mutex::new(Vec::new()));
        Collector { out }
    }
}

fn main() {
    let collector = Collector::new();

    // Do Send stuff with it
    let thread = thread::spawn({
        let out = Arc::clone(&collector.out);
        move || out.lock().unwrap().push("success")
    });
    thread.join().unwrap();
    println!("{:?}", Arc::into_inner(collector.out).unwrap().into_inner().unwrap());
}
$ cargo clippy

error: usage of `Arc<T>` where `T` is not `Send` or `Sync`
  --> src/main.rs:10:19
   |
10 |         let out = Arc::new(Mutex::new(Vec::new()));
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: consider using `Rc<T>` instead or wrapping `T` in a std::sync type like `Mutex<T>`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arc_with_non_send_sync
   = note: `#[deny(clippy::arc_with_non_send_sync)]` on by default

Version

rustc 1.72.0-nightly (839e9a6e1 2023-07-02)
binary: rustc
commit-hash: 839e9a6e1210934fd24b15548b811a97c77138fc
commit-date: 2023-07-02
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions