Skip to content

Commit 66a83d3

Browse files
committed
Factor out some ty param utils
1 parent 145d7fc commit 66a83d3

File tree

6 files changed

+63
-90
lines changed

6 files changed

+63
-90
lines changed
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::is_ty_param_diagnostic_item;
2+
use clippy_utils::{path_def_id, qpath_generic_tys};
33
use rustc_hir::{self as hir, def_id::DefId, QPath};
44
use rustc_lint::LateContext;
5-
use rustc_span::symbol::sym;
5+
use rustc_span::{sym, Symbol};
66

77
use super::BOX_COLLECTION;
88

@@ -11,10 +11,9 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
1111
if Some(def_id) == cx.tcx.lang_items().owned_box();
1212
if let Some(item_type) = get_std_collection(cx, qpath);
1313
then {
14-
let generic = if item_type == "String" {
15-
""
16-
} else {
17-
"<..>"
14+
let generic = match item_type {
15+
sym::String => "",
16+
_ => "<..>",
1817
};
1918
span_lint_and_help(
2019
cx,
@@ -37,14 +36,10 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
3736
}
3837
}
3938

40-
fn get_std_collection(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
41-
if is_ty_param_diagnostic_item(cx, qpath, sym::Vec).is_some() {
42-
Some("Vec")
43-
} else if is_ty_param_diagnostic_item(cx, qpath, sym::String).is_some() {
44-
Some("String")
45-
} else if is_ty_param_diagnostic_item(cx, qpath, sym::HashMap).is_some() {
46-
Some("HashMap")
47-
} else {
48-
None
49-
}
39+
fn get_std_collection(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<Symbol> {
40+
let param = qpath_generic_tys(qpath).next()?;
41+
let id = path_def_id(cx, param)?;
42+
cx.tcx
43+
.get_diagnostic_name(id)
44+
.filter(|&name| matches!(name, sym::HashMap | sym::String | sym::Vec))
5045
}
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::is_ty_param_diagnostic_item;
2+
use clippy_utils::{path_def_id, qpath_generic_tys};
3+
use if_chain::if_chain;
34
use rustc_hir::{self as hir, def_id::DefId, QPath};
45
use rustc_lint::LateContext;
56
use rustc_span::symbol::sym;
67

78
use super::OPTION_OPTION;
89

910
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
10-
if cx.tcx.is_diagnostic_item(sym::Option, def_id) && is_ty_param_diagnostic_item(cx, qpath, sym::Option).is_some() {
11-
span_lint(
12-
cx,
13-
OPTION_OPTION,
14-
hir_ty.span,
15-
"consider using `Option<T>` instead of `Option<Option<T>>` or a custom \
16-
enum if you need to distinguish all 3 cases",
17-
);
18-
true
19-
} else {
20-
false
11+
if_chain! {
12+
if cx.tcx.is_diagnostic_item(sym::Option, def_id);
13+
if let Some(arg) = qpath_generic_tys(qpath).next();
14+
if path_def_id(cx, arg) == Some(def_id);
15+
then {
16+
span_lint(
17+
cx,
18+
OPTION_OPTION,
19+
hir_ty.span,
20+
"consider using `Option<T>` instead of `Option<Option<T>>` or a custom \
21+
enum if you need to distinguish all 3 cases",
22+
);
23+
true
24+
} else {
25+
false
26+
}
2127
}
2228
}

clippy_lints/src/types/rc_buffer.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
3-
use clippy_utils::{is_ty_param_diagnostic_item, qpath_generic_tys};
3+
use clippy_utils::{path_def_id, qpath_generic_tys};
44
use rustc_errors::Applicability;
55
use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
66
use rustc_lint::LateContext;
@@ -20,7 +20,12 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
2020
format!("Rc<{}>", alternate),
2121
Applicability::MachineApplicable,
2222
);
23-
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Vec) {
23+
} else {
24+
let Some(ty) = qpath_generic_tys(qpath).next() else { return false };
25+
let Some(id) = path_def_id(cx, ty) else { return false };
26+
if !cx.tcx.is_diagnostic_item(sym::Vec, id) {
27+
return false;
28+
}
2429
let qpath = match &ty.kind {
2530
TyKind::Path(qpath) => qpath,
2631
_ => return false,
@@ -55,7 +60,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
5560
format!("Arc<{}>", alternate),
5661
Applicability::MachineApplicable,
5762
);
58-
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Vec) {
63+
} else if let Some(ty) = qpath_generic_tys(qpath).next() {
64+
let Some(id) = path_def_id(cx, ty) else { return false };
65+
if !cx.tcx.is_diagnostic_item(sym::Vec, id) {
66+
return false;
67+
}
5968
let qpath = match &ty.kind {
6069
TyKind::Path(qpath) => qpath,
6170
_ => return false,
@@ -85,13 +94,13 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
8594
}
8695

8796
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
88-
if is_ty_param_diagnostic_item(cx, qpath, sym::String).is_some() {
89-
Some("str")
90-
} else if is_ty_param_diagnostic_item(cx, qpath, sym::OsString).is_some() {
91-
Some("std::ffi::OsStr")
92-
} else if is_ty_param_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
93-
Some("std::path::Path")
94-
} else {
95-
None
96-
}
97+
let ty = qpath_generic_tys(qpath).next()?;
98+
let id = path_def_id(cx, ty)?;
99+
let path = match cx.tcx.get_diagnostic_name(id)? {
100+
sym::String => "str",
101+
sym::OsString => "std::ffi::OsStr",
102+
sym::PathBuf => "std::path::Path",
103+
_ => return None,
104+
};
105+
Some(path)
97106
}

