Adding extra check to ignore generic args.

This commit is contained in:
avborhanian 2023-06-08 00:22:04 -07:00
parent 8330887e1b
commit 2f5d1c748a
3 changed files with 12 additions and 2 deletions

View file

@ -6,6 +6,7 @@ use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_lint::LateLintPass; use rustc_lint::LateLintPass;
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
@ -46,6 +47,10 @@ impl LateLintPass<'_> for ArcWithNonSendSync {
if let ExprKind::Path(func_path) = func.kind; if let ExprKind::Path(func_path) = func.kind;
if last_path_segment(&func_path).ident.name == sym::new; if last_path_segment(&func_path).ident.name == sym::new;
if let arg_ty = cx.typeck_results().expr_ty(arg); if let arg_ty = cx.typeck_results().expr_ty(arg);
if match arg_ty.kind() {
ty::Param(_) => false,
_ => true,
};
if !cx.tcx if !cx.tcx
.lang_items() .lang_items()
.sync_trait() .sync_trait()

View file

@ -3,6 +3,11 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
fn foo<T>(x: T) {
// Should not lint - purposefully ignoring generic args.
let a = Arc::new(x);
}
fn main() { fn main() {
// This is safe, as `i32` implements `Send` and `Sync`. // This is safe, as `i32` implements `Send` and `Sync`.
let a = Arc::new(42); let a = Arc::new(42);

View file

@ -1,10 +1,10 @@
error: usage of `Arc<T>` where `T` is not `Send` or `Sync` error: usage of `Arc<T>` where `T` is not `Send` or `Sync`
--> $DIR/arc_with_non_send_sync.rs:11:13 --> $DIR/arc_with_non_send_sync.rs:16:13
| |
LL | let b = Arc::new(RefCell::new(42)); LL | let b = Arc::new(RefCell::new(42));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= help: consider using `Rc<T>` instead or wrapping `T` in a std::sync type like Mutex<T> = help: consider using `Rc<T>` instead or wrapping `T` in a std::sync type like `Mutex<T>`
= note: `-D clippy::arc-with-non-send-sync` implied by `-D warnings` = note: `-D clippy::arc-with-non-send-sync` implied by `-D warnings`
error: aborting due to previous error error: aborting due to previous error