Skip to content

Commit 0abcd34

Browse files
committed
Auto merge of rust-lang#12983 - frp:manual_rotate, r=llogiq
Implement a lint to replace manual bit rotations with rotate_left/rot… Fixes rust-lang#6861 r? `@llogiq` --- changelog: add [`manual_rotate`] lint
2 parents 06758d8 + b08b8b8 commit 0abcd34

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5539,6 +5539,7 @@ Released 2018-09-13
55395539
[`manual_range_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_range_patterns
55405540
[`manual_rem_euclid`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_rem_euclid
55415541
[`manual_retain`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_retain
5542+
[`manual_rotate`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_rotate
55425543
[`manual_saturating_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_arithmetic
55435544
[`manual_slice_size_calculation`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_slice_size_calculation
55445545
[`manual_split_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_split_once

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
313313
crate::manual_range_patterns::MANUAL_RANGE_PATTERNS_INFO,
314314
crate::manual_rem_euclid::MANUAL_REM_EUCLID_INFO,
315315
crate::manual_retain::MANUAL_RETAIN_INFO,
316+
crate::manual_rotate::MANUAL_ROTATE_INFO,
316317
crate::manual_slice_size_calculation::MANUAL_SLICE_SIZE_CALCULATION_INFO,
317318
crate::manual_string_new::MANUAL_STRING_NEW_INFO,
318319
crate::manual_strip::MANUAL_STRIP_INFO,

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ mod manual_non_exhaustive;
211211
mod manual_range_patterns;
212212
mod manual_rem_euclid;
213213
mod manual_retain;
214+
mod manual_rotate;
214215
mod manual_slice_size_calculation;
215216
mod manual_string_new;
216217
mod manual_strip;
@@ -1023,6 +1024,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
10231024
store.register_late_pass(|_| Box::new(default_instead_of_iter_empty::DefaultIterEmpty));
10241025
store.register_late_pass(move |_| Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv())));
10251026
store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(msrv())));
1027+
store.register_late_pass(move |_| Box::new(manual_rotate::ManualRotate));
10261028
store.register_late_pass(move |_| {
10271029
Box::new(operators::Operators::new(
10281030
verbose_bit_mask_threshold,

clippy_lints/src/manual_rotate.rs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use std::fmt::Display;
2+
3+
use clippy_utils::consts::{constant, Constant};
4+
use clippy_utils::diagnostics::span_lint_and_sugg;
5+
use clippy_utils::sugg;
6+
use rustc_errors::Applicability;
7+
use rustc_hir::{BinOpKind, Expr, ExprKind};
8+
use rustc_lint::{LateContext, LateLintPass};
9+
use rustc_middle::ty;
10+
use rustc_session::declare_lint_pass;
11+
12+
declare_clippy_lint! {
13+
/// ### What it does
14+
///
15+
/// It detects manual bit rotations that could be rewritten using standard
16+
/// functions `rotate_left` or `rotate_right`.
17+
///
18+
/// ### Why is this bad?
19+
///
20+
/// Calling the function better conveys the intent.
21+
///
22+
/// ### Known issues
23+
///
24+
/// Currently, the lint only catches shifts by constant amount.
25+
///
26+
/// ### Example
27+
/// ```no_run
28+
/// let x = 12345678_u32;
29+
/// let _ = (x >> 8) | (x << 24);
30+
/// ```
31+
/// Use instead:
32+
/// ```no_run
33+
/// let x = 12345678_u32;
34+
/// let _ = x.rotate_right(8);
35+
/// ```
36+
#[clippy::version = "1.81.0"]
37+
pub MANUAL_ROTATE,
38+
style,
39+
"using bit shifts to rotate integers"
40+
}
41+
42+
declare_lint_pass!(ManualRotate => [MANUAL_ROTATE]);
43+
44+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45+
enum ShiftDirection {
46+
Left,
47+
Right,
48+
}
49+
50+
impl Display for ShiftDirection {
51+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52+
f.write_str(match self {
53+
Self::Left => "rotate_left",
54+
Self::Right => "rotate_right",
55+
})
56+
}
57+
}
58+
59+
fn parse_shift<'tcx>(
60+
cx: &LateContext<'tcx>,
61+
expr: &'tcx Expr<'tcx>,
62+
) -> Option<(ShiftDirection, u128, &'tcx Expr<'tcx>)> {
63+
if let ExprKind::Binary(op, l, r) = expr.kind {
64+
let dir = match op.node {
65+
BinOpKind::Shl => ShiftDirection::Left,
66+
BinOpKind::Shr => ShiftDirection::Right,
67+
_ => return None,
68+
};
69+
let const_expr = constant(cx, cx.typeck_results(), r)?;
70+
if let Constant::Int(shift) = const_expr {
71+
return Some((dir, shift, l));
72+
}
73+
}
74+
None
75+
}
76+
77+
impl LateLintPass<'_> for ManualRotate {
78+
fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
79+
if let ExprKind::Binary(op, l, r) = expr.kind
80+
&& let BinOpKind::Add | BinOpKind::BitOr = op.node
81+
&& let Some((l_shift_dir, l_amount, l_expr)) = parse_shift(cx, l)
82+
&& let Some((r_shift_dir, r_amount, r_expr)) = parse_shift(cx, r)
83+
{
84+
if l_shift_dir == r_shift_dir {
85+
return;
86+
}
87+
if !clippy_utils::eq_expr_value(cx, l_expr, r_expr) {
88+
return;
89+
}
90+
let Some(bit_width) = (match cx.typeck_results().expr_ty(expr).kind() {
91+
ty::Int(itype) => itype.bit_width(),
92+
ty::Uint(itype) => itype.bit_width(),
93+
_ => return,
94+
}) else {
95+
return;
96+
};
97+
if l_amount + r_amount == u128::from(bit_width) {
98+
let (shift_function, amount) = if l_amount < r_amount {
99+
(l_shift_dir, l_amount)
100+
} else {
101+
(r_shift_dir, r_amount)
102+
};
103+
let mut applicability = Applicability::MachineApplicable;
104+
let expr_sugg = sugg::Sugg::hir_with_applicability(cx, l_expr, "_", &mut applicability).maybe_par();
105+
span_lint_and_sugg(
106+
cx,
107+
MANUAL_ROTATE,
108+
expr.span,
109+
"there is no need to manually implement bit rotation",
110+
"this expression can be rewritten as",
111+
format!("{expr_sugg}.{shift_function}({amount})"),
112+
Applicability::MachineApplicable,
113+
);
114+
}
115+
}
116+
}
117+
}