clippy_lints/src/types/rc_mutex.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::is_ty_param_diagnostic_item;
2+
use clippy_utils::{path_def_id, qpath_generic_tys};
33
use if_chain::if_chain;
44
use rustc_hir::{self as hir, def_id::DefId, QPath};
55
use rustc_lint::LateContext;
@@ -10,7 +10,9 @@ use super::RC_MUTEX;
1010
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
1111
if_chain! {
1212
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) ;
13-
if let Some(_) = is_ty_param_diagnostic_item(cx, qpath, sym::Mutex) ;
13+
if let Some(arg) = qpath_generic_tys(qpath).next();
14+
if let Some(id) = path_def_id(cx, arg);
15+
if cx.tcx.is_diagnostic_item(sym::Mutex, id);
1416
then {
1517
span_lint_and_help(
1618
cx,

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::{snippet, snippet_with_applicability};
3-
use clippy_utils::{is_ty_param_diagnostic_item, is_ty_param_lang_item, qpath_generic_tys};
3+
use clippy_utils::{path_def_id, qpath_generic_tys};
44
use rustc_errors::Applicability;
5-
use rustc_hir::{self as hir, def_id::DefId, LangItem, QPath, TyKind};
5+
use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
66
use rustc_lint::LateContext;
77
use rustc_span::symbol::sym;
88

@@ -39,14 +39,13 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
3939
return true;
4040
}
4141

42-
let (inner_sym, ty) = if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
43-
("Box", ty)
44-
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Rc) {
45-
("Rc", ty)
46-
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Arc) {
47-
("Arc", ty)
48-
} else {
49-
return false;
42+
let Some(ty) = qpath_generic_tys(qpath).next() else { return false };
43+
let Some(id) = path_def_id(cx, ty) else { return false };
44+
let (inner_sym, ty) = match cx.tcx.get_diagnostic_name(id) {
45+
Some(sym::Arc) => ("Arc", ty),
46+
Some(sym::Rc) => ("Rc", ty),
47+
_ if Some(id) == cx.tcx.lang_items().owned_box() => ("Box", ty),
48+
_ => return false,
5049
};
5150

5251
let inner_qpath = match &ty.kind {

clippy_utils/src/lib.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -262,44 +262,6 @@ pub fn is_wild(pat: &Pat<'_>) -> bool {
262262
matches!(pat.kind, PatKind::Wild)
263263
}
264264

265-
/// Checks if the first type parameter is a lang item.
266-
pub fn is_ty_param_lang_item<'tcx>(
267-
cx: &LateContext<'_>,
268-
qpath: &QPath<'tcx>,
269-
item: LangItem,
270-
) -> Option<&'tcx hir::Ty<'tcx>> {
271-
let ty = qpath_generic_tys(qpath).next()?;
272-
273-
if let TyKind::Path(qpath) = &ty.kind {
274-
cx.qpath_res(qpath, ty.hir_id)
275-
.opt_def_id()
276-
.map_or(false, |id| {
277-
cx.tcx.lang_items().require(item).map_or(false, |lang_id| id == lang_id)
278-
})
279-
.then(|| ty)
280-
} else {
281-
None
282-
}
283-
}
284-
285-
/// Checks if the first type parameter is a diagnostic item.
286-
pub fn is_ty_param_diagnostic_item<'tcx>(
287-
cx: &LateContext<'_>,
288-
qpath: &QPath<'tcx>,
289-
item: Symbol,
290-
) -> Option<&'tcx hir::Ty<'tcx>> {
291-
let ty = qpath_generic_tys(qpath).next()?;
292-
293-
if let TyKind::Path(qpath) = &ty.kind {
294-
cx.qpath_res(qpath, ty.hir_id)
295-
.opt_def_id()
296-
.map_or(false, |id| cx.tcx.is_diagnostic_item(item, id))
297-
.then(|| ty)
298-
} else {
299-
None
300-
}
301-
}
302-
303265
/// Checks if the method call given in `expr` belongs to the given trait.
304266
/// This is a deprecated function, consider using [`is_trait_method`].
305267
pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str]) -> bool {

0 commit comments

Comments
 (0)