Skip to content

Commit 98e87cf

Browse files
committed
Add integer_const() and is_zero_integer_const() utility functions
1 parent 162b0e8 commit 98e87cf

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

clippy_lints/src/if_not_else.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::consts::{ConstEvalCtxt, Constant};
1+
use clippy_utils::consts::is_zero_integer_const;
22
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
33
use clippy_utils::is_else_clause;
44
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet};
@@ -48,13 +48,6 @@ declare_clippy_lint! {
4848

4949
declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
5050

51-
fn is_zero_const(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
52-
if let Some(value) = ConstEvalCtxt::new(cx).eval_simple(expr) {
53-
return Constant::Int(0) == value;
54-
}
55-
false
56-
}
57-
5851
impl LateLintPass<'_> for IfNotElse {
5952
fn check_expr(&mut self, cx: &LateContext<'_>, e: &Expr<'_>) {
6053
if let ExprKind::If(cond, cond_inner, Some(els)) = e.kind
@@ -68,7 +61,7 @@ impl LateLintPass<'_> for IfNotElse {
6861
),
6962
// Don't lint on `… != 0`, as these are likely to be bit tests.
7063
// For example, `if foo & 0x0F00 != 0 { … } else { … }` is already in the "proper" order.
71-
ExprKind::Binary(op, _, rhs) if op.node == BinOpKind::Ne && !is_zero_const(rhs, cx) => (
64+
ExprKind::Binary(op, _, rhs) if op.node == BinOpKind::Ne && !is_zero_integer_const(cx, rhs) => (
7265
"unnecessary `!=` operation",
7366
"change to `==` and swap the blocks of the `if`/`else`",
7467
),

clippy_lints/src/operators/identity_op.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::consts::{ConstEvalCtxt, Constant, FullInt};
1+
use clippy_utils::consts::{ConstEvalCtxt, Constant, FullInt, integer_const, is_zero_integer_const};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::{clip, peel_hir_expr_refs, unsext};
@@ -170,9 +170,7 @@ fn is_allowed(cx: &LateContext<'_>, cmp: BinOpKind, left: &Expr<'_>, right: &Exp
170170
cx.typeck_results().expr_ty(left).peel_refs().is_integral()
171171
&& cx.typeck_results().expr_ty(right).peel_refs().is_integral()
172172
// `1 << 0` is a common pattern in bit manipulation code
173-
&& !(cmp == BinOpKind::Shl
174-
&& ConstEvalCtxt::new(cx).eval_simple(right) == Some(Constant::Int(0))
175-
&& ConstEvalCtxt::new(cx).eval_simple(left) == Some(Constant::Int(1)))
173+
&& !(cmp == BinOpKind::Shl && is_zero_integer_const(cx, right) && integer_const(cx, left) == Some(1))
176174
}
177175

178176
fn check_remainder(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span: Span, arg: Span) {

clippy_utils/src/consts.rs

+14
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,17 @@ fn field_of_struct<'tcx>(
961961
None
962962
}
963963
}
964+
965+
/// If `expr` evaluates to an integer constant, return its value.
966+
pub fn integer_const(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<u128> {
967+
if let Some(Constant::Int(value)) = ConstEvalCtxt::new(cx).eval_simple(expr) {
968+
Some(value)
969+
} else {
970+
None
971+
}
972+
}
973+
974+
/// Check if `expr` evaluates to an integer constant of 0.
975+
pub fn is_zero_integer_const(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
976+
integer_const(cx, expr) == Some(0)
977+
}

0 commit comments

Comments
 (0)