Skip to content

Commit f5ce0e5

Browse files
committed
rustc_lint: only query typeck_tables_of when a lint needs it.
1 parent 80bcbf5 commit f5ce0e5

File tree

100 files changed

+361
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+361
-366
lines changed

clippy_lints/src/arithmetic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
8686
_ => (),
8787
}
8888

89-
let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
89+
let (l_ty, r_ty) = (cx.tables().expr_ty(l), cx.tables().expr_ty(r));
9090
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
9191
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
9292
self.expr_span = Some(expr.span);
@@ -96,8 +96,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
9696
}
9797
},
9898
hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
99-
let ty = cx.tables.expr_ty(arg);
100-
if constant_simple(cx, cx.tables, expr).is_none() {
99+
let ty = cx.tables().expr_ty(arg);
100+
if constant_simple(cx, cx.tables(), expr).is_none() {
101101
if ty.is_integral() {
102102
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
103103
self.expr_span = Some(expr.span);

clippy_lints/src/assertions_on_constants.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
7272
}
7373
if_chain! {
7474
if let ExprKind::Unary(_, ref lit) = e.kind;
75-
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit);
75+
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables(), lit);
7676
if is_true;
7777
then {
7878
lint_true(true);
@@ -121,7 +121,7 @@ fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx E
121121
if let ExprKind::DropTemps(ref expr) = expr.kind;
122122
if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
123123
// bind the first argument of the `assert!` macro
124-
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr);
124+
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables(), expr);
125125
// arm 1 pattern
126126
if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
127127
if let ExprKind::Lit(ref lit) = lit_expr.kind;

