From 9d4a36841ae998579a98c919c31dd25fba2b5ee5 Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Mon, 1 Apr 2024 13:18:27 +0000 Subject: [PATCH] Reword `arc_with_non_send_sync` note and help messages --- clippy_lints/src/arc_with_non_send_sync.rs | 26 ++++++++----------- tests/ui/arc_with_non_send_sync.rs | 7 ------ tests/ui/arc_with_non_send_sync.stderr | 29 ++++++++-------------- 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/clippy_lints/src/arc_with_non_send_sync.rs b/clippy_lints/src/arc_with_non_send_sync.rs index 1102c7fb236b5..389338973894b 100644 --- a/clippy_lints/src/arc_with_non_send_sync.rs +++ b/clippy_lints/src/arc_with_non_send_sync.rs @@ -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( @@ -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`" )); }); }, diff --git a/tests/ui/arc_with_non_send_sync.rs b/tests/ui/arc_with_non_send_sync.rs index 349e81912e3c3..c287480bb1fd5 100644 --- a/tests/ui/arc_with_non_send_sync.rs +++ b/tests/ui/arc_with_non_send_sync.rs @@ -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` 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` } diff --git a/tests/ui/arc_with_non_send_sync.stderr b/tests/ui/arc_with_non_send_sync.stderr index d4e6303762042..da363a3ebdd98 100644 --- a/tests/ui/arc_with_non_send_sync.stderr +++ b/tests/ui/arc_with_non_send_sync.stderr @@ -4,38 +4,31 @@ error: usage of an `Arc` that is not `Send` and `Sync` LL | let _ = Arc::new(RefCell::new(42)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: `Arc>` is not `Send` and `Sync` as: - = note: - the trait `Sync` is not implemented for `RefCell` - = 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` + = note: `Arc>` is not `Send` and `Sync` as `RefCell` is not `Sync` + = help: if the `Arc` will not used be across threads replace it with an `Rc` + = help: otherwise make `RefCell` `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>` 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>` 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