mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Adding extra check to ignore generic args.
This commit is contained in:
parent
8330887e1b
commit
2f5d1c748a
3 changed files with 12 additions and 2 deletions
|
@ -6,6 +6,7 @@ use if_chain::if_chain;
|
|||
use rustc_hir::{Expr, ExprKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_lint::LateLintPass;
|
||||
use rustc_middle::ty;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
|
@ -46,6 +47,10 @@ impl LateLintPass<'_> for ArcWithNonSendSync {
|
|||
if let ExprKind::Path(func_path) = func.kind;
|
||||
if last_path_segment(&func_path).ident.name == sym::new;
|
||||
if let arg_ty = cx.typeck_results().expr_ty(arg);
|
||||
if match arg_ty.kind() {
|
||||
ty::Param(_) => false,
|
||||
_ => true,
|
||||
};
|
||||
if !cx.tcx
|
||||
.lang_items()
|
||||
.sync_trait()
|
||||
|
|
|
@ -3,6 +3,11 @@
|
|||
use std::cell::RefCell;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
fn foo<T>(x: T) {
|
||||
// Should not lint - purposefully ignoring generic args.
|
||||
let a = Arc::new(x);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// This is safe, as `i32` implements `Send` and `Sync`.
|
||||
let a = Arc::new(42);
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
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));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= 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`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
|
Loading…
Reference in a new issue