clippy_lints/src/assign_ops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
8282
hir::ExprKind::Assign(assignee, e, _) => {
8383
if let hir::ExprKind::Binary(op, l, r) = &e.kind {
8484
let lint = |assignee: &hir::Expr<'_>, rhs: &hir::Expr<'_>| {
85-
let ty = cx.tables.expr_ty(assignee);
86-
let rty = cx.tables.expr_ty(rhs);
85+
let ty = cx.tables().expr_ty(assignee);
86+
let rty = cx.tables().expr_ty(rhs);
8787
macro_rules! ops {
8888
($op:expr,
8989
$cx:expr,
@@ -167,7 +167,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
167167
// a = b commutative_op a
168168
// Limited to primitive type as these ops are know to be commutative
169169
if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r)
170-
&& cx.tables.expr_ty(assignee).is_primitive_ty()
170+
&& cx.tables().expr_ty(assignee).is_primitive_ty()
171171
{
172172
match op.node {
173173
hir::BinOpKind::Add

clippy_lints/src/atomic_ordering.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const ATOMIC_TYPES: [&str; 12] = [
5353
];
5454

5555
fn type_is_atomic(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
56-
if let ty::Adt(&ty::AdtDef { did, .. }, _) = cx.tables.expr_ty(expr).kind {
56+
if let ty::Adt(&ty::AdtDef { did, .. }, _) = cx.tables().expr_ty(expr).kind {
5757
ATOMIC_TYPES
5858
.iter()
5959
.any(|ty| match_def_path(cx, did, &["core", "sync", "atomic", ty]))
@@ -76,7 +76,7 @@ fn check_atomic_load_store(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
7676
if method == "load" || method == "store";
7777
let ordering_arg = if method == "load" { &args[1] } else { &args[2] };
7878
if let ExprKind::Path(ref ordering_qpath) = ordering_arg.kind;
79-
if let Some(ordering_def_id) = cx.tables.qpath_res(ordering_qpath, ordering_arg.hir_id).opt_def_id();
79+
if let Some(ordering_def_id) = cx.tables().qpath_res(ordering_qpath, ordering_arg.hir_id).opt_def_id();
8080
then {
8181
if method == "load" &&
8282
match_ordering_def_path(cx, ordering_def_id, &["Release", "AcqRel"]) {
@@ -107,12 +107,12 @@ fn check_memory_fence(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
107107
if_chain! {
108108
if let ExprKind::Call(ref func, ref args) = expr.kind;
109109
if let ExprKind::Path(ref func_qpath) = func.kind;
110-
if let Some(def_id) = cx.tables.qpath_res(func_qpath, func.hir_id).opt_def_id();
110+
if let Some(def_id) = cx.tables().qpath_res(func_qpath, func.hir_id).opt_def_id();
111111
if ["fence", "compiler_fence"]
112112
.iter()
113113
.any(|func| match_def_path(cx, def_id, &["core", "sync", "atomic", func]));
114114
if let ExprKind::Path(ref ordering_qpath) = &args[0].kind;
115-
if let Some(ordering_def_id) = cx.tables.qpath_res(ordering_qpath, args[0].hir_id).opt_def_id();
115+
if let Some(ordering_def_id) = cx.tables().qpath_res(ordering_qpath, args[0].hir_id).opt_def_id();
116116
if match_ordering_def_path(cx, ordering_def_id, &["Relaxed"]);
117117
then {
118118
span_lint_and_help(

clippy_lints/src/bit_mask.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ fn check_ineffective_gt(cx: &LateContext<'_, '_>, span: Span, m: u128, c: u128,
319319
}
320320

321321
fn fetch_int_literal(cx: &LateContext<'_, '_>, lit: &Expr<'_>) -> Option<u128> {
322-
match constant(cx, cx.tables, lit)?.0 {
322+
match constant(cx, cx.tables(), lit)?.0 {
323323
Constant::Int(n) => Some(n),
324324
_ => None,
325325
}

clippy_lints/src/booleans.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option<String> {
248248
})
249249
},
250250
ExprKind::MethodCall(path, _, args, _) if args.len() == 1 => {
251-
let type_of_receiver = cx.tables.expr_ty(&args[0]);
251+
let type_of_receiver = cx.tables().expr_ty(&args[0]);
252252
if !is_type_diagnostic_item(cx, type_of_receiver, sym!(option_type))
253253
&& !is_type_diagnostic_item(cx, type_of_receiver, sym!(result_type))
254254
{
@@ -450,7 +450,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
450450
self.bool_expr(e)
451451
},
452452
ExprKind::Unary(UnOp::UnNot, inner) => {
453-
if self.cx.tables.node_types()[inner.hir_id].is_bool() {
453+
if self.cx.tables().node_types()[inner.hir_id].is_bool() {
454454
self.bool_expr(e);
455455
} else {
456456
walk_expr(self, e);
@@ -465,7 +465,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
465465
}
466466

467467
fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr<'_>) -> bool {
468-
let ty = cx.tables.expr_ty(expr);
468+
let ty = cx.tables().expr_ty(expr);
469469
get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]))
470470
}
471471

clippy_lints/src/bytecount.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
5353
if let ExprKind::Binary(ref op, ref l, ref r) = body.value.kind;
5454
if op.node == BinOpKind::Eq;
5555
if match_type(cx,
56-
walk_ptrs_ty(cx.tables.expr_ty(&filter_args[0])),
56+
walk_ptrs_ty(cx.tables().expr_ty(&filter_args[0])),
5757
&paths::SLICE_ITER);
5858
then {
5959
let needle = match get_path_name(l) {
@@ -63,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
6363
_ => { return; }
6464
}
6565
};
66-
if ty::Uint(UintTy::U8) != walk_ptrs_ty(cx.tables.expr_ty(needle)).kind {
66+
if ty::Uint(UintTy::U8) != walk_ptrs_ty(cx.tables().expr_ty(needle)).kind {
6767
return;
6868
}
6969
let haystack = if let ExprKind::MethodCall(ref path, _, ref args, _) =

clippy_lints/src/cognitive_complexity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl CognitiveComplexity {
6060
let mut helper = CCHelper { cc: 1, returns: 0 };
6161
helper.visit_expr(expr);
6262
let CCHelper { cc, returns } = helper;
63-
let ret_ty = cx.tables.node_type(expr.hir_id);
63+
let ret_ty = cx.tables().node_type(expr.hir_id);
6464
let ret_adjust = if is_type_diagnostic_item(cx, ret_ty, sym!(result_type)) {
6565
returns
6666
} else {

clippy_lints/src/comparison_chain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ComparisonChain {
9999
}
100100

101101
// Check that the type being compared implements `core::cmp::Ord`
102-
let ty = cx.tables.expr_ty(lhs1);
102+
let ty = cx.tables().expr_ty(lhs1);
103103
let is_ord = get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]));
104104

105105
if !is_ord {

clippy_lints/src/copies.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn lint_same_then_else(cx: &LateContext<'_, '_>, blocks: &[&Block<'_>]) {
192192
/// Implementation of `IFS_SAME_COND`.
193193
fn lint_same_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
194194
let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
195-
let mut h = SpanlessHash::new(cx, cx.tables);
195+
let mut h = SpanlessHash::new(cx, cx.tables());
196196
h.hash_expr(expr);
197197
h.finish()
198198
};
@@ -215,7 +215,7 @@ fn lint_same_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
215215
/// Implementation of `SAME_FUNCTIONS_IN_IF_CONDITION`.
216216
fn lint_same_fns_in_if_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
217217
let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
218-
let mut h = SpanlessHash::new(cx, cx.tables);
218+
let mut h = SpanlessHash::new(cx, cx.tables());
219219
h.hash_expr(expr);
220220
h.finish()
221221
};
@@ -251,7 +251,7 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr<'_>) {
251251

252252
if let ExprKind::Match(_, ref arms, MatchSource::Normal) = expr.kind {
253253
let hash = |&(_, arm): &(usize, &Arm<'_>)| -> u64 {
254-
let mut h = SpanlessHash::new(cx, cx.tables);
254+
let mut h = SpanlessHash::new(cx, cx.tables());
255255
h.hash_expr(&arm.body);
256256
h.finish()
257257
};
@@ -320,7 +320,7 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat<'_>) -> FxHashMap<Sy
320320
},
321321
PatKind::Binding(.., ident, ref as_pat) => {
322322
if let Entry::Vacant(v) = map.entry(ident.name) {
323-
v.insert(cx.tables.pat_ty(pat));
323+
v.insert(cx.tables().pat_ty(pat));
324324
}
325325
if let Some(ref as_pat) = *as_pat {
326326
bindings_impl(cx, as_pat, map);

clippy_lints/src/default_trait_access.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
3636
if let ExprKind::Call(ref path, ..) = expr.kind;
3737
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
3838
if let ExprKind::Path(ref qpath) = path.kind;
39-
if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id();
39+
if let Some(def_id) = cx.tables().qpath_res(qpath, path.hir_id).opt_def_id();
4040
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
4141
then {
4242
match qpath {
@@ -54,7 +54,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
5454

5555
// TODO: Work out a way to put "whatever the imported way of referencing
5656
// this type in this file" rather than a fully-qualified type.
57-
let expr_ty = cx.tables.expr_ty(expr);
57+
let expr_ty = cx.tables().expr_ty(expr);
5858
if let ty::Adt(..) = expr_ty.kind {
5959
let replacement = format!("{}::default()", expr_ty);
6060
span_lint_and_sugg(

clippy_lints/src/dereference.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Dereferencing {
7373
fn lint_deref(cx: &LateContext<'_, '_>, method_name: &str, call_expr: &Expr<'_>, var_span: Span, expr_span: Span) {
7474
match method_name {
7575
"deref" => {
76-
if cx
77-
.tcx
78-
.lang_items()
79-
.deref_trait()
80-
.map_or(false, |id| implements_trait(cx, cx.tables.expr_ty(&call_expr), id, &[]))
81-
{
76+
if cx.tcx.lang_items().deref_trait().map_or(false, |id| {
77+
implements_trait(cx, cx.tables().expr_ty(&call_expr), id, &[])
78+
}) {
8279
span_lint_and_sugg(
8380
cx,
8481
EXPLICIT_DEREF_METHODS,
@@ -91,12 +88,9 @@ fn lint_deref(cx: &LateContext<'_, '_>, method_name: &str, call_expr: &Expr<'_>,
9188
}
9289
},
9390
"deref_mut" => {
94-
if cx
95-
.tcx
96-
.lang_items()
97-
.deref_mut_trait()
98-
.map_or(false, |id| implements_trait(cx, cx.tables.expr_ty(&call_expr), id, &[]))
99-
{
91+
if cx.tcx.lang_items().deref_mut_trait().map_or(false, |id| {
92+
implements_trait(cx, cx.tables().expr_ty(&call_expr), id, &[])
93+
}) {
10094
span_lint_and_sugg(
10195
cx,
10296
EXPLICIT_DEREF_METHODS,

clippy_lints/src/drop_forget_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
119119
let lint;
120120
let msg;
121121
let arg = &args[0];
122-
let arg_ty = cx.tables.expr_ty(arg);
122+
let arg_ty = cx.tables().expr_ty(arg);
123123

124124
if let ty::Ref(..) = arg_ty.kind {
125125
if match_def_path(cx, def_id, &paths::DROP) {

clippy_lints/src/duration_subsec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec {
4343
if_chain! {
4444
if let ExprKind::Binary(Spanned { node: BinOpKind::Div, .. }, ref left, ref right) = expr.kind;
4545
if let ExprKind::MethodCall(ref method_path, _ , ref args, _) = left.kind;
46-
if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(&args[0])), &paths::DURATION);
47-
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.tables, right);
46+
if match_type(cx, walk_ptrs_ty(cx.tables().expr_ty(&args[0])), &paths::DURATION);
47+
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.tables(), right);
4848
then {
4949
let suggested_fn = match (method_path.ident.as_str().as_ref(), divisor) {
5050
("subsec_micros", 1_000) | ("subsec_nanos", 1_000_000) => "subsec_millis",

clippy_lints/src/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn check_cond<'a, 'tcx, 'b>(
109109
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref key) = params[1].kind;
110110
then {
111111
let map = &params[0];
112-
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(map));
112+
let obj_ty = walk_ptrs_ty(cx.tables().expr_ty(map));
113113

114114
return if match_type(cx, obj_ty, &paths::BTREEMAP) {
115115
Some(("BTreeMap", map, key))

clippy_lints/src/eq_op.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
103103
(&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
104104
// &foo == &bar
105105
(&ExprKind::AddrOf(BorrowKind::Ref, _, ref l), &ExprKind::AddrOf(BorrowKind::Ref, _, ref r)) => {
106-
let lty = cx.tables.expr_ty(l);
107-
let rty = cx.tables.expr_ty(r);
106+
let lty = cx.tables().expr_ty(l);
107+
let rty = cx.tables().expr_ty(r);
108108
let lcpy = is_copy(cx, lty);
109109
let rcpy = is_copy(cx, rty);
110110
// either operator autorefs or both args are copyable
@@ -126,7 +126,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
126126
)
127127
} else if lcpy
128128
&& !rcpy
129-
&& implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
129+
&& implements_trait(cx, lty, trait_id, &[cx.tables().expr_ty(right).into()])
130130
{
131131
span_lint_and_then(
132132
cx,
@@ -145,7 +145,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
145145
)
146146
} else if !lcpy
147147
&& rcpy
148-
&& implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
148+
&& implements_trait(cx, cx.tables().expr_ty(left), trait_id, &[rty.into()])
149149
{
150150
span_lint_and_then(
151151
cx,
@@ -166,10 +166,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
166166
},
167167
// &foo == bar
168168
(&ExprKind::AddrOf(BorrowKind::Ref, _, ref l), _) => {
169-
let lty = cx.tables.expr_ty(l);
169+
let lty = cx.tables().expr_ty(l);
170170
let lcpy = is_copy(cx, lty);
171171
if (requires_ref || lcpy)
172-
&& implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
172+
&& implements_trait(cx, lty, trait_id, &[cx.tables().expr_ty(right).into()])
173173
{
174174
span_lint_and_then(
175175
cx,
@@ -190,10 +190,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
190190
},
191191
// foo == &bar
192192
(_, &ExprKind::AddrOf(BorrowKind::Ref, _, ref r)) => {
193-
let rty = cx.tables.expr_ty(r);
193+
let rty = cx.tables().expr_ty(r);
194194
let rcpy = is_copy(cx, rty);
195195
if (requires_ref || rcpy)
196-
&& implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
196+
&& implements_trait(cx, cx.tables().expr_ty(left), trait_id, &[rty.into()])
197197
{
198198
span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |diag| {
199199
let rsnip = snippet(cx, r.span, "...").to_string();

clippy_lints/src/erasing_op.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
4848
}
4949

5050
fn check(cx: &LateContext<'_, '_>, e: &Expr<'_>, span: Span) {
51-
if let Some(Constant::Int(0)) = constant_simple(cx, cx.tables, e) {
51+
if let Some(Constant::Int(0)) = constant_simple(cx, cx.tables(), e) {
5252
span_lint(
5353
cx,
5454
ERASING_OP,

clippy_lints/src/escape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
8484

8585
let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
8686
cx.tcx.infer_ctxt().enter(|infcx| {
87-
ExprUseVisitor::new(&mut v, &infcx, fn_def_id, cx.param_env, cx.tables).consume_body(body);
87+
ExprUseVisitor::new(&mut v, &infcx, fn_def_id, cx.param_env, cx.tables()).consume_body(body);
8888
});
8989

9090
for node in v.set {

0 commit comments

Comments
 (0)