Skip to content

Commit 2f5d1c7

Browse files
committed
Adding extra check to ignore generic args.
1 parent 8330887 commit 2f5d1c7

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clippy_lints/src/arc_with_non_send_sync.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use if_chain::if_chain;
66
use rustc_hir::{Expr, ExprKind};
77
use rustc_lint::LateContext;
88
use rustc_lint::LateLintPass;
9+
use rustc_middle::ty;
910
use rustc_session::{declare_lint_pass, declare_tool_lint};
1011
use rustc_span::symbol::sym;
1112

@@ -46,6 +47,10 @@ impl LateLintPass<'_> for ArcWithNonSendSync {
4647
if let ExprKind::Path(func_path) = func.kind;
4748
if last_path_segment(&func_path).ident.name == sym::new;
4849
if let arg_ty = cx.typeck_results().expr_ty(arg);
50+
if match arg_ty.kind() {
51+
ty::Param(_) => false,
52+
_ => true,
53+
};
4954
if !cx.tcx
5055
.lang_items()
5156
.sync_trait()

tests/ui/arc_with_non_send_sync.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
use std::cell::RefCell;
44
use std::sync::{Arc, Mutex};
55

6+
fn foo<T>(x: T) {
7+
// Should not lint - purposefully ignoring generic args.
8+
let a = Arc::new(x);
9+
}
10+
611
fn main() {
712
// This is safe, as `i32` implements `Send` and `Sync`.
813
let a = Arc::new(42);

tests/ui/arc_with_non_send_sync.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error: usage of `Arc<T>` where `T` is not `Send` or `Sync`
2-
--> $DIR/arc_with_non_send_sync.rs:11:13
2+
--> $DIR/arc_with_non_send_sync.rs:16:13
33
|
44
LL | let b = Arc::new(RefCell::new(42));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: consider using `Rc<T>` instead or wrapping `T` in a std::sync type like Mutex<T>
7+
= help: consider using `Rc<T>` instead or wrapping `T` in a std::sync type like `Mutex<T>`
88
= note: `-D clippy::arc-with-non-send-sync` implied by `-D warnings`
99

1010
error: aborting due to previous error

0 commit comments

Comments
 (0)