Skip to content

Commit 023cb52

Browse files
committed
fix: print actual consts when shifting by them
this is kind of a side-effect of the next commit
1 parent 750e99b commit 023cb52

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

clippy_lints/src/manual_rotate.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,18 @@ impl LateLintPass<'_> for ManualRotate {
8484
{
8585
let const_eval = ConstEvalCtxt::new(cx);
8686

87-
if let Some(Constant::Int(l_amount)) = const_eval.eval(l_amount)
88-
&& let Some(Constant::Int(r_amount)) = const_eval.eval(r_amount)
89-
&& l_amount + r_amount == u128::from(bit_width)
87+
if let Some(Constant::Int(l_amount_val)) = const_eval.eval(l_amount)
88+
&& let Some(Constant::Int(r_amount_val)) = const_eval.eval(r_amount)
89+
&& l_amount_val + r_amount_val == u128::from(bit_width)
9090
{
91-
let (shift_function, amount) = if l_amount < r_amount {
91+
let (shift_function, amount) = if l_amount_val < r_amount_val {
9292
(l_shift_dir, l_amount)
9393
} else {
9494
(r_shift_dir, r_amount)
9595
};
9696
let mut applicability = Applicability::MachineApplicable;
9797
let expr_sugg = sugg::Sugg::hir_with_applicability(cx, l_expr, "_", &mut applicability).maybe_paren();
98+
let amount = sugg::Sugg::hir_with_applicability(cx, amount, "_", &mut applicability);
9899
span_lint_and_sugg(
99100
cx,
100101
MANUAL_ROTATE,

tests/ui/manual_rotate.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ fn main() {
44
let (x_u8, x_u16, x_u32, x_u64) = (1u8, 1u16, 1u32, 1u64);
55
let (x_i8, x_i16, x_i32, x_i64) = (1i8, 1i16, 1i32, 1i64);
66
let a_u32 = 1u32;
7+
const N: u32 = 5;
78
// True positives
89
let y_u8 = x_u8.rotate_right(3);
910
//~^ manual_rotate
@@ -29,6 +30,9 @@ fn main() {
2930
//~^ manual_rotate
3031
let y_u64_as = (x_u32 as u64).rotate_right(8);
3132
//~^ manual_rotate
33+
// shift by a const
34+
let _ = x_i64.rotate_right(N);
35+
//~^ manual_rotate
3236

3337
// False positives - can't be replaced with a rotation
3438
let y_u8_false = (x_u8 >> 6) | (x_u8 << 3);

tests/ui/manual_rotate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ fn main() {
44
let (x_u8, x_u16, x_u32, x_u64) = (1u8, 1u16, 1u32, 1u64);
55
let (x_i8, x_i16, x_i32, x_i64) = (1i8, 1i16, 1i32, 1i64);
66
let a_u32 = 1u32;
7+
const N: u32 = 5;
78
// True positives
89
let y_u8 = (x_u8 >> 3) | (x_u8 << 5);
910
//~^ manual_rotate
@@ -29,6 +30,9 @@ fn main() {
2930
//~^ manual_rotate
3031
let y_u64_as = (x_u32 as u64 >> 8) | ((x_u32 as u64) << 56);
3132
//~^ manual_rotate
33+
// shift by a const
34+
let _ = (x_i64 >> N) | (x_i64 << (64 - N));
35+
//~^ manual_rotate
3236

3337
// False positives - can't be replaced with a rotation
3438
let y_u8_false = (x_u8 >> 6) | (x_u8 << 3);

tests/ui/manual_rotate.stderr

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: there is no need to manually implement bit rotation
2-
--> tests/ui/manual_rotate.rs:8:16
2+
--> tests/ui/manual_rotate.rs:9:16
33
|
44
LL | let y_u8 = (x_u8 >> 3) | (x_u8 << 5);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u8.rotate_right(3)`
@@ -8,64 +8,70 @@ LL | let y_u8 = (x_u8 >> 3) | (x_u8 << 5);
88
= help: to override `-D warnings` add `#[allow(clippy::manual_rotate)]`
99

1010
error: there is no need to manually implement bit rotation
11-
--> tests/ui/manual_rotate.rs:10:17
11+
--> tests/ui/manual_rotate.rs:11:17
1212
|
1313
LL | let y_u16 = (x_u16 >> 7) | (x_u16 << 9);
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u16.rotate_right(7)`
1515

1616
error: there is no need to manually implement bit rotation
17-
--> tests/ui/manual_rotate.rs:12:17
17+
--> tests/ui/manual_rotate.rs:13:17
1818
|
1919
LL | let y_u32 = (x_u32 >> 8) | (x_u32 << 24);
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u32.rotate_right(8)`
2121

2222
error: there is no need to manually implement bit rotation
23-
--> tests/ui/manual_rotate.rs:14:17
23+
--> tests/ui/manual_rotate.rs:15:17
2424
|
2525
LL | let y_u64 = (x_u64 >> 9) | (x_u64 << 55);
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u64.rotate_right(9)`
2727

2828
error: there is no need to manually implement bit rotation
29-
--> tests/ui/manual_rotate.rs:16:16
29+
--> tests/ui/manual_rotate.rs:17:16
3030
|
3131
LL | let y_i8 = (x_i8 >> 3) | (x_i8 << 5);
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i8.rotate_right(3)`
3333

3434
error: there is no need to manually implement bit rotation
35-
--> tests/ui/manual_rotate.rs:18:17
35+
--> tests/ui/manual_rotate.rs:19:17
3636
|
3737
LL | let y_i16 = (x_i16 >> 7) | (x_i16 << 9);
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i16.rotate_right(7)`
3939

4040
error: there is no need to manually implement bit rotation
41-
--> tests/ui/manual_rotate.rs:20:17
41+
--> tests/ui/manual_rotate.rs:21:17
4242
|
4343
LL | let y_i32 = (x_i32 >> 8) | (x_i32 << 24);
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i32.rotate_right(8)`
4545

4646
error: there is no need to manually implement bit rotation
47-
--> tests/ui/manual_rotate.rs:22:17
47+
--> tests/ui/manual_rotate.rs:23:17
4848
|
4949
LL | let y_i64 = (x_i64 >> 9) | (x_i64 << 55);
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i64.rotate_right(9)`
5151

5252
error: there is no need to manually implement bit rotation
53-
--> tests/ui/manual_rotate.rs:25:22
53+
--> tests/ui/manual_rotate.rs:26:22
5454
|
5555
LL | let y_u32_plus = (x_u32 >> 8) + (x_u32 << 24);
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u32.rotate_right(8)`
5757

5858
error: there is no need to manually implement bit rotation
59-
--> tests/ui/manual_rotate.rs:28:25
59+
--> tests/ui/manual_rotate.rs:29:25
6060
|
6161
LL | let y_u32_complex = ((x_u32 | 3256) >> 8) | ((x_u32 | 3256) << 24);
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `(x_u32 | 3256).rotate_right(8)`
6363

6464
error: there is no need to manually implement bit rotation
65-
--> tests/ui/manual_rotate.rs:30:20
65+
--> tests/ui/manual_rotate.rs:31:20
6666
|
6767
LL | let y_u64_as = (x_u32 as u64 >> 8) | ((x_u32 as u64) << 56);
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `(x_u32 as u64).rotate_right(8)`
6969

70-
error: aborting due to 11 previous errors
70+
error: there is no need to manually implement bit rotation
71+
--> tests/ui/manual_rotate.rs:34:13
72+
|
73+
LL | let _ = (x_i64 >> N) | (x_i64 << (64 - N));
74+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i64.rotate_right(N)`
75+
76+
error: aborting due to 12 previous errors
7177

0 commit comments

Comments
 (0)