Skip to content

Commit dc5a5a4

Browse files
authored
Merge pull request #2763 from phansch/tasty_ice_cream
Fix SpanlessHash and SpanlessEq tables
2 parents 3c068d7 + ed885dc commit dc5a5a4

21 files changed

+132
-51
lines changed

clippy_lints/src/array_indexing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
6464
let size = size.assert_usize(cx.tcx).unwrap().into();
6565

6666
// Index is a constant uint
67-
if let Some((Constant::Int(const_index), _)) = constant(cx, index) {
67+
if let Some((Constant::Int(const_index), _)) = constant(cx, cx.tables, index) {
6868
if size <= const_index {
6969
utils::span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span, "const index is out of bounds");
7070
}
@@ -101,14 +101,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
101101
/// Returns an option containing a tuple with the start and end (exclusive) of
102102
/// the range.
103103
fn to_const_range<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, range: Range, array_size: u128) -> Option<(u128, u128)> {
104-
let s = range.start.map(|expr| constant(cx, expr).map(|(c, _)| c));
104+
let s = range.start.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
105105
let start = match s {
106106
Some(Some(Constant::Int(x))) => x,
107107
Some(_) => return None,
108108
None => 0,
109109
};
110110

111-
let e = range.end.map(|expr| constant(cx, expr).map(|(c, _)| c));
111+
let e = range.end.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
112112
let end = match e {
113113
Some(Some(Constant::Int(x))) => if range.limits == RangeLimits::Closed {
114114
x + 1

clippy_lints/src/bit_mask.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ fn check_ineffective_gt(cx: &LateContext, span: Span, m: u128, c: u128, op: &str
301301
}
302302

303303
fn fetch_int_literal(cx: &LateContext, lit: &Expr) -> Option<u128> {
304-
match constant(cx, lit)?.0 {
304+
match constant(cx, cx.tables, lit)?.0 {
305305
Constant::Int(n) => Some(n),
306306
_ => None,
307307
}

clippy_lints/src/consts.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,23 @@ pub fn lit_to_constant<'tcx>(lit: &LitKind, ty: Ty<'tcx>) -> Constant {
163163
}
164164
}
165165

166-
pub fn constant(lcx: &LateContext, e: &Expr) -> Option<(Constant, bool)> {
166+
pub fn constant<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'c ty::TypeckTables<'cc>, e: &Expr) -> Option<(Constant, bool)> {
167167
let mut cx = ConstEvalLateContext {
168168
tcx: lcx.tcx,
169-
tables: lcx.tables,
169+
tables,
170170
param_env: lcx.param_env,
171171
needed_resolution: false,
172172
substs: lcx.tcx.intern_substs(&[]),
173173
};
174174
cx.expr(e).map(|cst| (cst, cx.needed_resolution))
175175
}
176176

177-
pub fn constant_simple(lcx: &LateContext, e: &Expr) -> Option<Constant> {
178-
constant(lcx, e).and_then(|(cst, res)| if res { None } else { Some(cst) })
177+
pub fn constant_simple<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'c ty::TypeckTables<'cc>, e: &Expr) -> Option<Constant> {
178+
constant(lcx, tables, e).and_then(|(cst, res)| if res { None } else { Some(cst) })
179179
}
180180

181181
/// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`
182-
pub fn constant_context<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'cc ty::TypeckTables<'cc>) -> ConstEvalLateContext<'c, 'cc> {
182+
pub fn constant_context<'c, 'cc>(lcx: &LateContext<'c, 'cc>, tables: &'c ty::TypeckTables<'cc>) -> ConstEvalLateContext<'c, 'cc> {
183183
ConstEvalLateContext {
184184
tcx: lcx.tcx,
185185
tables,

clippy_lints/src/copies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn lint_same_then_else(cx: &LateContext, blocks: &[&Block]) {
151151
/// Implementation of `IFS_SAME_COND`.
152152
fn lint_same_cond(cx: &LateContext, conds: &[&Expr]) {
153153
let hash: &Fn(&&Expr) -> u64 = &|expr| -> u64 {
154-
let mut h = SpanlessHash::new(cx);
154+
let mut h = SpanlessHash::new(cx, cx.tables);
155155
h.hash_expr(expr);
156156
h.finish()
157157
};
@@ -174,7 +174,7 @@ fn lint_same_cond(cx: &LateContext, conds: &[&Expr]) {
174174
fn lint_match_arms(cx: &LateContext, expr: &Expr) {
175175
if let ExprMatch(_, ref arms, MatchSource::Normal) = expr.node {
176176
let hash = |&(_, arm): &(usize, &Arm)| -> u64 {
177-
let mut h = SpanlessHash::new(cx);
177+
let mut h = SpanlessHash::new(cx, cx.tables);
178178
h.hash_expr(&arm.body);
179179
h.finish()
180180
};

clippy_lints/src/double_comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a, 'tcx> DoubleComparisonPass {
5252
}
5353
_ => return,
5454
};
55-
let spanless_eq = SpanlessEq::new(cx).ignore_fn();
55+
let mut spanless_eq = SpanlessEq::new(cx).ignore_fn();
5656
if !(spanless_eq.eq_expr(&llhs, &rlhs) && spanless_eq.eq_expr(&lrhs, &rrhs)) {
5757
return;
5858
}

clippy_lints/src/erasing_op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
5050
}
5151

5252
fn check(cx: &LateContext, e: &Expr, span: Span) {
53-
if let Some(Constant::Int(v)) = constant_simple(cx, e) {
53+
if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
5454
if v == 0 {
5555
span_lint(
5656
cx,

clippy_lints/src/identity_op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
6060

6161
#[allow(cast_possible_wrap)]
6262
fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
63-
if let Some(Constant::Int(v)) = constant_simple(cx, e) {
63+
if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
6464
let check = match cx.tables.expr_ty(e).sty {
6565
ty::TyInt(ity) => unsext(cx.tcx, -1i128, ity),
6666
ty::TyUint(uty) => clip(cx.tcx, !0, uty),

clippy_lints/src/loops.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,8 @@ fn check_for_loop_reverse_range<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arg: &'tcx
11201120
}) = higher::range(cx, arg)
11211121
{
11221122
// ...and both sides are compile-time constant integers...
1123-
if let Some((start_idx, _)) = constant(cx, start) {
1124-
if let Some((end_idx, _)) = constant(cx, end) {
1123+
if let Some((start_idx, _)) = constant(cx, cx.tables, start) {
1124+
if let Some((end_idx, _)) = constant(cx, cx.tables, end) {
11251125
// ...and the start index is greater than the end index,
11261126
// this loop will never run. This is often confusing for developers
11271127
// who think that this will iterate from the larger value to the
@@ -2146,7 +2146,7 @@ fn path_name(e: &Expr) -> Option<Name> {
21462146
}
21472147

21482148
fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, block: &'tcx Block, expr: &'tcx Expr) {
2149-
if constant(cx, cond).is_some() {
2149+
if constant(cx, cx.tables, cond).is_some() {
21502150
// A pure constant condition (e.g. while false) is not linted.
21512151
return;
21522152
}

clippy_lints/src/matches.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,8 @@ fn all_ranges<'a, 'tcx>(
470470
[].iter()
471471
}.filter_map(|pat| {
472472
if let PatKind::Range(ref lhs, ref rhs, ref range_end) = pat.node {
473-
let lhs = constant(cx, lhs)?.0;
474-
let rhs = constant(cx, rhs)?.0;
473+
let lhs = constant(cx, cx.tables, lhs)?.0;
474+
let rhs = constant(cx, cx.tables, rhs)?.0;
475475
let rhs = match *range_end {
476476
RangeEnd::Included => Bound::Included(rhs),
477477
RangeEnd::Excluded => Bound::Excluded(rhs),
@@ -480,7 +480,7 @@ fn all_ranges<'a, 'tcx>(
480480
}
481481

482482
if let PatKind::Lit(ref value) = pat.node {
483-
let value = constant(cx, value)?.0;
483+
let value = constant(cx, cx.tables, value)?.0;
484484
return Some(SpannedRange { span: pat.span, node: (value.clone(), Bound::Included(value)) });
485485
}
486486

clippy_lints/src/methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &
17511751

17521752
/// lint for length-1 `str`s for methods in `PATTERN_METHODS`
17531753
fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
1754-
if let Some((Constant::Str(r), _)) = constant(cx, arg) {
1754+
if let Some((Constant::Str(r), _)) = constant(cx, cx.tables, arg) {
17551755
if r.len() == 1 {
17561756
let c = r.chars().next().unwrap();
17571757
let snip = snippet(cx, expr.span, "..");

clippy_lints/src/minmax.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ fn fetch_const<'a>(cx: &LateContext, args: &'a [Expr], m: MinMax) -> Option<(Min
8181
if args.len() != 2 {
8282
return None;
8383
}
84-
if let Some(c) = constant_simple(cx, &args[0]) {
85-
if constant_simple(cx, &args[1]).is_none() {
84+
if let Some(c) = constant_simple(cx, cx.tables, &args[0]) {
85+
if constant_simple(cx, cx.tables, &args[1]).is_none() {
8686
// otherwise ignore
8787
Some((m, c, &args[1]))
8888
} else {
8989
None
9090
}
91-
} else if let Some(c) = constant_simple(cx, &args[1]) {
91+
} else if let Some(c) = constant_simple(cx, cx.tables, &args[1]) {
9292
Some((m, c, &args[0]))
9393
} else {
9494
None

clippy_lints/src/misc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,15 +446,15 @@ fn check_nan(cx: &LateContext, path: &Path, expr: &Expr) {
446446
}
447447

448448
fn is_named_constant<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> bool {
449-
if let Some((_, res)) = constant(cx, expr) {
449+
if let Some((_, res)) = constant(cx, cx.tables, expr) {
450450
res
451451
} else {
452452
false
453453
}
454454
}
455455

456456
fn is_allowed<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> bool {
457-
match constant(cx, expr) {
457+
match constant(cx, cx.tables, expr) {
458458
Some((Constant::F32(f), _)) => f == 0.0 || f.is_infinite(),
459459
Some((Constant::F64(f), _)) => f == 0.0 || f.is_infinite(),
460460
_ => false,

clippy_lints/src/ranges.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
9494
// Range with step_by(0).
9595
if name == "step_by" && args.len() == 2 && has_step_by(cx, &args[0]) {
9696
use consts::{constant, Constant};
97-
if let Some((Constant::Int(0), _)) = constant(cx, &args[1]) {
97+
if let Some((Constant::Int(0), _)) = constant(cx, cx.tables, &args[1]) {
9898
span_lint(
9999
cx,
100100
ITERATOR_STEP_BY_ZERO,

clippy_lints/src/regex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
138138
}
139139

140140
fn const_str<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) -> Option<String> {
141-
constant(cx, e).and_then(|(c, _)| match c {
141+
constant(cx, cx.tables, e).and_then(|(c, _)| match c {
142142
Constant::Str(s) => Some(s),
143143
_ => None,
144144
})

clippy_lints/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ fn detect_extreme_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -
13411341

13421342
let ty = cx.tables.expr_ty(expr);
13431343

1344-
let cv = constant(cx, expr)?.0;
1344+
let cv = constant(cx, cx.tables, expr)?.0;
13451345

13461346
let which = match (&ty.sty, cv) {
13471347
(&ty::TyBool, Constant::Bool(false)) |
@@ -1526,7 +1526,7 @@ fn numeric_cast_precast_bounds<'a>(cx: &LateContext, expr: &'a Expr) -> Option<(
15261526
}
15271527

15281528
fn node_as_const_fullint<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<FullInt> {
1529-
let val = constant(cx, expr)?.0;
1529+
let val = constant(cx, cx.tables, expr)?.0;
15301530
if let Constant::Int(const_int) = val {
15311531
match cx.tables.expr_ty(expr).sty {
15321532
ty::TyInt(ity) => Some(FullInt::S(sext(cx.tcx, const_int, ity))),

0 commit comments

Comments
 (0)