Skip to content

Commit 582fc66

Browse files
committed
Use diagnostic item for
1 parent f30bf69 commit 582fc66

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

clippy_lints/src/ptr.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Checks for usage of `&Vec[_]` and `&String`.
22
33
use crate::utils::ptr::get_spans;
4-
use crate::utils::{match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_then, walk_ptrs_hir_ty};
4+
use crate::utils::{match_qpath, is_type_diagnostic_item, match_type, paths, snippet_opt,
5+
span_lint, span_lint_and_then, walk_ptrs_hir_ty};
56
use if_chain::if_chain;
67
use rustc::hir::QPath;
78
use rustc::hir::*;
@@ -11,7 +12,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1112
use rustc_errors::Applicability;
1213
use std::borrow::Cow;
1314
use syntax::source_map::Span;
14-
use syntax_pos::MultiSpan;
15+
use syntax_pos::{MultiSpan, symbol::sym};
1516

1617
declare_clippy_lint! {
1718
/// **What it does:** This lint checks for function arguments of type `&String`
@@ -148,7 +149,7 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl, fn_id: HirId, opt_body_id:
148149

149150
for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() {
150151
if let ty::Ref(_, ty, MutImmutable) = ty.sty {
151-
if match_type(cx, ty, &paths::VEC) {
152+
if is_type_diagnostic_item(cx, ty, sym::Vec) {
152153
let mut ty_snippet = None;
153154
if_chain! {
154155
if let TyKind::Path(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).node;

clippy_lints/src/utils/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
130130
}
131131
}
132132

133+
/// Checks if the type is equal to a diagnostic item
134+
pub fn is_type_diagnostic_item(cx: &LateContext<'_, '_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
135+
match ty.sty {
136+
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),
137+
_ => false,
138+
}
139+
}
140+
133141
/// Checks if the method call given in `expr` belongs to the given trait.
134142
pub fn match_trait_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str]) -> bool {
135143
let def_id = cx.tables.type_dependent_def_id(expr.hir_id).unwrap();

0 commit comments

Comments
 (0)