Skip to content

Commit a982142

Browse files
committed
move 'match_type_parameter_diagnostic' to utils, get missed diagnostic item
1 parent 3cea89b commit a982142

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

clippy_lints/src/redundant_clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
fn_has_unsatisfiable_preds, has_drop, is_copy, is_type_diagnostic_item, match_def_path, match_type, paths,
3-
snippet_opt, span_lint_hir, span_lint_hir_and_then, walk_ptrs_ty_depth,
2+
fn_has_unsatisfiable_preds, has_drop, is_copy, is_type_diagnostic_item, match_def_path, paths, snippet_opt,
3+
span_lint_hir, span_lint_hir_and_then, walk_ptrs_ty_depth,
44
};
55
use if_chain::if_chain;
66
use rustc_data_structures::{fx::FxHashMap, transitive_relation::TransitiveRelation};

clippy_lints/src/types.rs

+13-32
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_semver::RustcVersion;
2323
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
2424
use rustc_span::hygiene::{ExpnKind, MacroKind};
2525
use rustc_span::source_map::Span;
26-
use rustc_span::symbol::{sym, Symbol};
26+
use rustc_span::symbol::sym;
2727
use rustc_target::abi::LayoutOf;
2828
use rustc_target::spec::abi::Abi;
2929
use rustc_typeck::hir_ty_to_ty;
@@ -33,10 +33,10 @@ use crate::utils::paths;
3333
use crate::utils::sugg::Sugg;
3434
use crate::utils::{
3535
clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_hir_ty_cfg_dependant,
36-
is_type_diagnostic_item, last_path_segment, match_def_path, match_path, meets_msrv, method_chain_args,
37-
multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt,
38-
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
39-
span_lint_and_then, unsext,
36+
is_type_diagnostic_item, last_path_segment, match_def_path, match_path, match_type_parameter_diagnostic_item,
37+
meets_msrv, method_chain_args, multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet,
38+
snippet_opt, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help,
39+
span_lint_and_sugg, span_lint_and_then, unsext,
4040
};
4141

4242
declare_clippy_lint! {
@@ -307,33 +307,14 @@ fn match_type_parameter(cx: &LateContext<'_>, qpath: &QPath<'_>, path: &[&str])
307307
None
308308
}
309309

310-
/// Checks if `qpath` has last segment with type parameter matching `diagnostic`
311-
fn match_type_parameter_diagnostic(cx: &LateContext<'_>, qpath: &QPath<'_>, diagnostic: Symbol) -> Option<Span> {
312-
let last = last_path_segment(qpath);
313-
if_chain! {
314-
if let Some(ref params) = last.args;
315-
if !params.parenthesized;
316-
if let Some(ty) = params.args.iter().find_map(|arg| match arg {
317-
GenericArg::Type(ty) => Some(ty),
318-
_ => None,
319-
});
320-
if let TyKind::Path(ref qpath) = ty.kind;
321-
if cx.qpath_res(qpath, ty.hir_id).opt_def_id() == cx.tcx.get_diagnostic_item(diagnostic);
322-
then {
323-
return Some(ty.span);
324-
}
325-
}
326-
None
327-
}
328-
329310
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
330-
if match_type_parameter_diagnostic(cx, qpath, sym::string_type).is_some() {
311+
if match_type_parameter_diagnostic_item(cx, qpath, sym::string_type).is_some() {
331312
return Some("str");
332313
}
333-
if match_type_parameter_diagnostic(cx, qpath, sym::OsString).is_some() {
314+
if match_type_parameter_diagnostic_item(cx, qpath, sym::OsString).is_some() {
334315
return Some("std::ffi::OsStr");
335316
}
336-
if match_type_parameter_diagnostic(cx, qpath, sym::PathBuf).is_some() {
317+
if match_type_parameter_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
337318
return Some("std::path::Path");
338319
}
339320
None
@@ -400,7 +381,7 @@ impl Types {
400381
);
401382
return; // don't recurse into the type
402383
}
403-
if match_type_parameter_diagnostic(cx, qpath, sym::vec_type).is_some() {
384+
if match_type_parameter_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
404385
span_lint_and_help(
405386
cx,
406387
BOX_VEC,
@@ -412,7 +393,7 @@ impl Types {
412393
return; // don't recurse into the type
413394
}
414395
} else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
415-
if let Some(span) = match_type_parameter_diagnostic(cx, qpath, sym::Rc) {
396+
if let Some(span) = match_type_parameter_diagnostic_item(cx, qpath, sym::Rc) {
416397
let mut applicability = Applicability::MachineApplicable;
417398
span_lint_and_sugg(
418399
cx,
@@ -464,7 +445,7 @@ impl Types {
464445
);
465446
return; // don't recurse into the type
466447
}
467-
if match_type_parameter_diagnostic(cx, qpath, sym::vec_type).is_some() {
448+
if match_type_parameter_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
468449
let vec_ty = match &last_path_segment(qpath).args.unwrap().args[0] {
469450
GenericArg::Type(ty) => match &ty.kind {
470451
TyKind::Path(qpath) => qpath,
@@ -517,7 +498,7 @@ impl Types {
517498
);
518499
return; // don't recurse into the type
519500
}
520-
if match_type_parameter_diagnostic(cx, qpath, sym::vec_type).is_some() {
501+
if match_type_parameter_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
521502
let vec_ty = match &last_path_segment(qpath).args.unwrap().args[0] {
522503
GenericArg::Type(ty) => match &ty.kind {
523504
TyKind::Path(qpath) => qpath,
@@ -582,7 +563,7 @@ impl Types {
582563
}
583564
}
584565
} else if cx.tcx.is_diagnostic_item(sym::option_type, def_id) {
585-
if match_type_parameter(cx, qpath, &paths::OPTION).is_some() {
566+
if match_type_parameter_diagnostic_item(cx, qpath, sym::option_type).is_some() {
586567
span_lint(
587568
cx,
588569
OPTION_OPTION,

clippy_utils/src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,29 @@ pub fn is_diagnostic_assoc_item(cx: &LateContext<'_>, def_id: DefId, diag_item:
253253
.map_or(false, |assoc_def_id| cx.tcx.is_diagnostic_item(diag_item, assoc_def_id))
254254
}
255255

256+
/// Checks if `qpath` has last segment with type parameter matching `diagnostic_item`
257+
pub fn match_type_parameter_diagnostic_item(
258+
cx: &LateContext<'_>,
259+
qpath: &QPath<'_>,
260+
diagnostic_item: Symbol,
261+
) -> Option<Span> {
262+
let last = last_path_segment(qpath);
263+
if_chain! {
264+
if let Some(ref params) = last.args;
265+
if !params.parenthesized;
266+
if let Some(ty) = params.args.iter().find_map(|arg| match arg {
267+
rustc_hir::GenericArg::Type(ty) => Some(ty),
268+
_ => None,
269+
});
270+
if let TyKind::Path(ref qpath) = ty.kind;
271+
if cx.qpath_res(qpath, ty.hir_id).opt_def_id() == cx.tcx.get_diagnostic_item(diagnostic_item);
272+
then {
273+
return Some(ty.span);
274+
}
275+
}
276+
None
277+
}
278+
256279
/// Checks if an expression references a variable of the given name.
257280
pub fn match_var(expr: &Expr<'_>, var: Symbol) -> bool {
258281
if let ExprKind::Path(QPath::Resolved(None, ref path)) = expr.kind {

0 commit comments

Comments
 (0)