Skip to content

Commit a73e751

Browse files
committed
Auto merge of rust-lang#12609 - Alexendoo:arc-with-non-send-sync-message, r=Jarcho
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
2 parents 9725c4a + 9d4a368 commit a73e751

File tree

3 files changed

+22
-40
lines changed

3 files changed

+22
-40
lines changed

clippy_lints/src/arc_with_non_send_sync.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ impl<'tcx> LateLintPass<'tcx> for ArcWithNonSendSync {
5656
&& let Some(send) = cx.tcx.get_diagnostic_item(sym::Send)
5757
&& let Some(sync) = cx.tcx.lang_items().sync_trait()
5858
&& let [is_send, is_sync] = [send, sync].map(|id| implements_trait(cx, arg_ty, id, &[]))
59-
&& !(is_send && is_sync)
59+
&& let reason = match (is_send, is_sync) {
60+
(false, false) => "neither `Send` nor `Sync`",
61+
(false, true) => "not `Send`",
62+
(true, false) => "not `Sync`",
63+
_ => return,
64+
}
6065
&& !is_from_proc_macro(cx, expr)
6166
{
6267
span_lint_and_then(
@@ -66,21 +71,12 @@ impl<'tcx> LateLintPass<'tcx> for ArcWithNonSendSync {
6671
"usage of an `Arc` that is not `Send` and `Sync`",
6772
|diag| {
6873
with_forced_trimmed_paths!({
69-
diag.note(format!("`Arc<{arg_ty}>` is not `Send` and `Sync` as:"));
70-
71-
if !is_send {
72-
diag.note(format!("- the trait `Send` is not implemented for `{arg_ty}`"));
73-
}
74-
if !is_sync {
75-
diag.note(format!("- the trait `Sync` is not implemented for `{arg_ty}`"));
76-
}
77-
78-
diag.help("consider using an `Rc` instead. `Arc` does not provide benefits for non `Send` and `Sync` types");
79-
80-
diag.note("if you intend to use `Arc` with `Send` and `Sync` traits");
81-
8274
diag.note(format!(
83-
"wrap the inner type with a `Mutex` or implement `Send` and `Sync` for `{arg_ty}`"
75+
"`Arc<{arg_ty}>` is not `Send` and `Sync` as `{arg_ty}` is {reason}"
76+
));
77+
diag.help("if the `Arc` will not used be across threads replace it with an `Rc`");
78+
diag.help(format!(
79+
"otherwise make `{arg_ty}` `Send` and `Sync` or consider a wrapper type such as `Mutex`"
8480
));
8581
});
8682
},

tests/ui/arc_with_non_send_sync.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,9 @@ fn main() {
3333
let _ = Arc::new(42);
3434

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

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

4440
let _ = Arc::new(&42 as *const i32);
45-
//~^ ERROR: usage of an `Arc` that is not `Send` and `Sync`
46-
//~| NOTE: the trait `Send` is not implemented for `*const i32`
47-
//~| NOTE: the trait `Sync` is not implemented for `*const i32`
4841
}

tests/ui/arc_with_non_send_sync.stderr

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,31 @@ error: usage of an `Arc` that is not `Send` and `Sync`
44
LL | let _ = Arc::new(RefCell::new(42));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `Arc<RefCell<i32>>` is not `Send` and `Sync` as:
8-
= note: - the trait `Sync` is not implemented for `RefCell<i32>`
9-
= help: consider using an `Rc` instead. `Arc` does not provide benefits for non `Send` and `Sync` types
10-
= note: if you intend to use `Arc` with `Send` and `Sync` traits
11-
= note: wrap the inner type with a `Mutex` or implement `Send` and `Sync` for `RefCell<i32>`
7+
= note: `Arc<RefCell<i32>>` is not `Send` and `Sync` as `RefCell<i32>` is not `Sync`
8+
= help: if the `Arc` will not used be across threads replace it with an `Rc`
9+
= help: otherwise make `RefCell<i32>` `Send` and `Sync` or consider a wrapper type such as `Mutex`
1210
= note: `-D clippy::arc-with-non-send-sync` implied by `-D warnings`
1311
= help: to override `-D warnings` add `#[allow(clippy::arc_with_non_send_sync)]`
1412

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

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

4033
error: aborting due to 3 previous errors
4134

0 commit comments

Comments
 (0)