Skip to content

Commit bb16096

Browse files
committed
Use {get,match}_def_path from LateContext
1 parent 5837f2d commit bb16096

25 files changed

+126
-131
lines changed

clippy_lints/src/attrs.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::reexport::*;
44
use crate::utils::{
5-
in_macro, last_line_of_span, match_def_path, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then,
5+
in_macro, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then,
66
without_block_comments,
77
};
88
use if_chain::if_chain;
@@ -11,7 +11,7 @@ use rustc::lint::{
1111
in_external_macro, CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray,
1212
LintContext, LintPass,
1313
};
14-
use rustc::ty::{self, TyCtxt};
14+
use rustc::ty;
1515
use rustc::{declare_tool_lint, lint_array};
1616
use rustc_errors::Applicability;
1717
use semver::Version;
@@ -234,7 +234,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
234234
}
235235

236236
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
237-
if is_relevant_item(cx.tcx, item) {
237+
if is_relevant_item(cx, item) {
238238
check_attrs(cx, item.span, item.ident.name, &item.attrs)
239239
}
240240
match item.node {
@@ -302,13 +302,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
302302
}
303303

304304
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
305-
if is_relevant_impl(cx.tcx, item) {
305+
if is_relevant_impl(cx, item) {
306306
check_attrs(cx, item.span, item.ident.name, &item.attrs)
307307
}
308308
}
309309

310310
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
311-
if is_relevant_trait(cx.tcx, item) {
311+
if is_relevant_trait(cx, item) {
312312
check_attrs(cx, item.span, item.ident.name, &item.attrs)
313313
}
314314
}
@@ -361,52 +361,52 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
361361
}
362362
}
363363

364-
fn is_relevant_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &Item) -> bool {
364+
fn is_relevant_item(cx: &LateContext<'_, '_>, item: &Item) -> bool {
365365
if let ItemKind::Fn(_, _, _, eid) = item.node {
366-
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value)
366+
is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value)
367367
} else {
368368
true
369369
}
370370
}
371371

372-
fn is_relevant_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &ImplItem) -> bool {
372+
fn is_relevant_impl(cx: &LateContext<'_, '_>, item: &ImplItem) -> bool {
373373
match item.node {
374-
ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value),
374+
ImplItemKind::Method(_, eid) => is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value),
375375
_ => false,
376376
}
377377
}
378378

379-
fn is_relevant_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &TraitItem) -> bool {
379+
fn is_relevant_trait(cx: &LateContext<'_, '_>, item: &TraitItem) -> bool {
380380
match item.node {
381381
TraitItemKind::Method(_, TraitMethod::Required(_)) => true,
382382
TraitItemKind::Method(_, TraitMethod::Provided(eid)) => {
383-
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value)
383+
is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value)
384384
},
385385
_ => false,
386386
}
387387
}
388388

389-
fn is_relevant_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
389+
fn is_relevant_block(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
390390
if let Some(stmt) = block.stmts.first() {
391391
match &stmt.node {
392392
StmtKind::Local(_) => true,
393-
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(tcx, tables, expr),
393+
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, tables, expr),
394394
_ => false,
395395
}
396396
} else {
397-
block.expr.as_ref().map_or(false, |e| is_relevant_expr(tcx, tables, e))
397+
block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e))
398398
}
399399
}
400400

