Skip to content

Commit

Permalink
Use {get,match}_def_path from LateContext
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed Apr 7, 2019
1 parent 59e8374 commit bebc8b8
Show file tree
Hide file tree
Showing 25 changed files with 122 additions and 130 deletions.
36 changes: 18 additions & 18 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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;
use rustc::hir::*;
use rustc::lint::{
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;
Expand Down Expand Up @@ -233,7 +233,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 {
Expand Down Expand Up @@ -298,13 +298,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)
}
}
Expand Down Expand Up @@ -357,52 +357,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
}
Expand Down
36 changes: 18 additions & 18 deletions clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -175,7 +175,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,
Expand All @@ -194,11 +194,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,
Expand All @@ -207,7 +207,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,
Expand All @@ -226,7 +226,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))
Expand All @@ -244,7 +244,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);
if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
then {
let value = match impl_ty {
Expand Down Expand Up @@ -275,8 +275,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,
}
},
Expand All @@ -293,10 +293,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)),
Expand All @@ -321,16 +321,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;
}
Expand Down Expand Up @@ -368,9 +368,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),
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/default_trait_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down Expand Up @@ -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(..) => {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/drop_bounds.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/drop_forget_ref.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/explicit_write.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions clippy_lints/src/fallible_impl_from.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
}
Expand All @@ -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>,
}
Expand All @@ -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);
Expand All @@ -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);
}
}
Expand All @@ -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(),
};
Expand All @@ -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,
}
}
Loading

0 comments on commit bebc8b8

Please sign in to comment.