tests/ui/manual_rotate.fixed

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![warn(clippy::manual_rotate)]
2+
#![allow(unused)]
3+
fn main() {
4+
let (x_u8, x_u16, x_u32, x_u64) = (1u8, 1u16, 1u32, 1u64);
5+
let (x_i8, x_i16, x_i32, x_i64) = (1i8, 1i16, 1i32, 1i64);
6+
let a_u32 = 1u32;
7+
// True positives
8+
let y_u8 = x_u8.rotate_right(3);
9+
let y_u16 = x_u16.rotate_right(7);
10+
let y_u32 = x_u32.rotate_right(8);
11+
let y_u64 = x_u64.rotate_right(9);
12+
let y_i8 = x_i8.rotate_right(3);
13+
let y_i16 = x_i16.rotate_right(7);
14+
let y_i32 = x_i32.rotate_right(8);
15+
let y_i64 = x_i64.rotate_right(9);
16+
// Plus also works instead of |
17+
let y_u32_plus = x_u32.rotate_right(8);
18+
// Complex expression
19+
let y_u32_complex = (x_u32 | 3256).rotate_right(8);
20+
let y_u64_as = (x_u32 as u64).rotate_right(8);
21+
22+
// False positives - can't be replaced with a rotation
23+
let y_u8_false = (x_u8 >> 6) | (x_u8 << 3);
24+
let y_u32_false = (x_u32 >> 8) | (x_u32 >> 24);
25+
let y_u64_false2 = (x_u64 >> 9) & (x_u64 << 55);
26+
// Variable mismatch
27+
let y_u32_wrong_vars = (x_u32 >> 8) | (a_u32 << 24);
28+
// Has side effects and therefore should not be matched
29+
let mut l = vec![12_u8, 34];
30+
let y = (l.pop().unwrap() << 3) + (l.pop().unwrap() >> 5);
31+
}