401-
fn is_relevant_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
401+
fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
402402
match &expr.node {
403-
ExprKind::Block(block, _) => is_relevant_block(tcx, tables, block),
404-
ExprKind::Ret(Some(e)) => is_relevant_expr(tcx, tables, e),
403+
ExprKind::Block(block, _) => is_relevant_block(cx, tables, block),
404+
ExprKind::Ret(Some(e)) => is_relevant_expr(cx, tables, e),
405405
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
406406
ExprKind::Call(path_expr, _) => {
407407
if let ExprKind::Path(qpath) = &path_expr.node {
408408
if let Some(fun_id) = tables.qpath_def(qpath, path_expr.hir_id).opt_def_id() {
409-
!match_def_path(tcx, fun_id, &paths::BEGIN_PANIC)
409+
!cx.match_def_path(fun_id, &paths::BEGIN_PANIC)
410410
} else {
411411
true
412412
}

clippy_lints/src/consts.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::float_cmp)]
22

3-
use crate::utils::{clip, get_def_path, sext, unsext};
3+
use crate::utils::{clip, sext, unsext};
44
use if_chain::if_chain;
55
use rustc::hir::def::Def;
66
use rustc::hir::*;
@@ -16,7 +16,7 @@ use std::convert::TryInto;
1616
use std::hash::{Hash, Hasher};
1717
use syntax::ast::{FloatTy, LitKind};
1818
use syntax::ptr::P;
19-
use syntax_pos::symbol::Symbol;
19+
use syntax_pos::symbol::{LocalInternedString, Symbol};
2020

2121
/// A `LitKind`-like enum to fold constant `Expr`s into.
2222
#[derive(Debug, Clone)]
@@ -180,7 +180,7 @@ pub fn constant<'c, 'cc>(
180180
e: &Expr,
181181
) -> Option<(Constant, bool)> {
182182
let mut cx = ConstEvalLateContext {
183-
tcx: lcx.tcx,
183+
lcx,
184184
tables,
185185
param_env: lcx.param_env,
186186
needed_resolution: false,
@@ -199,11 +199,11 @@ pub fn constant_simple<'c, 'cc>(
199199

200200
/// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`.
201201
pub fn constant_context<'c, 'cc>(
202-
lcx: &LateContext<'c, 'cc>,
202+
lcx: &'c LateContext<'c, 'cc>,
203203
tables: &'c ty::TypeckTables<'cc>,
204204
) -> ConstEvalLateContext<'c, 'cc> {
205205
ConstEvalLateContext {
206-
tcx: lcx.tcx,
206+
lcx,
207207
tables,
208208
param_env: lcx.param_env,
209209
needed_resolution: false,
@@ -212,7 +212,7 @@ pub fn constant_context<'c, 'cc>(
212212
}
213213

214214
pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
215-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
215+
lcx: &'a LateContext<'a, 'tcx>,
216216
tables: &'a ty::TypeckTables<'tcx>,
217217
param_env: ty::ParamEnv<'tcx>,
218218
needed_resolution: bool,
@@ -231,7 +231,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
231231
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
232232
ExprKind::Repeat(ref value, _) => {
233233
let n = match self.tables.expr_ty(e).sty {
234-
ty::Array(_, n) => n.assert_usize(self.tcx).expect("array length"),
234+
ty::Array(_, n) => n.assert_usize(self.lcx.tcx).expect("array length"),
235235
_ => span_bug!(e.span, "typeck error"),
236236
};
237237
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
@@ -249,7 +249,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
249249
if let ExprKind::Path(qpath) = &callee.node;
250250
let def = self.tables.qpath_def(qpath, callee.hir_id);
251251
if let Some(def_id) = def.opt_def_id();
252-
let def_path = get_def_path(self.tcx, def_id);
252+
let def_path = self.lcx.get_def_path(def_id)
253+
.iter()
254+
.map(LocalInternedString::get)
255+
.collect::<Vec<_>>();
253256
if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
254257
then {
255258
let value = match impl_ty {
@@ -280,8 +283,8 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
280283
Int(value) => {
281284
let value = !value;
282285
match ty.sty {
283-
ty::Int(ity) => Some(Int(unsext(self.tcx, value as i128, ity))),
284-
ty::Uint(ity) => Some(Int(clip(self.tcx, value, ity))),
286+
ty::Int(ity) => Some(Int(unsext(self.lcx.tcx, value as i128, ity))),
287+
ty::Uint(ity) => Some(Int(clip(self.lcx.tcx, value, ity))),
285288
_ => None,
286289
}
287290
},
@@ -298,10 +301,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
298301
_ => return None,
299302
};
300303
// sign extend
301-
let value = sext(self.tcx, value, ity);
304+
let value = sext(self.lcx.tcx, value, ity);
302305
let value = value.checked_neg()?;
303306
// clear unused bits
304-
Some(Int(unsext(self.tcx, value, ity)))
307+
Some(Int(unsext(self.lcx.tcx, value, ity)))
305308
},
306309
F32(f) => Some(F32(-f)),
307310
F64(f) => Some(F64(-f)),
@@ -326,16 +329,16 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
326329
let substs = if self.substs.is_empty() {
327330
substs
328331
} else {
329-
substs.subst(self.tcx, self.substs)
332+
substs.subst(self.lcx.tcx, self.substs)
330333
};
331-
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs)?;
334+
let instance = Instance::resolve(self.lcx.tcx, self.param_env, def_id, substs)?;
332335
let gid = GlobalId {
333336
instance,
334337
promoted: None,
335338
};
336339

337-
let result = self.tcx.const_eval(self.param_env.and(gid)).ok()?;
338-
let ret = miri_to_const(self.tcx, &result);
340+
let result = self.lcx.tcx.const_eval(self.param_env.and(gid)).ok()?;
341+
let ret = miri_to_const(self.lcx.tcx, &result);
339342
if ret.is_some() {
340343
self.needed_resolution = true;
341344
}
@@ -373,9 +376,9 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
373376
match (l, r) {
374377
(Constant::Int(l), Some(Constant::Int(r))) => match self.tables.expr_ty(left).sty {
375378
ty::Int(ity) => {
376-
let l = sext(self.tcx, l, ity);
377-
let r = sext(self.tcx, r, ity);
378-
let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
379+
let l = sext(self.lcx.tcx, l, ity);
380+
let r = sext(self.lcx.tcx, r, ity);
381+
let zext = |n: i128| Constant::Int(unsext(self.lcx.tcx, n, ity));
379382
match op.node {
380383
BinOpKind::Add => l.checked_add(r).map(zext),
381384
BinOpKind::Sub => l.checked_sub(r).map(zext),

clippy_lints/src/default_trait_access.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::ty;
55
use rustc::{declare_tool_lint, lint_array};
66
use rustc_errors::Applicability;
77

8-
use crate::utils::{any_parent_is_automatically_derived, match_def_path, paths, span_lint_and_sugg};
8+
use crate::utils::{any_parent_is_automatically_derived, paths, span_lint_and_sugg};
99

1010
declare_clippy_lint! {
1111
/// **What it does:** Checks for literal calls to `Default::default()`.
@@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
4848
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
4949
if let ExprKind::Path(ref qpath) = path.node;
5050
if let Some(def_id) = cx.tables.qpath_def(qpath, path.hir_id).opt_def_id();
51-
if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD);
51+
if cx.match_def_path(def_id, &paths::DEFAULT_TRAIT_METHOD);
5252
then {
5353
match qpath {
5454
QPath::Resolved(..) => {

clippy_lints/src/drop_bounds.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{match_def_path, paths, span_lint};
1+
use crate::utils::{paths, span_lint};
22
use if_chain::if_chain;
33
use rustc::hir::*;
44
use rustc::lint::{LateLintPass, LintArray, LintPass};
@@ -66,7 +66,7 @@ fn lint_bound<'a, 'tcx>(cx: &rustc::lint::LateContext<'a, 'tcx>, bound: &'tcx Ge
6666
if_chain! {
6767
if let GenericBound::Trait(t, _) = bound;
6868
if let Some(def_id) = t.trait_ref.path.def.opt_def_id();
69-
if match_def_path(cx.tcx, def_id, &paths::DROP_TRAIT);
69+
if cx.match_def_path(def_id, &paths::DROP_TRAIT);
7070
then {
7171
span_lint(
7272
cx,

clippy_lints/src/drop_forget_ref.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_copy, match_def_path, paths, span_note_and_lint};
1+
use crate::utils::{is_copy, paths, span_note_and_lint};
22
use if_chain::if_chain;
33
use rustc::hir::*;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -132,10 +132,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
132132
let arg_ty = cx.tables.expr_ty(arg);
133133

134134
if let ty::Ref(..) = arg_ty.sty {
135-
if match_def_path(cx.tcx, def_id, &paths::DROP) {
135+
if cx.match_def_path(def_id, &paths::DROP) {
136136
lint = DROP_REF;
137137
msg = DROP_REF_SUMMARY.to_string();
138-
} else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
138+
} else if cx.match_def_path(def_id, &paths::MEM_FORGET) {
139139
lint = FORGET_REF;
140140
msg = FORGET_REF_SUMMARY.to_string();
141141
} else {
@@ -148,10 +148,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
148148
arg.span,
149149
&format!("argument has type {}", arg_ty));
150150
} else if is_copy(cx, arg_ty) {
151-
if match_def_path(cx.tcx, def_id, &paths::DROP) {
151+
if cx.match_def_path(def_id, &paths::DROP) {
152152
lint = DROP_COPY;
153153
msg = DROP_COPY_SUMMARY.to_string();
154-
} else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
154+
} else if cx.match_def_path(def_id, &paths::MEM_FORGET) {
155155
lint = FORGET_COPY;
156156
msg = FORGET_COPY_SUMMARY.to_string();
157157
} else {

clippy_lints/src/explicit_write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_expn_of, match_def_path, resolve_node, span_lint, span_lint_and_sugg};
1+
use crate::utils::{is_expn_of, resolve_node, span_lint, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc::hir::*;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -54,9 +54,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
5454
if let ExprKind::Path(ref qpath) = dest_fun.node;
5555
if let Some(dest_fun_id) =
5656
resolve_node(cx, qpath, dest_fun.hir_id).opt_def_id();
57-
if let Some(dest_name) = if match_def_path(cx.tcx, dest_fun_id, &["std", "io", "stdio", "stdout"]) {
57+
if let Some(dest_name) = if cx.match_def_path(dest_fun_id, &["std", "io", "stdio", "stdout"]) {
5858
Some("stdout")
59-
} else if match_def_path(cx.tcx, dest_fun_id, &["std", "io", "stdio", "stderr"]) {
59+
} else if cx.match_def_path(dest_fun_id, &["std", "io", "stdio", "stderr"]) {
6060
Some("stderr")
6161
} else {
6262
None

clippy_lints/src/fallible_impl_from.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT, OPTION, RESULT};
2-
use crate::utils::{is_expn_of, match_def_path, method_chain_args, span_lint_and_then, walk_ptrs_ty};
2+
use crate::utils::{is_expn_of, method_chain_args, span_lint_and_then, walk_ptrs_ty};
33
use if_chain::if_chain;
44
use rustc::hir;
55
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -47,7 +47,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FallibleImplFrom {
4747
if_chain! {
4848
if let hir::ItemKind::Impl(.., ref impl_items) = item.node;
4949
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id);
50-
if match_def_path(cx.tcx, impl_trait_ref.def_id, &FROM_TRAIT);
50+
if cx.match_def_path(impl_trait_ref.def_id, &FROM_TRAIT);
5151
then {
5252
lint_impl_body(cx, item.span, impl_items);
5353
}
@@ -60,7 +60,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
6060
use rustc::hir::*;
6161

6262
struct FindPanicUnwrap<'a, 'tcx: 'a> {
63-
tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
63+
lcx: &'a LateContext<'a, 'tcx>,
6464
tables: &'tcx ty::TypeckTables<'tcx>,
6565
result: Vec<Span>,
6666
}
@@ -72,8 +72,8 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
7272
if let ExprKind::Call(ref func_expr, _) = expr.node;
7373
if let ExprKind::Path(QPath::Resolved(_, ref path)) = func_expr.node;
7474
if let Some(path_def_id) = path.def.opt_def_id();
75-
if match_def_path(self.tcx, path_def_id, &BEGIN_PANIC) ||
76-
match_def_path(self.tcx, path_def_id, &BEGIN_PANIC_FMT);
75+
if self.lcx.match_def_path(path_def_id, &BEGIN_PANIC) ||
76+
self.lcx.match_def_path(path_def_id, &BEGIN_PANIC_FMT);
7777
if is_expn_of(expr.span, "unreachable").is_none();
7878
then {
7979
self.result.push(expr.span);
@@ -83,7 +83,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
8383
// check for `unwrap`
8484
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
8585
let reciever_ty = walk_ptrs_ty(self.tables.expr_ty(&arglists[0][0]));
86-
if match_type(self.tcx, reciever_ty, &OPTION) || match_type(self.tcx, reciever_ty, &RESULT) {
86+
if match_type(self.lcx, reciever_ty, &OPTION) || match_type(self.lcx, reciever_ty, &RESULT) {
8787
self.result.push(expr.span);
8888
}
8989
}
@@ -107,7 +107,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
107107
let body = cx.tcx.hir().body(body_id);
108108
let impl_item_def_id = cx.tcx.hir().local_def_id_from_hir_id(impl_item.id.hir_id);
109109
let mut fpu = FindPanicUnwrap {
110-
tcx: cx.tcx,
110+
lcx: cx,
111111
tables: cx.tcx.typeck_tables_of(impl_item_def_id),
112112
result: Vec::new(),
113113
};
@@ -132,9 +132,9 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
132132
}
133133
}
134134

135-
fn match_type<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'_>, path: &[&str]) -> bool {
135+
fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
136136
match ty.sty {
137-
ty::Adt(adt, _) => match_def_path(tcx, adt.did, path),
137+
ty::Adt(adt, _) => cx.match_def_path(adt.did, path),
138138
_ => false,
139139
}
140140
}

0 commit comments

Comments
 (0)