Skip to content

Commit a7c8ed3

Browse files
committed
add test for shift with negative RHS
1 parent 47689ff commit a7c8ed3

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

clippy_utils/src/consts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
496496
// sign extend
497497
let value = sext(self.lcx.tcx, value, ity);
498498

499-
// Applying unary - to the most negative value of any signed integer type panicks.
499+
// Applying unary - to the most negative value of any signed integer type panics.
500500
if value == min {
501501
return None;
502502
}
@@ -638,22 +638,22 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
638638
let r = sext(self.lcx.tcx, r, ity);
639639

640640
// Using / or %, where the left-hand argument is the smallest integer of a signed integer type and
641-
// the right-hand argument is -1 always panicks, even with overflow-checks disabled
641+
// the right-hand argument is -1 always panics, even with overflow-checks disabled
642642
if l == ty_min_value && r == -1 {
643643
return None;
644644
}
645645

646646
let zext = |n: i128| Constant::Int(unsext(self.lcx.tcx, n, ity));
647647
match op.node {
648648
// When +, * or binary - create a value greater than the maximum value, or less than
649-
// the minimum value that can be stored, it panicks.
649+
// the minimum value that can be stored, it panics.
650650
BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(zext),
651651
BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(zext),
652652
BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(zext),
653653
BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
654654
BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
655655
// Using << or >> where the right-hand argument is greater than or equal to the number of bits
656-
// in the type of the left-hand argument, or is negative panicks.
656+
// in the type of the left-hand argument, or is negative panics.
657657
BinOpKind::Shr if r < bits && !r.is_negative() => l.checked_shr(r.try_into().ok()?).map(zext),
658658
BinOpKind::Shl if r < bits && !r.is_negative() => l.checked_shl(r.try_into().ok()?).map(zext),
659659
BinOpKind::BitXor => Some(zext(l ^ r)),

tests/ui/unnecessary_lazy_eval.fixed

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,21 @@ fn issue9422(x: usize) -> Option<usize> {
202202
fn panicky_arithmetic_ops(x: usize) {
203203
let _x = false.then(|| i32::MAX + 1); // don't lint
204204
let _x = false.then(|| i32::MAX * 2); // don't lint
205-
let _x = false.then_some(i32::MAX - 1); // lint
205+
let _x = false.then_some(i32::MAX - 1);
206+
//~^ ERROR: unnecessary closure used with `bool::then`
207+
let _x = false.then(|| i32::MIN - 1); // don't lint
206208
#[allow(clippy::identity_op)]
207-
let _x = false.then_some((1 + 2 * 3 - 2 / 3 + 9) << 2); // lint
208-
let _x = false.then_some(255u8 << 7); // lint
209+
let _x = false.then_some((1 + 2 * 3 - 2 / 3 + 9) << 2);
210+
//~^ ERROR: unnecessary closure used with `bool::then`
211+
let _x = false.then_some(255u8 << 7);
212+
//~^ ERROR: unnecessary closure used with `bool::then`
209213
let _x = false.then(|| 255u8 << 8); // don't lint
210214
let _x = false.then(|| 255u8 >> 8); // don't lint
211215
let _x = false.then(|| 255u8 >> x); // don't lint
212216
let _x = false.then(|| i32::MIN / -1); // don't lint
213-
let _x = false.then_some(-i32::MAX); // lint
217+
let _x = false.then_some(-i32::MAX);
218+
//~^ ERROR: unnecessary closure used with `bool::then`
214219
let _x = false.then(|| -i32::MIN); // don't lint
220+
let _x = false.then(|| 255 >> -7); // don't lint
221+
let _x = false.then(|| 255 << -1); // don't lint
215222
}

tests/ui/unnecessary_lazy_eval.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,21 @@ fn issue9422(x: usize) -> Option<usize> {
202202
fn panicky_arithmetic_ops(x: usize) {
203203
let _x = false.then(|| i32::MAX + 1); // don't lint
204204
let _x = false.then(|| i32::MAX * 2); // don't lint
205-
let _x = false.then(|| i32::MAX - 1); // lint
205+
let _x = false.then(|| i32::MAX - 1);
206+
//~^ ERROR: unnecessary closure used with `bool::then`
207+
let _x = false.then(|| i32::MIN - 1); // don't lint
206208
#[allow(clippy::identity_op)]
207-
let _x = false.then(|| (1 + 2 * 3 - 2 / 3 + 9) << 2); // lint
208-
let _x = false.then(|| 255u8 << 7); // lint
209+
let _x = false.then(|| (1 + 2 * 3 - 2 / 3 + 9) << 2);
210+
//~^ ERROR: unnecessary closure used with `bool::then`
211+
let _x = false.then(|| 255u8 << 7);
212+
//~^ ERROR: unnecessary closure used with `bool::then`
209213
let _x = false.then(|| 255u8 << 8); // don't lint
210214
let _x = false.then(|| 255u8 >> 8); // don't lint
211215
let _x = false.then(|| 255u8 >> x); // don't lint
212216
let _x = false.then(|| i32::MIN / -1); // don't lint
213-
let _x = false.then(|| -i32::MAX); // lint
217+
let _x = false.then(|| -i32::MAX);
218+
//~^ ERROR: unnecessary closure used with `bool::then`
214219
let _x = false.then(|| -i32::MIN); // don't lint
220+
let _x = false.then(|| 255 >> -7); // don't lint
221+
let _x = false.then(|| 255 << -1); // don't lint
215222
}

tests/ui/unnecessary_lazy_eval.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,31 +315,31 @@ LL | | or_else(|_| Ok(ext_str.some_field));
315315
error: unnecessary closure used with `bool::then`
316316
--> $DIR/unnecessary_lazy_eval.rs:205:14
317317
|
318-
LL | let _x = false.then(|| i32::MAX - 1); // lint
318+
LL | let _x = false.then(|| i32::MAX - 1);
319319
| ^^^^^^---------------------
320320
| |
321321
| help: use `then_some(..)` instead: `then_some(i32::MAX - 1)`
322322

323323
error: unnecessary closure used with `bool::then`
324-
--> $DIR/unnecessary_lazy_eval.rs:207:14
324+
--> $DIR/unnecessary_lazy_eval.rs:209:14
325325
|
326-
LL | let _x = false.then(|| (1 + 2 * 3 - 2 / 3 + 9) << 2); // lint
326+
LL | let _x = false.then(|| (1 + 2 * 3 - 2 / 3 + 9) << 2);
327327
| ^^^^^^-------------------------------------
328328
| |
329329
| help: use `then_some(..)` instead: `then_some((1 + 2 * 3 - 2 / 3 + 9) << 2)`
330330

331331
error: unnecessary closure used with `bool::then`
332-
--> $DIR/unnecessary_lazy_eval.rs:208:14
332+
--> $DIR/unnecessary_lazy_eval.rs:211:14
333333
|
334-
LL | let _x = false.then(|| 255u8 << 7); // lint
334+
LL | let _x = false.then(|| 255u8 << 7);
335335
| ^^^^^^-------------------
336336
| |
337337
| help: use `then_some(..)` instead: `then_some(255u8 << 7)`
338338

339339
error: unnecessary closure used with `bool::then`
340-
--> $DIR/unnecessary_lazy_eval.rs:213:14
340+
--> $DIR/unnecessary_lazy_eval.rs:217:14
341341
|
342-
LL | let _x = false.then(|| -i32::MAX); // lint
342+
LL | let _x = false.then(|| -i32::MAX);
343343
| ^^^^^^------------------
344344
| |
345345
| help: use `then_some(..)` instead: `then_some(-i32::MAX)`

0 commit comments

Comments
 (0)