Skip to content

Commit 9bdc273

Browse files
committed
relocate functions from clippy_lints::types
relocate `is_ty_param_lang_item` and `is_ty_param_diagnostic_item` to `clippy_utils`
1 parent 3877a41 commit 9bdc273

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

clippy_lints/src/types.rs

+5-31
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, get_qpath_generic_tys, higher, in_constant, indent_of, int_bits,
36-
is_hir_ty_cfg_dependant, is_type_diagnostic_item, last_path_segment, match_def_path, match_path, meets_msrv,
37-
method_chain_args, 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_hir_ty_cfg_dependant, is_ty_param_diagnostic_item, is_ty_param_lang_item, is_type_diagnostic_item,
37+
last_path_segment, match_def_path, match_path, meets_msrv, method_chain_args, multispan_sugg,
38+
numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt, snippet_with_applicability,
39+
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
4040
};
4141

4242
declare_clippy_lint! {
@@ -287,32 +287,6 @@ impl<'tcx> LateLintPass<'tcx> for Types {
287287
}
288288
}
289289

290-
/// Checks if the first type parameter is a lang item.
291-
fn is_ty_param_lang_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item: LangItem) -> Option<&'tcx hir::Ty<'tcx>> {
292-
let ty = get_qpath_generic_tys(qpath).next()?;
293-
294-
if let TyKind::Path(qpath) = &ty.kind {
295-
cx.qpath_res(qpath, ty.hir_id)
296-
.opt_def_id()
297-
.and_then(|id| (cx.tcx.lang_items().require(item) == Ok(id)).then(|| ty))
298-
} else {
299-
None
300-
}
301-
}
302-
303-
/// Checks if the first type parameter is a diagnostic item.
304-
fn is_ty_param_diagnostic_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item: Symbol) -> Option<&'tcx hir::Ty<'tcx>> {
305-
let ty = get_qpath_generic_tys(qpath).next()?;
306-
307-
if let TyKind::Path(qpath) = &ty.kind {
308-
cx.qpath_res(qpath, ty.hir_id)
309-
.opt_def_id()
310-
.and_then(|id| cx.tcx.is_diagnostic_item(item, id).then(|| ty))
311-
} else {
312-
None
313-
}
314-
}
315-
316290
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
317291
if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() {
318292
Some("str")

clippy_utils/src/lib.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
6464
use rustc_hir::Node;
6565
use rustc_hir::{
6666
def, Arm, Block, Body, Constness, Crate, Expr, ExprKind, FnDecl, GenericArgs, HirId, Impl, ImplItem, ImplItemKind,
67-
Item, ItemKind, MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind, TraitRef,
68-
TyKind, Unsafety,
67+
Item, ItemKind, LangItem, MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind,
68+
TraitRef, TyKind, Unsafety,
6969
};
7070
use rustc_infer::infer::TyCtxtInferExt;
7171
use rustc_lint::{LateContext, Level, Lint, LintContext};
@@ -232,6 +232,36 @@ pub fn is_type_lang_item(cx: &LateContext<'_>, ty: Ty<'_>, lang_item: hir::LangI
232232
}
233233
}
234234

235+
/// Checks if the first type parameter is a lang item.
236+
pub fn is_ty_param_lang_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item: LangItem) -> Option<&'tcx hir::Ty<'tcx>> {
237+
let ty = get_qpath_generic_tys(qpath).next()?;
238+
239+
if let TyKind::Path(qpath) = &ty.kind {
240+
cx.qpath_res(qpath, ty.hir_id)
241+
.opt_def_id()
242+
.and_then(|id| (cx.tcx.lang_items().require(item) == Ok(id)).then(|| ty))
243+
} else {
244+
None
245+
}
246+
}
247+
248+
/// Checks if the first type parameter is a diagnostic item.
249+
pub fn is_ty_param_diagnostic_item(
250+
cx: &LateContext<'_>,
251+
qpath: &QPath<'tcx>,
252+
item: Symbol,
253+
) -> Option<&'tcx hir::Ty<'tcx>> {
254+
let ty = get_qpath_generic_tys(qpath).next()?;
255+
256+
if let TyKind::Path(qpath) = &ty.kind {
257+
cx.qpath_res(qpath, ty.hir_id)
258+
.opt_def_id()
259+
.and_then(|id| cx.tcx.is_diagnostic_item(item, id).then(|| ty))
260+
} else {
261+
None
262+
}
263+
}
264+
235265
/// Checks if the method call given in `expr` belongs to the given trait.
236266
pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str]) -> bool {
237267
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();

0 commit comments

Comments
 (0)