Skip to content

Commit

Permalink
Auto merge of #12609 - Alexendoo:arc-with-non-send-sync-message, r=Ja…
Browse files Browse the repository at this point in the history
…rcho

Reword `arc_with_non_send_sync` note and help messages

Addresses rust-lang/rust-clippy#12608 (comment)

Makes the note more concise and reframes the `Rc` suggestion around whether it crosses threads currently due to a manual `Send`/`Sync` impl or may do in the future

changelog: none
  • Loading branch information
bors committed Apr 4, 2024
2 parents 9725c4a + 9d4a368 commit a73e751
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
26 changes: 11 additions & 15 deletions clippy_lints/src/arc_with_non_send_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ impl<'tcx> LateLintPass<'tcx> for ArcWithNonSendSync {
&& let Some(send) = cx.tcx.get_diagnostic_item(sym::Send)
&& let Some(sync) = cx.tcx.lang_items().sync_trait()
&& let [is_send, is_sync] = [send, sync].map(|id| implements_trait(cx, arg_ty, id, &[]))
&& !(is_send && is_sync)
&& let reason = match (is_send, is_sync) {
(false, false) => "neither `Send` nor `Sync`",
(false, true) => "not `Send`",
(true, false) => "not `Sync`",
_ => return,
}
&& !is_from_proc_macro(cx, expr)
{
span_lint_and_then(
Expand All @@ -66,21 +71,12 @@ impl<'tcx> LateLintPass<'tcx> for ArcWithNonSendSync {
"usage of an `Arc` that is not `Send` and `Sync`",
|diag| {
with_forced_trimmed_paths!({
diag.note(format!("`Arc<{arg_ty}>` is not `Send` and `Sync` as:"));

if !is_send {
diag.note(format!("- the trait `Send` is not implemented for `{arg_ty}`"));
}
if !is_sync {
diag.note(format!("- the trait `Sync` is not implemented for `{arg_ty}`"));
}

diag.help("consider using an `Rc` instead. `Arc` does not provide benefits for non `Send` and `Sync` types");

diag.note("if you intend to use `Arc` with `Send` and `Sync` traits");

diag.note(format!(
"wrap the inner type with a `Mutex` or implement `Send` and `Sync` for `{arg_ty}`"
"`Arc<{arg_ty}>` is not `Send` and `Sync` as `{arg_ty}` is {reason}"
));
diag.help("if the `Arc` will not used be across threads replace it with an `Rc`");
diag.help(format!(
"otherwise make `{arg_ty}` `Send` and `Sync` or consider a wrapper type such as `Mutex`"
));
});
},
Expand Down
7 changes: 0 additions & 7 deletions tests/ui/arc_with_non_send_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ fn main() {
let _ = Arc::new(42);

let _ = Arc::new(RefCell::new(42));
//~^ ERROR: usage of an `Arc` that is not `Send` and `Sync`
//~| NOTE: the trait `Sync` is not implemented for `RefCell<i32>`

let mutex = Mutex::new(1);
let _ = Arc::new(mutex.lock().unwrap());
//~^ ERROR: usage of an `Arc` that is not `Send` and `Sync`
//~| NOTE: the trait `Send` is not implemented for `MutexGuard<'_, i32>`

let _ = Arc::new(&42 as *const i32);
//~^ ERROR: usage of an `Arc` that is not `Send` and `Sync`
//~| NOTE: the trait `Send` is not implemented for `*const i32`
//~| NOTE: the trait `Sync` is not implemented for `*const i32`
}
29 changes: 11 additions & 18 deletions tests/ui/arc_with_non_send_sync.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,31 @@ error: usage of an `Arc` that is not `Send` and `Sync`
LL | let _ = Arc::new(RefCell::new(42));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `Arc<RefCell<i32>>` is not `Send` and `Sync` as:
= note: - the trait `Sync` is not implemented for `RefCell<i32>`
= help: consider using an `Rc` instead. `Arc` does not provide benefits for non `Send` and `Sync` types
= note: if you intend to use `Arc` with `Send` and `Sync` traits
= note: wrap the inner type with a `Mutex` or implement `Send` and `Sync` for `RefCell<i32>`
= note: `Arc<RefCell<i32>>` is not `Send` and `Sync` as `RefCell<i32>` is not `Sync`
= help: if the `Arc` will not used be across threads replace it with an `Rc`
= help: otherwise make `RefCell<i32>` `Send` and `Sync` or consider a wrapper type such as `Mutex`
= note: `-D clippy::arc-with-non-send-sync` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::arc_with_non_send_sync)]`

error: usage of an `Arc` that is not `Send` and `Sync`
--> tests/ui/arc_with_non_send_sync.rs:40:13
--> tests/ui/arc_with_non_send_sync.rs:38:13
|
LL | let _ = Arc::new(mutex.lock().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `Arc<MutexGuard<'_, i32>>` is not `Send` and `Sync` as:
= note: - the trait `Send` is not implemented for `MutexGuard<'_, i32>`
= help: consider using an `Rc` instead. `Arc` does not provide benefits for non `Send` and `Sync` types
= note: if you intend to use `Arc` with `Send` and `Sync` traits
= note: wrap the inner type with a `Mutex` or implement `Send` and `Sync` for `MutexGuard<'_, i32>`
= note: `Arc<MutexGuard<'_, i32>>` is not `Send` and `Sync` as `MutexGuard<'_, i32>` is not `Send`
= help: if the `Arc` will not used be across threads replace it with an `Rc`
= help: otherwise make `MutexGuard<'_, i32>` `Send` and `Sync` or consider a wrapper type such as `Mutex`

error: usage of an `Arc` that is not `Send` and `Sync`
--> tests/ui/arc_with_non_send_sync.rs:44:13
--> tests/ui/arc_with_non_send_sync.rs:40:13
|
LL | let _ = Arc::new(&42 as *const i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `Arc<*const i32>` is not `Send` and `Sync` as:
= note: - the trait `Send` is not implemented for `*const i32`
= note: - the trait `Sync` is not implemented for `*const i32`
= help: consider using an `Rc` instead. `Arc` does not provide benefits for non `Send` and `Sync` types
= note: if you intend to use `Arc` with `Send` and `Sync` traits
= note: wrap the inner type with a `Mutex` or implement `Send` and `Sync` for `*const i32`
= note: `Arc<*const i32>` is not `Send` and `Sync` as `*const i32` is neither `Send` nor `Sync`
= help: if the `Arc` will not used be across threads replace it with an `Rc`
= help: otherwise make `*const i32` `Send` and `Sync` or consider a wrapper type such as `Mutex`

error: aborting due to 3 previous errors

0 comments on commit a73e751

Please sign in to comment.