Skip to content

Commit 0d0dbae

Browse files
committed
Auto merge of rust-lang#11233 - Centri3:rust-lang#11232, r=Jarcho
[`arc_with_non_send_sync`]: No longer lints macro-generated code Fixes rust-lang#11232 changelog: [`arc_with_non_send_sync`]: No longer lints macro-generated code
2 parents 0fec99f + 90947e9 commit 0d0dbae

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

clippy_lints/src/arc_with_non_send_sync.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::last_path_segment;
32
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
3+
use clippy_utils::{is_from_proc_macro, last_path_segment};
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::ty;
@@ -38,10 +38,11 @@ declare_clippy_lint! {
3838
}
3939
declare_lint_pass!(ArcWithNonSendSync => [ARC_WITH_NON_SEND_SYNC]);
4040

41-
impl LateLintPass<'_> for ArcWithNonSendSync {
42-
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
43-
let ty = cx.typeck_results().expr_ty(expr);
44-
if is_type_diagnostic_item(cx, ty, sym::Arc)
41+
impl<'tcx> LateLintPass<'tcx> for ArcWithNonSendSync {
42+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
43+
if !expr.span.from_expansion()
44+
&& let ty = cx.typeck_results().expr_ty(expr)
45+
&& is_type_diagnostic_item(cx, ty, sym::Arc)
4546
&& let ExprKind::Call(func, [arg]) = expr.kind
4647
&& let ExprKind::Path(func_path) = func.kind
4748
&& last_path_segment(&func_path).ident.name == sym::new
@@ -54,6 +55,7 @@ impl LateLintPass<'_> for ArcWithNonSendSync {
5455
&& let Some(sync) = cx.tcx.lang_items().sync_trait()
5556
&& let [is_send, is_sync] = [send, sync].map(|id| implements_trait(cx, arg_ty, id, &[]))
5657
&& !(is_send && is_sync)
58+
&& !is_from_proc_macro(cx, expr)
5759
{
5860
span_lint_and_then(
5961
cx,

tests/ui/arc_with_non_send_sync.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
//@aux-build:proc_macros.rs:proc-macro
12
#![warn(clippy::arc_with_non_send_sync)]
23
#![allow(unused_variables)]
4+
5+
#[macro_use]
6+
extern crate proc_macros;
7+
38
use std::cell::RefCell;
9+
use std::ptr::{null, null_mut};
410
use std::sync::{Arc, Mutex};
511

612
fn foo<T>(x: T) {
@@ -11,14 +17,32 @@ fn issue11076<T>() {
1117
let a: Arc<Vec<T>> = Arc::new(Vec::new());
1218
}
1319

20+
fn issue11232() {
21+
external! {
22+
let a: Arc<*const u8> = Arc::new(null());
23+
let a: Arc<*mut u8> = Arc::new(null_mut());
24+
}
25+
with_span! {
26+
span
27+
let a: Arc<*const u8> = Arc::new(null());
28+
let a: Arc<*mut u8> = Arc::new(null_mut());
29+
}
30+
}
31+
1432
fn main() {
1533
let _ = Arc::new(42);
1634

17-
// !Sync
1835
let _ = Arc::new(RefCell::new(42));
36+
//~^ ERROR: usage of an `Arc` that is not `Send` or `Sync`
37+
//~| NOTE: the trait `Sync` is not implemented for `RefCell<i32>`
38+
1939
let mutex = Mutex::new(1);
20-
// !Send
2140
let _ = Arc::new(mutex.lock().unwrap());
22-
// !Send + !Sync
41+
//~^ ERROR: usage of an `Arc` that is not `Send` or `Sync`
42+
//~| NOTE: the trait `Send` is not implemented for `MutexGuard<'_, i32>`
43+
2344
let _ = Arc::new(&42 as *const i32);
45+
//~^ ERROR: usage of an `Arc` that is not `Send` or `Sync`
46+
//~| NOTE: the trait `Send` is not implemented for `*const i32`
47+
//~| NOTE: the trait `Sync` is not implemented for `*const i32`
2448
}

tests/ui/arc_with_non_send_sync.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: usage of an `Arc` that is not `Send` or `Sync`
2-
--> $DIR/arc_with_non_send_sync.rs:18:13
2+
--> $DIR/arc_with_non_send_sync.rs:35:13
33
|
44
LL | let _ = Arc::new(RefCell::new(42));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -10,7 +10,7 @@ LL | let _ = Arc::new(RefCell::new(42));
1010
= note: `-D clippy::arc-with-non-send-sync` implied by `-D warnings`
1111

1212
error: usage of an `Arc` that is not `Send` or `Sync`
13-
--> $DIR/arc_with_non_send_sync.rs:21:13
13+
--> $DIR/arc_with_non_send_sync.rs:40:13
1414
|
1515
LL | let _ = Arc::new(mutex.lock().unwrap());
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -20,7 +20,7 @@ LL | let _ = Arc::new(mutex.lock().unwrap());
2020
= help: consider using an `Rc` instead or wrapping the inner type with a `Mutex`
2121

2222
error: usage of an `Arc` that is not `Send` or `Sync`
23-
--> $DIR/arc_with_non_send_sync.rs:23:13
23+
--> $DIR/arc_with_non_send_sync.rs:44:13
2424
|
2525
LL | let _ = Arc::new(&42 as *const i32);
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)