Skip to content

Commit bebc8b8

Browse files
committed
Use {get,match}_def_path from LateContext
1 parent 59e8374 commit bebc8b8

25 files changed

+122
-130
lines changed

clippy_lints/src/attrs.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
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;
99
use rustc::hir::*;
1010
use rustc::lint::{
1111
CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass,
1212
};
13-
use rustc::ty::{self, TyCtxt};
13+
use rustc::ty;
1414
use rustc::{declare_tool_lint, lint_array};
1515
use rustc_errors::Applicability;
1616
use semver::Version;
@@ -233,7 +233,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
233233
}
234234

235235
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
236-
if is_relevant_item(cx.tcx, item) {
236+
if is_relevant_item(cx, item) {
237237
check_attrs(cx, item.span, item.ident.name, &item.attrs)
238238
}
239239
match item.node {
@@ -298,13 +298,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
298298
}
299299

300300
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
301-
if is_relevant_impl(cx.tcx, item) {
301+
if is_relevant_impl(cx, item) {
302302
check_attrs(cx, item.span, item.ident.name, &item.attrs)
303303
}
304304
}
305305

306306
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
307-
if is_relevant_trait(cx.tcx, item) {
307+
if is_relevant_trait(cx, item) {
308308
check_attrs(cx, item.span, item.ident.name, &item.attrs)
309309
}
310310
}
@@ -357,52 +357,52 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
357357
}
358358
}
359359

360-
fn is_relevant_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &Item) -> bool {
360+
fn is_relevant_item(cx: &LateContext<'_, '_>, item: &Item) -> bool {
361361
if let ItemKind::Fn(_, _, _, eid) = item.node {
362-
is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value)
362+
is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value)
363363
} else {
364364
true
365365
}
366366
}
367367

368-
fn is_relevant_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &ImplItem) -> bool {
368+
fn is_relevant_impl(cx: &LateContext<'_, '_>, item: &ImplItem) -> bool {
369369
match item.node {
370-
ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value),
370+
ImplItemKind::Method(_, eid) => is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value),
371371
_ => false,
372372
}
373373
}
374374

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

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

