rc_mutex use span_lint instead of span_lint_and_sugg

This commit is contained in:
lyj 2021-06-05 20:53:24 +08:00
parent c0f3c2fe27
commit a5ced1fc2b
4 changed files with 15 additions and 60 deletions

View file

@ -1,9 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{get_qpath_generic_tys, is_ty_param_diagnostic_item};
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_ty_param_diagnostic_item;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
use rustc_hir::{self as hir, def_id::DefId, QPath};
use rustc_lint::LateContext;
use rustc_span::symbol::sym;
@ -12,28 +10,14 @@ use super::RC_MUTEX;
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
if_chain! {
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) ;
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) ;
if let TyKind::Path(ref qpath_inner)=ty.kind;
if let Some(_) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) ;
then{
let mut applicability = Applicability::MachineApplicable;
let inner_span = match get_qpath_generic_tys(qpath_inner).next() {
Some(ty) => ty.span,
None => return false,
};
span_lint_and_sugg(
span_lint(
cx,
RC_MUTEX,
hir_ty.span,
"you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`",
"try",
format!(
"Rc<RefCell<{}>>",
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
),
applicability,
"found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` instead",
);
return true;
}

View file

@ -1,28 +0,0 @@
// run-rustfix
#![warn(clippy::rc_mutex)]
#![allow(unused_imports)]
#![allow(clippy::boxed_local, clippy::needless_pass_by_value)]
#![allow(clippy::blacklisted_name, unused_variables, dead_code)]
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Mutex;
pub struct MyStruct {}
pub struct SubT<T> {
foo: T,
}
pub enum MyEnum {
One,
Two,
}
pub fn test1<T>(foo: Rc<RefCell<T>>) {}
pub fn test2(foo: Rc<RefCell<MyEnum>>) {}
pub fn test3(foo: Rc<RefCell<SubT<usize>>>) {}
fn main() {}

View file

@ -1,4 +1,3 @@
// run-rustfix
#![warn(clippy::rc_mutex)]
#![allow(unused_imports)]
#![allow(clippy::boxed_local, clippy::needless_pass_by_value)]

View file

@ -1,22 +1,22 @@
error: you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`
--> $DIR/rc_mutex.rs:22:22
error: Found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` instead
--> $DIR/rc_mutex.rs:21:22
|
LL | pub fn test1<T>(foo: Rc<Mutex<T>>) {}
| ^^^^^^^^^^^^ help: try: `Rc<RefCell<T>>`
| ^^^^^^^^^^^^
|
= note: `-D clippy::rc-mutex` implied by `-D warnings`
error: you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`
--> $DIR/rc_mutex.rs:24:19
error: Found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` instead
--> $DIR/rc_mutex.rs:23:19
|
LL | pub fn test2(foo: Rc<Mutex<MyEnum>>) {}
| ^^^^^^^^^^^^^^^^^ help: try: `Rc<RefCell<MyEnum>>`
| ^^^^^^^^^^^^^^^^^
error: you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`
--> $DIR/rc_mutex.rs:26:19
error: Found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` instead
--> $DIR/rc_mutex.rs:25:19
|
LL | pub fn test3(foo: Rc<Mutex<SubT<usize>>>) {}
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `Rc<RefCell<SubT<usize>>>`
| ^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors