False positive in arc_with_non_send_sync

Open
#11,382 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
tooling

Research direction

Start by locating the implementation and tests for the arc_with_non_send_sync lint, then run the provided Rust reproducer to confirm the warning. Trace why the Arc construction is flagged despite the enclosing Worker being marked Send and Sync; done means the reproducer emits no lint while the lint's intended warnings remain covered.

Written by the indexing model from the issue text.

Description

C-bug I-false-positive
Summary

This lint triggers when a non-send type is wrapped in Arc, even if this Arc is then used by the type that is Send.
The reproducer example is messy, but still, probably lint shouldn't trigger there.

Playground link for convenience.

Lint Name

arc_with_non_send_sync

Reproducer

I tried this code:

use std::{sync::Arc, marker::PhantomData, cell::Cell};

#[derive(Default)]
struct ResourceNotSend {
    _not_send: PhantomData<Cell<()>>,
}

impl ResourceNotSend {
    fn threadsafe_method(&self) {
        println!("huh");
    }
}

struct Worker {
    resource: Arc<ResourceNotSend>
}

unsafe impl Send for Worker {}
unsafe impl Sync for Worker {}

impl Worker {
    fn foo(&self) {
        self.resource.threadsafe_method();
    }
}


fn main() {
    let resource = Arc::new(ResourceNotSend::default());
    let worker = Worker { resource: resource.clone() };
    std::thread::spawn(move || {
        worker.foo();
    }).join().unwrap();
}

I saw this happen:

warning: usage of an `Arc` that is not `Send` or `Sync`
  --> src/main.rs:29:20
   |
29 |     let resource = Arc::new(ResourceNotSend::default());
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the trait `Sync` is not implemented for `ResourceNotSend`
   = note: required for `Arc<ResourceNotSend>` to implement `Send` and `Sync`
   = help: consider using an `Rc` instead or wrapping the inner type with a `Mutex`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arc_with_non_send_sync
   = note: `#[warn(clippy::arc_with_non_send_sync)]` on by default

I expected to see this happen:

No lint spawned.

Version
rustc 1.73.0-nightly (08d00b40a 2023-08-09)
binary: rustc
commit-hash: 08d00b40aef2017fe6dba3ff7d6476efa0c10888
commit-date: 2023-08-09
host: aarch64-apple-darwin
release: 1.73.0-nightly
LLVM version: 17.0.0
Additional Labels

No response

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

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.

More from rust-lang/rust-clippy

All issues in rust-lang/rust-clippy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.