-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmanual_is_power_of_two.rs
139 lines (128 loc) · 4.51 KB
/
manual_is_power_of_two.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::sugg::Sugg;
use clippy_utils::{SpanlessEq, is_in_const_context, is_integer_literal};
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::impl_lint_pass;
declare_clippy_lint! {
/// ### What it does
/// Checks for expressions like `x.count_ones() == 1` or `x & (x - 1) == 0`, with x and unsigned integer, which may be manual
/// reimplementations of `x.is_power_of_two()`.
///
/// ### Why is this bad?
/// Manual reimplementations of `is_power_of_two` increase code complexity for little benefit.
///
/// ### Example
/// ```no_run
/// let a: u32 = 4;
/// let result = a.count_ones() == 1;
/// ```
/// Use instead:
/// ```no_run
/// let a: u32 = 4;
/// let result = a.is_power_of_two();
/// ```
#[clippy::version = "1.83.0"]
pub MANUAL_IS_POWER_OF_TWO,
pedantic,
"manually reimplementing `is_power_of_two`"
}
pub struct ManualIsPowerOfTwo {
msrv: Msrv,
}
impl_lint_pass!(ManualIsPowerOfTwo => [MANUAL_IS_POWER_OF_TWO]);
impl ManualIsPowerOfTwo {
pub fn new(conf: &'static Conf) -> Self {
Self { msrv: conf.msrv }
}
fn build_sugg(&self, cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
if is_in_const_context(cx) && !self.msrv.meets(cx, msrvs::CONST_IS_POWER_OF_TWO) {
return;
}
let mut applicability = Applicability::MachineApplicable;
let snippet = Sugg::hir_with_applicability(cx, receiver, "_", &mut applicability);
span_lint_and_sugg(
cx,
MANUAL_IS_POWER_OF_TWO,
expr.span,
"manually reimplementing `is_power_of_two`",
"consider using `.is_power_of_two()`",
format!("{}.is_power_of_two()", snippet.maybe_paren()),
applicability,
);
}
}
impl<'tcx> LateLintPass<'tcx> for ManualIsPowerOfTwo {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if !expr.span.from_expansion()
&& let ExprKind::Binary(bin_op, lhs, rhs) = expr.kind
&& !lhs.span.from_expansion()
&& !rhs.span.from_expansion()
&& bin_op.node == BinOpKind::Eq
{
if let Some(a) = count_ones_receiver(cx, lhs)
&& is_integer_literal(rhs, 1)
{
self.build_sugg(cx, expr, a);
} else if let Some(a) = count_ones_receiver(cx, rhs)
&& is_integer_literal(lhs, 1)
{
self.build_sugg(cx, expr, a);
} else if is_integer_literal(rhs, 0)
&& let Some(a) = is_and_minus_one(cx, lhs)
{
self.build_sugg(cx, expr, a);
} else if is_integer_literal(lhs, 0)
&& let Some(a) = is_and_minus_one(cx, rhs)
{
self.build_sugg(cx, expr, a);
}
}
}
}
/// Return the unsigned integer receiver of `.count_ones()`
fn count_ones_receiver<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
if let ExprKind::MethodCall(method_name, receiver, [], _) = expr.kind
&& method_name.ident.as_str() == "count_ones"
&& matches!(cx.typeck_results().expr_ty_adjusted(receiver).kind(), ty::Uint(_))
{
Some(receiver)
} else {
None
}
}
/// Return `greater` if `smaller == greater - 1`
fn is_one_less<'tcx>(
cx: &LateContext<'tcx>,
greater: &'tcx Expr<'tcx>,
smaller: &Expr<'tcx>,
) -> Option<&'tcx Expr<'tcx>> {
if let ExprKind::Binary(op, lhs, rhs) = smaller.kind
&& !lhs.span.from_expansion()
&& !rhs.span.from_expansion()
&& op.node == BinOpKind::Sub
&& SpanlessEq::new(cx).eq_expr(greater, lhs)
&& is_integer_literal(rhs, 1)
&& matches!(cx.typeck_results().expr_ty_adjusted(greater).kind(), ty::Uint(_))
{
Some(greater)
} else {
None
}
}
/// Return `v` if `expr` is `v & (v - 1)` or `(v - 1) & v`
fn is_and_minus_one<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
if let ExprKind::Binary(op, lhs, rhs) = expr.kind
&& !lhs.span.from_expansion()
&& !rhs.span.from_expansion()
&& op.node == BinOpKind::BitAnd
{
is_one_less(cx, lhs, rhs).or_else(|| is_one_less(cx, rhs, lhs))
} else {
None
}
}