tests/ui/manual_rotate.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![warn(clippy::manual_rotate)]
2+
#![allow(unused)]
3+
fn main() {
4+
let (x_u8, x_u16, x_u32, x_u64) = (1u8, 1u16, 1u32, 1u64);
5+
let (x_i8, x_i16, x_i32, x_i64) = (1i8, 1i16, 1i32, 1i64);
6+
let a_u32 = 1u32;
7+
// True positives
8+
let y_u8 = (x_u8 >> 3) | (x_u8 << 5);
9+
let y_u16 = (x_u16 >> 7) | (x_u16 << 9);
10+
let y_u32 = (x_u32 >> 8) | (x_u32 << 24);
11+
let y_u64 = (x_u64 >> 9) | (x_u64 << 55);
12+
let y_i8 = (x_i8 >> 3) | (x_i8 << 5);
13+
let y_i16 = (x_i16 >> 7) | (x_i16 << 9);
14+
let y_i32 = (x_i32 >> 8) | (x_i32 << 24);
15+
let y_i64 = (x_i64 >> 9) | (x_i64 << 55);
16+
// Plus also works instead of |
17+
let y_u32_plus = (x_u32 >> 8) + (x_u32 << 24);
18+
// Complex expression
19+
let y_u32_complex = ((x_u32 | 3256) >> 8) | ((x_u32 | 3256) << 24);
20+
let y_u64_as = (x_u32 as u64 >> 8) | ((x_u32 as u64) << 56);
21+
22+
// False positives - can't be replaced with a rotation
23+
let y_u8_false = (x_u8 >> 6) | (x_u8 << 3);
24+
let y_u32_false = (x_u32 >> 8) | (x_u32 >> 24);
25+
let y_u64_false2 = (x_u64 >> 9) & (x_u64 << 55);
26+
// Variable mismatch
27+
let y_u32_wrong_vars = (x_u32 >> 8) | (a_u32 << 24);
28+
// Has side effects and therefore should not be matched
29+
let mut l = vec![12_u8, 34];
30+
let y = (l.pop().unwrap() << 3) + (l.pop().unwrap() >> 5);
31+
}

tests/ui/manual_rotate.stderr

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
error: there is no need to manually implement bit rotation
2+
--> tests/ui/manual_rotate.rs:8:16
3+
|
4+
LL | let y_u8 = (x_u8 >> 3) | (x_u8 << 5);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u8.rotate_right(3)`
6+
|
7+
= note: `-D clippy::manual-rotate` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::manual_rotate)]`
9+
10+
error: there is no need to manually implement bit rotation
11+
--> tests/ui/manual_rotate.rs:9:17
12+
|
13+
LL | let y_u16 = (x_u16 >> 7) | (x_u16 << 9);
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u16.rotate_right(7)`
15+
16+
error: there is no need to manually implement bit rotation
17+
--> tests/ui/manual_rotate.rs:10:17
18+
|
19+
LL | let y_u32 = (x_u32 >> 8) | (x_u32 << 24);
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u32.rotate_right(8)`
21+
22+
error: there is no need to manually implement bit rotation
23+
--> tests/ui/manual_rotate.rs:11:17
24+
|
25+
LL | let y_u64 = (x_u64 >> 9) | (x_u64 << 55);
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u64.rotate_right(9)`
27+
28+
error: there is no need to manually implement bit rotation
29+
--> tests/ui/manual_rotate.rs:12:16
30+
|
31+
LL | let y_i8 = (x_i8 >> 3) | (x_i8 << 5);
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i8.rotate_right(3)`
33+
34+
error: there is no need to manually implement bit rotation
35+
--> tests/ui/manual_rotate.rs:13:17
36+
|
37+
LL | let y_i16 = (x_i16 >> 7) | (x_i16 << 9);
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i16.rotate_right(7)`
39+
40+
error: there is no need to manually implement bit rotation
41+
--> tests/ui/manual_rotate.rs:14:17
42+
|
43+
LL | let y_i32 = (x_i32 >> 8) | (x_i32 << 24);
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i32.rotate_right(8)`
45+
46+
error: there is no need to manually implement bit rotation
47+
--> tests/ui/manual_rotate.rs:15:17
48+
|
49+
LL | let y_i64 = (x_i64 >> 9) | (x_i64 << 55);
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_i64.rotate_right(9)`
51+
52+
error: there is no need to manually implement bit rotation
53+
--> tests/ui/manual_rotate.rs:17:22
54+
|
55+
LL | let y_u32_plus = (x_u32 >> 8) + (x_u32 << 24);
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x_u32.rotate_right(8)`
57+
58+
error: there is no need to manually implement bit rotation
59+
--> tests/ui/manual_rotate.rs:19:25
60+
|
61+
LL | let y_u32_complex = ((x_u32 | 3256) >> 8) | ((x_u32 | 3256) << 24);
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `(x_u32 | 3256).rotate_right(8)`
63+
64+
error: there is no need to manually implement bit rotation
65+
--> tests/ui/manual_rotate.rs:20:20
66+
|
67+
LL | let y_u64_as = (x_u32 as u64 >> 8) | ((x_u32 as u64) << 56);
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `(x_u32 as u64).rotate_right(8)`
69+
70+
error: aborting due to 11 previous errors
71+

0 commit comments

Comments
 (0)