397-
fn is_relevant_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
397+
fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
398398
match &expr.node {
399-
ExprKind::Block(block, _) => is_relevant_block(tcx, tables, block),
400-
ExprKind::Ret(Some(e)) => is_relevant_expr(tcx, tables, e),
399+
ExprKind::Block(block, _) => is_relevant_block(cx, tables, block),
400+
ExprKind::Ret(Some(e)) => is_relevant_expr(cx, tables, e),
401401
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
402402
ExprKind::Call(path_expr, _) => {
403403
if let ExprKind::Path(qpath) = &path_expr.node {
404404
if let Some(fun_id) = tables.qpath_def(qpath, path_expr.hir_id).opt_def_id() {
405-
!match_def_path(tcx, fun_id, &paths::BEGIN_PANIC)
405+
!cx.match_def_path(fun_id, &paths::BEGIN_PANIC)
406406
} else {
407407
true
408408
}

clippy_lints/src/consts.rs

Lines changed: 18 additions & 18 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::*;
@@ -175,7 +175,7 @@ pub fn constant<'c, 'cc>(
175175
e: &Expr,
176176
) -> Option<(Constant, bool)> {
177177
let mut cx = ConstEvalLateContext {
178-
tcx: lcx.tcx,
178+
lcx,
179179
tables,
180180
param_env: lcx.param_env,
181181
needed_resolution: false,
@@ -194,11 +194,11 @@ pub fn constant_simple<'c, 'cc>(
194194

195195
/// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`
196196
pub fn constant_context<'c, 'cc>(
197-
lcx: &LateContext<'c, 'cc>,
197+
lcx: &'c LateContext<'c, 'cc>,
198198
tables: &'c ty::TypeckTables<'cc>,
199199
) -> ConstEvalLateContext<'c, 'cc> {
200200
ConstEvalLateContext {
201-
tcx: lcx.tcx,
201+
lcx,
202202
tables,
203203
param_env: lcx.param_env,
204204
needed_resolution: false,
@@ -207,7 +207,7 @@ pub fn constant_context<'c, 'cc>(
207207
}
208208

209209
pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
210-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
210+
lcx: &'a LateContext<'a, 'tcx>,
211211
tables: &'a ty::TypeckTables<'tcx>,
212212
param_env: ty::ParamEnv<'tcx>,
213213
needed_resolution: bool,
@@ -226,7 +226,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
226226
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
227227
ExprKind::Repeat(ref value, _) => {
228228
let n = match self.tables.expr_ty(e).sty {
229-
ty::Array(_, n) => n.assert_usize(self.tcx).expect("array length"),
229+
ty::Array(_, n) => n.assert_usize(self.lcx.tcx).expect("array length"),
230230
_ => span_bug!(e.span, "typeck error"),
231231
};
232232
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
@@ -244,7 +244,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
244244
if let ExprKind::Path(qpath) = &callee.node;
245245
let def = self.tables.qpath_def(qpath, callee.hir_id);
246246
if let Some(def_id) = def.opt_def_id();
247-
let def_path = get_def_path(self.tcx, def_id);
247+
let def_path = self.lcx.get_def_path(def_id);
248248
if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
249249
then {
250250
let value = match impl_ty {
@@ -275,8 +275,8 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
275275
Int(value) => {
276276
let value = !value;
277277
match ty.sty {
278-
ty::Int(ity) => Some(Int(unsext(self.tcx, value as i128, ity))),
279-
ty::Uint(ity) => Some(Int(clip(self.tcx, value, ity))),
278+
ty::Int(ity) => Some(Int(unsext(self.lcx.tcx, value as i128, ity))),
279+
ty::Uint(ity) => Some(Int(clip(self.lcx.tcx, value, ity))),
280280
_ => None,
281281
}
282282
},
@@ -293,10 +293,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
293293
_ => return None,
294294
};
295295
// sign extend
296-
let value = sext(self.tcx, value, ity);
296+
let value = sext(self.lcx.tcx, value, ity);
297297
let value = value.checked_neg()?;
298298
// clear unused bits
299-
Some(Int(unsext(self.tcx, value, ity)))
299+
Some(Int(unsext(self.lcx.tcx, value, ity)))
300300
},
301301
F32(f) => Some(F32(-f)),
302302
F64(f) => Some(F64(-f)),
@@ -321,16 +321,16 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
321321
let substs = if self.substs.is_empty() {
322322
substs
323323
} else {
324-
substs.subst(self.tcx, self.substs)
324+
substs.subst(self.lcx.tcx, self.substs)
325325
};
326-
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs)?;
326+
let instance = Instance::resolve(self.lcx.tcx, self.param_env, def_id, substs)?;
327327
let gid = GlobalId {
328328
instance,
329329
promoted: None,
330330
};
331331

332-
let result = self.tcx.const_eval(self.param_env.and(gid)).ok()?;
333-
let ret = miri_to_const(self.tcx, &result);
332+
let result = self.lcx.tcx.const_eval(self.param_env.and(gid)).ok()?;
333+
let ret = miri_to_const(self.lcx.tcx, &result);
334334
if ret.is_some() {
335335
self.needed_resolution = true;
336336
}
@@ -368,9 +368,9 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
368368
match (l, r) {
369369
(Constant::Int(l), Some(Constant::Int(r))) => match self.tables.expr_ty(left).sty {
370370
ty::Int(ity) => {
371-
let l = sext(self.tcx, l, ity);
372-
let r = sext(self.tcx, r, ity);
373-
let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
371+
let l = sext(self.lcx.tcx, l, ity);
372+
let r = sext(self.lcx.tcx, r, ity);
373+
let zext = |n: i128| Constant::Int(unsext(self.lcx.tcx, n, ity));
374374
match op.node {
375375
BinOpKind::Add => l.checked_add(r).map(zext),
376376
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)