Use {get,match}_def_path from LateContext

This commit is contained in:
flip1995 2019-04-07 19:44:10 +02:00
parent 2f100e04af
commit 840eac2c05
No known key found for this signature in database
GPG Key ID: 693086869D506637
25 changed files with 122 additions and 128 deletions

View File

@ -2,7 +2,7 @@
use crate::reexport::*;
use crate::utils::{
in_macro, last_line_of_span, match_def_path, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then,
in_macro, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then,
without_block_comments,
};
use if_chain::if_chain;
@ -11,7 +11,7 @@ use rustc::lint::{
in_external_macro, CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray,
LintContext, LintPass,
};
use rustc::ty::{self, TyCtxt};
use rustc::ty;
use rustc::{declare_tool_lint, lint_array};
use rustc_errors::Applicability;
use semver::Version;
@ -234,7 +234,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
}
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
if is_relevant_item(cx.tcx, item) {
if is_relevant_item(cx, item) {
check_attrs(cx, item.span, item.ident.name, &item.attrs)
}
match item.node {
@ -302,13 +302,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
}
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
if is_relevant_impl(cx.tcx, item) {
if is_relevant_impl(cx, item) {
check_attrs(cx, item.span, item.ident.name, &item.attrs)
}
}
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
if is_relevant_trait(cx.tcx, item) {
if is_relevant_trait(cx, item) {
check_attrs(cx, item.span, item.ident.name, &item.attrs)
}
}
@ -361,52 +361,52 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
}
}
fn is_relevant_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &Item) -> bool {
fn is_relevant_item(cx: &LateContext<'_, '_>, item: &Item) -> bool {
if let ItemKind::Fn(_, _, _, eid) = item.node {
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value)
is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value)
} else {
true
}
}
fn is_relevant_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &ImplItem) -> bool {
fn is_relevant_impl(cx: &LateContext<'_, '_>, item: &ImplItem) -> bool {
match item.node {
ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value),
ImplItemKind::Method(_, eid) => is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value),
_ => false,
}
}
fn is_relevant_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &TraitItem) -> bool {
fn is_relevant_trait(cx: &LateContext<'_, '_>, item: &TraitItem) -> bool {
match item.node {
TraitItemKind::Method(_, TraitMethod::Required(_)) => true,
TraitItemKind::Method(_, TraitMethod::Provided(eid)) => {
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value)
is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value)
},
_ => false,
}
}
fn is_relevant_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
fn is_relevant_block(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
if let Some(stmt) = block.stmts.first() {
match &stmt.node {
StmtKind::Local(_) => true,
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(tcx, tables, expr),
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, tables, expr),
_ => false,
}
} else {
block.expr.as_ref().map_or(false, |e| is_relevant_expr(tcx, tables, e))
block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e))
}
}
fn is_relevant_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
match &expr.node {
ExprKind::Block(block, _) => is_relevant_block(tcx, tables, block),
ExprKind::Ret(Some(e)) => is_relevant_expr(tcx, tables, e),
ExprKind::Block(block, _) => is_relevant_block(cx, tables, block),
ExprKind::Ret(Some(e)) => is_relevant_expr(cx, tables, e),
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
ExprKind::Call(path_expr, _) => {
if let ExprKind::Path(qpath) = &path_expr.node {
if let Some(fun_id) = tables.qpath_def(qpath, path_expr.hir_id).opt_def_id() {
!match_def_path(tcx, fun_id, &paths::BEGIN_PANIC)
!cx.match_def_path(fun_id, &paths::BEGIN_PANIC)
} else {
true
}

View File

@ -1,6 +1,6 @@
#![allow(clippy::float_cmp)]
use crate::utils::{clip, get_def_path, sext, unsext};
use crate::utils::{clip, sext, unsext};
use if_chain::if_chain;
use rustc::hir::def::Def;
use rustc::hir::*;
@ -180,7 +180,7 @@ pub fn constant<'c, 'cc>(
e: &Expr,
) -> Option<(Constant, bool)> {
let mut cx = ConstEvalLateContext {
tcx: lcx.tcx,
lcx,
tables,
param_env: lcx.param_env,
needed_resolution: false,
@ -199,11 +199,11 @@ pub fn constant_simple<'c, 'cc>(
/// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`.
pub fn constant_context<'c, 'cc>(
lcx: &LateContext<'c, 'cc>,
lcx: &'c LateContext<'c, 'cc>,
tables: &'c ty::TypeckTables<'cc>,
) -> ConstEvalLateContext<'c, 'cc> {
ConstEvalLateContext {
tcx: lcx.tcx,
lcx,
tables,
param_env: lcx.param_env,
needed_resolution: false,
@ -212,7 +212,7 @@ pub fn constant_context<'c, 'cc>(
}
pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
lcx: &'a LateContext<'a, 'tcx>,
tables: &'a ty::TypeckTables<'tcx>,
param_env: ty::ParamEnv<'tcx>,
needed_resolution: bool,
@ -231,7 +231,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
ExprKind::Repeat(ref value, _) => {
let n = match self.tables.expr_ty(e).sty {
ty::Array(_, n) => n.assert_usize(self.tcx).expect("array length"),
ty::Array(_, n) => n.assert_usize(self.lcx.tcx).expect("array length"),
_ => span_bug!(e.span, "typeck error"),
};
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
@ -249,7 +249,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
if let ExprKind::Path(qpath) = &callee.node;
let def = self.tables.qpath_def(qpath, callee.hir_id);
if let Some(def_id) = def.opt_def_id();
let def_path = get_def_path(self.tcx, def_id)
let def_path = self.lcx.get_def_path(def_id)
.iter()
.map(LocalInternedString::get)
.collect::<Vec<_>>();
@ -283,8 +283,8 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
Int(value) => {
let value = !value;
match ty.sty {
ty::Int(ity) => Some(Int(unsext(self.tcx, value as i128, ity))),
ty::Uint(ity) => Some(Int(clip(self.tcx, value, ity))),
ty::Int(ity) => Some(Int(unsext(self.lcx.tcx, value as i128, ity))),
ty::Uint(ity) => Some(Int(clip(self.lcx.tcx, value, ity))),
_ => None,
}
},
@ -301,10 +301,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
_ => return None,
};
// sign extend
let value = sext(self.tcx, value, ity);
let value = sext(self.lcx.tcx, value, ity);
let value = value.checked_neg()?;
// clear unused bits
Some(Int(unsext(self.tcx, value, ity)))
Some(Int(unsext(self.lcx.tcx, value, ity)))
},
F32(f) => Some(F32(-f)),
F64(f) => Some(F64(-f)),
@ -329,16 +329,16 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
let substs = if self.substs.is_empty() {
substs
} else {
substs.subst(self.tcx, self.substs)
substs.subst(self.lcx.tcx, self.substs)
};
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs)?;
let instance = Instance::resolve(self.lcx.tcx, self.param_env, def_id, substs)?;
let gid = GlobalId {
instance,
promoted: None,
};
let result = self.tcx.const_eval(self.param_env.and(gid)).ok()?;
let ret = miri_to_const(self.tcx, &result);
let result = self.lcx.tcx.const_eval(self.param_env.and(gid)).ok()?;
let ret = miri_to_const(self.lcx.tcx, &result);
if ret.is_some() {
self.needed_resolution = true;
}
@ -376,9 +376,9 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
match (l, r) {
(Constant::Int(l), Some(Constant::Int(r))) => match self.tables.expr_ty(left).sty {
ty::Int(ity) => {
let l = sext(self.tcx, l, ity);
let r = sext(self.tcx, r, ity);
let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
let l = sext(self.lcx.tcx, l, ity);
let r = sext(self.lcx.tcx, r, ity);
let zext = |n: i128| Constant::Int(unsext(self.lcx.tcx, n, ity));
match op.node {
BinOpKind::Add => l.checked_add(r).map(zext),
BinOpKind::Sub => l.checked_sub(r).map(zext),

View File

@ -5,7 +5,7 @@ use rustc::ty;
use rustc::{declare_tool_lint, lint_array};
use rustc_errors::Applicability;
use crate::utils::{any_parent_is_automatically_derived, match_def_path, paths, span_lint_and_sugg};
use crate::utils::{any_parent_is_automatically_derived, paths, span_lint_and_sugg};
declare_clippy_lint! {
/// **What it does:** Checks for literal calls to `Default::default()`.
@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
if let ExprKind::Path(ref qpath) = path.node;
if let Some(def_id) = cx.tables.qpath_def(qpath, path.hir_id).opt_def_id();
if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD);
if cx.match_def_path(def_id, &paths::DEFAULT_TRAIT_METHOD);
then {
match qpath {
QPath::Resolved(..) => {

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, paths, span_lint};
use crate::utils::{paths, span_lint};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateLintPass, LintArray, LintPass};
@ -66,7 +66,7 @@ fn lint_bound<'a, 'tcx>(cx: &rustc::lint::LateContext<'a, 'tcx>, bound: &'tcx Ge
if_chain! {
if let GenericBound::Trait(t, _) = bound;
if let Some(def_id) = t.trait_ref.path.def.opt_def_id();
if match_def_path(cx.tcx, def_id, &paths::DROP_TRAIT);
if cx.match_def_path(def_id, &paths::DROP_TRAIT);
then {
span_lint(
cx,

View File

@ -1,4 +1,4 @@
use crate::utils::{is_copy, match_def_path, paths, span_note_and_lint};
use crate::utils::{is_copy, paths, span_note_and_lint};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -132,10 +132,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
let arg_ty = cx.tables.expr_ty(arg);
if let ty::Ref(..) = arg_ty.sty {
if match_def_path(cx.tcx, def_id, &paths::DROP) {
if cx.match_def_path(def_id, &paths::DROP) {
lint = DROP_REF;
msg = DROP_REF_SUMMARY.to_string();
} else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
} else if cx.match_def_path(def_id, &paths::MEM_FORGET) {
lint = FORGET_REF;
msg = FORGET_REF_SUMMARY.to_string();
} else {
@ -148,10 +148,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
arg.span,
&format!("argument has type {}", arg_ty));
} else if is_copy(cx, arg_ty) {
if match_def_path(cx.tcx, def_id, &paths::DROP) {
if cx.match_def_path(def_id, &paths::DROP) {
lint = DROP_COPY;
msg = DROP_COPY_SUMMARY.to_string();
} else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
} else if cx.match_def_path(def_id, &paths::MEM_FORGET) {
lint = FORGET_COPY;
msg = FORGET_COPY_SUMMARY.to_string();
} else {

View File

@ -1,4 +1,4 @@
use crate::utils::{is_expn_of, match_def_path, resolve_node, span_lint, span_lint_and_sugg};
use crate::utils::{is_expn_of, resolve_node, span_lint, span_lint_and_sugg};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -54,9 +54,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if let ExprKind::Path(ref qpath) = dest_fun.node;
if let Some(dest_fun_id) =
resolve_node(cx, qpath, dest_fun.hir_id).opt_def_id();
if let Some(dest_name) = if match_def_path(cx.tcx, dest_fun_id, &["std", "io", "stdio", "stdout"]) {
if let Some(dest_name) = if cx.match_def_path(dest_fun_id, &["std", "io", "stdio", "stdout"]) {
Some("stdout")
} else if match_def_path(cx.tcx, dest_fun_id, &["std", "io", "stdio", "stderr"]) {
} else if cx.match_def_path(dest_fun_id, &["std", "io", "stdio", "stderr"]) {
Some("stderr")
} else {
None

View File

@ -1,5 +1,5 @@
use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT, OPTION, RESULT};
use crate::utils::{is_expn_of, match_def_path, method_chain_args, span_lint_and_then, walk_ptrs_ty};
use crate::utils::{is_expn_of, method_chain_args, span_lint_and_then, walk_ptrs_ty};
use if_chain::if_chain;
use rustc::hir;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -47,7 +47,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FallibleImplFrom {
if_chain! {
if let hir::ItemKind::Impl(.., ref impl_items) = item.node;
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id);
if match_def_path(cx.tcx, impl_trait_ref.def_id, &FROM_TRAIT);
if cx.match_def_path(impl_trait_ref.def_id, &FROM_TRAIT);
then {
lint_impl_body(cx, item.span, impl_items);
}
@ -60,7 +60,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
use rustc::hir::*;
struct FindPanicUnwrap<'a, 'tcx: 'a> {
tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
lcx: &'a LateContext<'a, 'tcx>,
tables: &'tcx ty::TypeckTables<'tcx>,
result: Vec<Span>,
}
@ -72,8 +72,8 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
if let ExprKind::Call(ref func_expr, _) = expr.node;
if let ExprKind::Path(QPath::Resolved(_, ref path)) = func_expr.node;
if let Some(path_def_id) = path.def.opt_def_id();
if match_def_path(self.tcx, path_def_id, &BEGIN_PANIC) ||
match_def_path(self.tcx, path_def_id, &BEGIN_PANIC_FMT);
if self.lcx.match_def_path(path_def_id, &BEGIN_PANIC) ||
self.lcx.match_def_path(path_def_id, &BEGIN_PANIC_FMT);
if is_expn_of(expr.span, "unreachable").is_none();
then {
self.result.push(expr.span);
@ -83,7 +83,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
// check for `unwrap`
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
let reciever_ty = walk_ptrs_ty(self.tables.expr_ty(&arglists[0][0]));
if match_type(self.tcx, reciever_ty, &OPTION) || match_type(self.tcx, reciever_ty, &RESULT) {
if match_type(self.lcx, reciever_ty, &OPTION) || match_type(self.lcx, reciever_ty, &RESULT) {
self.result.push(expr.span);
}
}
@ -107,7 +107,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
let body = cx.tcx.hir().body(body_id);
let impl_item_def_id = cx.tcx.hir().local_def_id_from_hir_id(impl_item.id.hir_id);
let mut fpu = FindPanicUnwrap {
tcx: cx.tcx,
lcx: cx,
tables: cx.tcx.typeck_tables_of(impl_item_def_id),
result: Vec::new(),
};
@ -132,9 +132,9 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
}
}
fn match_type<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'_>, path: &[&str]) -> bool {
fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
match ty.sty {
ty::Adt(adt, _) => match_def_path(tcx, adt.did, path),
ty::Adt(adt, _) => cx.match_def_path(adt.did, path),
_ => false,
}
}

View File

@ -1,7 +1,6 @@
use crate::utils::paths;
use crate::utils::{
in_macro, is_expn_of, last_path_segment, match_def_path, match_type, resolve_node, snippet, span_lint_and_then,
walk_ptrs_ty,
in_macro, is_expn_of, last_path_segment, match_type, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty,
};
use if_chain::if_chain;
use rustc::hir::*;
@ -59,9 +58,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if_chain! {
if let ExprKind::Path(ref qpath) = fun.node;
if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
let new_v1 = match_def_path(cx.tcx, fun_def_id, &paths::FMT_ARGUMENTS_NEWV1);
let new_v1_fmt = match_def_path(
cx.tcx,
let new_v1 = cx.match_def_path(fun_def_id, &paths::FMT_ARGUMENTS_NEWV1);
let new_v1_fmt = cx.match_def_path(
fun_def_id,
&paths::FMT_ARGUMENTS_NEWV1FORMATTED
);
@ -162,7 +160,7 @@ fn get_single_string_arg<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr) -> Option
if args.len() == 2;
if let ExprKind::Path(ref qpath) = args[1].node;
if let Some(fun_def_id) = resolve_node(cx, qpath, args[1].hir_id).opt_def_id();
if match_def_path(cx.tcx, fun_def_id, &paths::DISPLAY_FMT_METHOD);
if cx.match_def_path(fun_def_id, &paths::DISPLAY_FMT_METHOD);
then {
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pat[0]));
if ty.sty == ty::Str || match_type(cx, ty, &paths::STRING) {

View File

@ -1,6 +1,4 @@
use crate::utils::{
in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then,
};
use crate::utils::{in_macro, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then};
use crate::utils::{paths, resolve_node};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -99,7 +97,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
ExprKind::Call(ref path, ref args) => {
if let ExprKind::Path(ref qpath) = path.node {
if let Some(def_id) = resolve_node(cx, qpath, path.hir_id).opt_def_id() {
if match_def_path(cx.tcx, def_id, &paths::FROM_FROM[..]) {
if cx.match_def_path(def_id, &paths::FROM_FROM[..]) {
let a = cx.tables.expr_ty(e);
let b = cx.tables.expr_ty(&args[0]);
if same_tys(cx, a, b) {

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, paths, span_help_and_lint};
use crate::utils::{paths, span_help_and_lint};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -47,12 +47,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidRef {
if let ty::Ref(..) = cx.tables.expr_ty(expr).sty;
if let Some(def_id) = cx.tables.qpath_def(qpath, path.hir_id).opt_def_id();
then {
let msg = if match_def_path(cx.tcx, def_id, &paths::MEM_ZEROED) |
match_def_path(cx.tcx, def_id, &paths::INIT)
let msg = if cx.match_def_path(def_id, &paths::MEM_ZEROED) |
cx.match_def_path(def_id, &paths::INIT)
{
ZERO_REF_SUMMARY
} else if match_def_path(cx.tcx, def_id, &paths::MEM_UNINIT) |
match_def_path(cx.tcx, def_id, &paths::UNINIT)
} else if cx.match_def_path(def_id, &paths::MEM_UNINIT) |
cx.match_def_path(def_id, &paths::UNINIT)
{
UNINIT_REF_SUMMARY
} else {

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, paths, snippet, span_lint_and_then, walk_ptrs_ty_depth};
use crate::utils::{paths, snippet, span_lint_and_then, walk_ptrs_ty_depth};
use if_chain::if_chain;
use rustc::hir::{Expr, ExprKind};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -46,7 +46,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemDiscriminant {
// is `mem::discriminant`
if let ExprKind::Path(ref func_qpath) = func.node;
if let Some(def_id) = cx.tables.qpath_def(func_qpath, func.hir_id).opt_def_id();
if match_def_path(cx.tcx, def_id, &paths::MEM_DISCRIMINANT);
if cx.match_def_path(def_id, &paths::MEM_DISCRIMINANT);
// type is non-enum
let ty_param = cx.tables.node_substs(func.hir_id).type_at(0);
if !ty_param.is_enum();

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, paths, span_lint};
use crate::utils::{paths, span_lint};
use rustc::hir::{Expr, ExprKind};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
@ -38,7 +38,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemForget {
if let ExprKind::Call(ref path_expr, ref args) = e.node {
if let ExprKind::Path(ref qpath) = path_expr.node {
if let Some(def_id) = cx.tables.qpath_def(qpath, path_expr.hir_id).opt_def_id() {
if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
if cx.match_def_path(def_id, &paths::MEM_FORGET) {
let forgot_ty = cx.tables.expr_ty(&args[0]);
if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, match_qpath, paths, snippet_with_applicability, span_lint_and_sugg};
use crate::utils::{match_qpath, paths, snippet_with_applicability, span_lint_and_sugg};
use if_chain::if_chain;
use rustc::hir::{Expr, ExprKind, MutMutable, QPath};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -52,7 +52,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
if func_args.len() == 2;
if let ExprKind::Path(ref func_qpath) = func.node;
if let Some(def_id) = cx.tables.qpath_def(func_qpath, func.hir_id).opt_def_id();
if match_def_path(cx.tcx, def_id, &paths::MEM_REPLACE);
if cx.match_def_path(def_id, &paths::MEM_REPLACE);
// Check that second argument is `Option::None`
if let ExprKind::Path(ref replacement_qpath) = func_args[1].node;

View File

@ -21,10 +21,10 @@ use crate::utils::paths;
use crate::utils::sugg;
use crate::utils::{
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy, is_expn_of,
is_self, is_self_ty, iter_input_pats, last_path_segment, match_def_path, match_path, match_qpath,
match_trait_method, match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys,
single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
is_self, is_self_ty, iter_input_pats, last_path_segment, match_path, match_qpath, match_trait_method, match_type,
match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path, snippet,
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg, span_lint_and_then,
span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
};
#[derive(Clone)]
@ -1464,7 +1464,7 @@ fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, new: &hir::Ex
if args.len() == 1;
if let hir::ExprKind::Path(ref path) = fun.node;
if let Def::Method(did) = cx.tables.qpath_def(path, fun.hir_id);
if match_def_path(cx.tcx, did, &paths::CSTRING_NEW);
if cx.match_def_path(did, &paths::CSTRING_NEW);
then {
span_lint_and_then(
cx,

View File

@ -1,5 +1,5 @@
use crate::consts::{constant_simple, Constant};
use crate::utils::{match_def_path, paths, span_lint};
use crate::utils::{paths, span_lint};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
@ -73,9 +73,9 @@ fn min_max<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr) -> Option<(MinMax, Cons
if let ExprKind::Call(ref path, ref args) = expr.node {
if let ExprKind::Path(ref qpath) = path.node {
cx.tables.qpath_def(qpath, path.hir_id).opt_def_id().and_then(|def_id| {
if match_def_path(cx.tcx, def_id, &paths::CMP_MIN) {
if cx.match_def_path(def_id, &paths::CMP_MIN) {
fetch_const(cx, args, MinMax::Min)
} else if match_def_path(cx.tcx, def_id, &paths::CMP_MAX) {
} else if cx.match_def_path(def_id, &paths::CMP_MAX) {
fetch_const(cx, args, MinMax::Max)
} else {
None

View File

@ -1,4 +1,4 @@
use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, paths, resolve_node, span_lint};
use crate::utils::{is_direct_expn_of, is_expn_of, paths, resolve_node, span_lint};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -62,7 +62,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if let ExprKind::Call(ref fun, ref params) = ex.node;
if let ExprKind::Path(ref qpath) = fun.node;
if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
if cx.match_def_path(fun_def_id, &paths::BEGIN_PANIC);
if params.len() == 2;
then {
if is_expn_of(expr.span, "unimplemented").is_some() {

View File

@ -8,7 +8,7 @@ use syntax::ptr::P;
use crate::utils::paths::*;
use crate::utils::sugg::Sugg;
use crate::utils::{match_def_path, match_type, span_lint_and_then, SpanlessEq};
use crate::utils::{match_type, span_lint_and_then, SpanlessEq};
declare_clippy_lint! {
/// **What it does:** Checks for expressions that could be replaced by the question mark operator.
@ -129,7 +129,7 @@ impl Pass {
ExprKind::Ret(Some(ref expr)) => Self::expression_returns_none(cx, expr),
ExprKind::Path(ref qp) => {
if let Def::Ctor(def_id, def::CtorOf::Variant, _) = cx.tables.qpath_def(qp, expression.hir_id) {
return match_def_path(cx.tcx, def_id, &OPTION_NONE);
return cx.match_def_path(def_id, &OPTION_NONE);
}
false

View File

@ -1,5 +1,5 @@
use crate::utils::{
has_drop, in_macro, is_copy, match_def_path, match_type, paths, snippet_opt, span_lint_hir, span_lint_hir_and_then,
has_drop, in_macro, is_copy, match_type, paths, snippet_opt, span_lint_hir, span_lint_hir_and_then,
walk_ptrs_ty_depth,
};
use if_chain::if_chain;
@ -104,14 +104,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
let (fn_def_id, arg, arg_ty, _) = unwrap_or_continue!(is_call_with_ref_arg(cx, mir, &terminator.kind));
let from_borrow = match_def_path(cx.tcx, fn_def_id, &paths::CLONE_TRAIT_METHOD)
|| match_def_path(cx.tcx, fn_def_id, &paths::TO_OWNED_METHOD)
|| (match_def_path(cx.tcx, fn_def_id, &paths::TO_STRING_METHOD)
&& match_type(cx, arg_ty, &paths::STRING));
let from_borrow = cx.match_def_path(fn_def_id, &paths::CLONE_TRAIT_METHOD)
|| cx.match_def_path(fn_def_id, &paths::TO_OWNED_METHOD)
|| (cx.match_def_path(fn_def_id, &paths::TO_STRING_METHOD) && match_type(cx, arg_ty, &paths::STRING));
let from_deref = !from_borrow
&& (match_def_path(cx.tcx, fn_def_id, &paths::PATH_TO_PATH_BUF)
|| match_def_path(cx.tcx, fn_def_id, &paths::OS_STR_TO_OS_STRING));
&& (cx.match_def_path(fn_def_id, &paths::PATH_TO_PATH_BUF)
|| cx.match_def_path(fn_def_id, &paths::OS_STR_TO_OS_STRING));
if !from_borrow && !from_deref {
continue;
@ -144,7 +143,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
if let Some((pred_fn_def_id, pred_arg, pred_arg_ty, Some(res))) =
is_call_with_ref_arg(cx, mir, &pred_terminator.kind);
if *res == mir::Place::Base(mir::PlaceBase::Local(cloned));
if match_def_path(cx.tcx, pred_fn_def_id, &paths::DEREF_TRAIT_METHOD);
if cx.match_def_path(pred_fn_def_id, &paths::DEREF_TRAIT_METHOD);
if match_type(cx, pred_arg_ty, &paths::PATH_BUF)
|| match_type(cx, pred_arg_ty, &paths::OS_STRING);
then {

View File

@ -1,5 +1,5 @@
use crate::consts::{constant, Constant};
use crate::utils::{is_expn_of, match_def_path, match_type, paths, span_help_and_lint, span_lint};
use crate::utils::{is_expn_of, match_type, paths, span_help_and_lint, span_lint};
use if_chain::if_chain;
use regex_syntax;
use rustc::hir::*;
@ -120,15 +120,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if args.len() == 1;
if let Some(def_id) = cx.tables.qpath_def(qpath, fun.hir_id).opt_def_id();
then {
if match_def_path(cx.tcx, def_id, &paths::REGEX_NEW) ||
match_def_path(cx.tcx, def_id, &paths::REGEX_BUILDER_NEW) {
if cx.match_def_path(def_id, &paths::REGEX_NEW) ||
cx.match_def_path(def_id, &paths::REGEX_BUILDER_NEW) {
check_regex(cx, &args[0], true);
} else if match_def_path(cx.tcx, def_id, &paths::REGEX_BYTES_NEW) ||
match_def_path(cx.tcx, def_id, &paths::REGEX_BYTES_BUILDER_NEW) {
} else if cx.match_def_path(def_id, &paths::REGEX_BYTES_NEW) ||
cx.match_def_path(def_id, &paths::REGEX_BYTES_BUILDER_NEW) {
check_regex(cx, &args[0], false);
} else if match_def_path(cx.tcx, def_id, &paths::REGEX_SET_NEW) {
} else if cx.match_def_path(def_id, &paths::REGEX_SET_NEW) {
check_set(cx, &args[0], true);
} else if match_def_path(cx.tcx, def_id, &paths::REGEX_BYTES_SET_NEW) {
} else if cx.match_def_path(def_id, &paths::REGEX_BYTES_SET_NEW) {
check_set(cx, &args[0], false);
}
}

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, span_lint_and_sugg};
use crate::utils::span_lint_and_sugg;
use if_chain::if_chain;
use rustc::hir;
use rustc::hir::def::Def;
@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
if let Def::Const(def_id) = cx.tables.qpath_def(qp, expr.hir_id);
then {
for &(const_path, repl_snip) in REPLACEMENTS {
if match_def_path(cx.tcx, def_id, const_path) {
if cx.match_def_path(def_id, const_path) {
span_lint_and_sugg(
cx,
REPLACE_CONSTS,

View File

@ -1,4 +1,4 @@
use crate::utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then, sugg};
use crate::utils::{last_path_segment, paths, snippet, span_lint, span_lint_and_then, sugg};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@ -231,7 +231,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
if let ExprKind::Call(ref path_expr, ref args) = e.node {
if let ExprKind::Path(ref qpath) = path_expr.node {
if let Some(def_id) = cx.tables.qpath_def(qpath, path_expr.hir_id).opt_def_id() {
if match_def_path(cx.tcx, def_id, &paths::TRANSMUTE) {
if cx.match_def_path(def_id, &paths::TRANSMUTE) {
let from_ty = cx.tables.expr_ty(&args[0]);
let to_ty = cx.tables.expr_ty(e);

View File

@ -10,7 +10,6 @@ use rustc::hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisito
use rustc::hir::*;
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
use rustc::ty::layout::LayoutOf;
use rustc::ty::print::Printer;
use rustc::ty::{self, InferTy, Ty, TyCtxt, TypeckTables};
use rustc::{declare_tool_lint, lint_array};
use rustc_errors::Applicability;
@ -24,8 +23,8 @@ use crate::consts::{constant, Constant};
use crate::utils::paths;
use crate::utils::{
clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro, int_bits, last_path_segment,
match_def_path, match_path, multispan_sugg, same_tys, sext, snippet, snippet_opt, snippet_with_applicability,
span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext, AbsolutePathPrinter,
match_path, multispan_sugg, same_tys, sext, snippet, snippet_opt, snippet_with_applicability, span_help_and_lint,
span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
};
/// Handles all the linting of funky types
@ -229,7 +228,7 @@ fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath, path: &[&str])
});
if let TyKind::Path(ref qpath) = ty.node;
if let Some(did) = cx.tables.qpath_def(qpath, ty.hir_id).opt_def_id();
if match_def_path(cx.tcx, did, path);
if cx.match_def_path(did, path);
then {
return true;
}
@ -263,7 +262,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) {
);
return; // don't recurse into the type
}
} else if match_def_path(cx.tcx, def_id, &paths::VEC) {
} else if cx.match_def_path(def_id, &paths::VEC) {
if_chain! {
// Get the _ part of Vec<_>
if let Some(ref last) = last_path_segment(qpath).args;
@ -298,7 +297,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) {
}
}
}
} else if match_def_path(cx.tcx, def_id, &paths::OPTION) {
} else if cx.match_def_path(def_id, &paths::OPTION) {
if match_type_parameter(cx, qpath, &paths::OPTION) {
span_lint(
cx,
@ -309,7 +308,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) {
);
return; // don't recurse into the type
}
} else if match_def_path(cx.tcx, def_id, &paths::LINKED_LIST) {
} else if cx.match_def_path(def_id, &paths::LINKED_LIST) {
span_help_and_lint(
cx,
LINKEDLIST,
@ -1136,9 +1135,9 @@ impl LintPass for CastPass {
// Check if the given type is either `core::ffi::c_void` or
// one of the platform specific `libc::<platform>::c_void` of libc.
fn is_c_void<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'_>) -> bool {
fn is_c_void(cx: &LateContext<'_, '_>, ty: Ty<'_>) -> bool {
if let ty::Adt(adt, _) = ty.sty {
let names = AbsolutePathPrinter { tcx }.print_def_path(adt.did, &[]).unwrap();
let names = cx.get_def_path(adt.did);
if names.is_empty() {
return false;
@ -1262,7 +1261,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
if let Some(to_align) = cx.layout_of(to_ptr_ty.ty).ok().map(|a| a.align.abi);
if from_align < to_align;
// with c_void, we inherently need to trust the user
if !is_c_void(cx.tcx, from_ptr_ty.ty);
if !is_c_void(cx, from_ptr_ty.ty);
then {
span_lint(
cx,

View File

@ -3,7 +3,7 @@
#![deny(clippy::missing_docs_in_private_items)]
use crate::utils::{is_expn_of, match_def_path, match_qpath, paths, resolve_node};
use crate::utils::{is_expn_of, match_qpath, paths, resolve_node};
use if_chain::if_chain;
use rustc::lint::LateContext;
use rustc::{hir, ty};
@ -216,11 +216,11 @@ pub fn vec_macro<'e>(cx: &LateContext<'_, '_>, expr: &'e hir::Expr) -> Option<Ve
if is_expn_of(fun.span, "vec").is_some();
if let Some(fun_def_id) = resolve_node(cx, path, fun.hir_id).opt_def_id();
then {
return if match_def_path(cx.tcx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 {
return if cx.match_def_path(fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 {
// `vec![elem; size]` case
Some(VecArgs::Repeat(&args[0], &args[1]))
}
else if match_def_path(cx.tcx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 {
else if cx.match_def_path(fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 {
// `vec![a, b, c]` case
if_chain! {
if let hir::ExprKind::Box(ref boxed) = args[0].node;

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, match_type, paths, span_help_and_lint, span_lint, walk_ptrs_ty};
use crate::utils::{match_type, paths, span_help_and_lint, span_lint, walk_ptrs_ty};
use if_chain::if_chain;
use rustc::hir;
use rustc::hir::def::Def;
@ -144,7 +144,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass {
if_chain! {
if let hir::TraitRef{path, ..} = trait_ref;
if let Def::Trait(def_id) = path.def;
if match_def_path(cx.tcx, def_id, &paths::LINT_PASS);
if cx.match_def_path(def_id, &paths::LINT_PASS);
then {
let mut collector = LintCollector {
output: &mut self.registered_lints,
@ -196,7 +196,7 @@ fn is_lint_ref_type<'tcx>(cx: &LateContext<'_, 'tcx>, ty: &Ty) -> bool {
{
if let TyKind::Path(ref path) = inner.node {
if let Def::Struct(def_id) = cx.tables.qpath_def(path, inner.hir_id) {
return match_def_path(cx.tcx, def_id, &paths::LINT);
return cx.match_def_path(def_id, &paths::LINT);
}
}
}

View File

@ -96,7 +96,7 @@ pub fn in_macro(span: Span) -> bool {
/// Checks if type is struct, enum or union type with the given def path.
pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
match ty.sty {
ty::Adt(adt, _) => match_def_path(cx.tcx, adt.did, path),
ty::Adt(adt, _) => cx.match_def_path(adt.did, path),
_ => false,
}
}
@ -106,7 +106,7 @@ pub fn match_trait_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str])
let def_id = cx.tables.type_dependent_def_id(expr.hir_id).unwrap();
let trt_id = cx.tcx.trait_of_item(def_id);
if let Some(trt_id) = trt_id {
match_def_path(cx.tcx, trt_id, path)
cx.match_def_path(trt_id, path)
} else {
false
}
@ -989,7 +989,7 @@ pub fn has_iter_method(cx: &LateContext<'_, '_>, probably_ref_ty: Ty<'_>) -> Opt
};
for path in &INTO_ITER_COLLECTIONS {
if match_def_path(cx.tcx, def_id, path) {
if cx.match_def_path(def_id, path) {
return Some(path.last().unwrap());
}
}