Skip to content

Commit a17a214

Browse files
committed
wire the lint up to operators/ logic
1 parent cd1ed2a commit a17a214

File tree

4 files changed

+41
-45
lines changed

4 files changed

+41
-45
lines changed

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
227227
crate::init_numbered_fields::INIT_NUMBERED_FIELDS_INFO,
228228
crate::inline_fn_without_body::INLINE_FN_WITHOUT_BODY_INFO,
229229
crate::int_plus_one::INT_PLUS_ONE_INFO,
230-
crate::integer_division_remainder_used::INTEGER_DIVISION_REMAINDER_USED_INFO,
231230
crate::invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS_INFO,
232231
crate::item_name_repetitions::ENUM_VARIANT_NAMES_INFO,
233232
crate::item_name_repetitions::MODULE_INCEPTION_INFO,
@@ -591,6 +590,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
591590
crate::operators::IMPOSSIBLE_COMPARISONS_INFO,
592591
crate::operators::INEFFECTIVE_BIT_MASK_INFO,
593592
crate::operators::INTEGER_DIVISION_INFO,
593+
crate::operators::INTEGER_DIVISION_REMAINDER_USED_INFO,
594594
crate::operators::MANUAL_DIV_CEIL_INFO,
595595
crate::operators::MANUAL_IS_MULTIPLE_OF_INFO,
596596
crate::operators::MANUAL_MIDPOINT_INFO,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,6 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
796796
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
797797
store.register_late_pass(move |_| Box::new(assigning_clones::AssigningClones::new(conf)));
798798
store.register_late_pass(|_| Box::new(zero_repeat_side_effects::ZeroRepeatSideEffects));
799-
store.register_late_pass(|_| Box::new(integer_division_remainder_used::IntegerDivisionRemainderUsed));
800799
store.register_late_pass(move |_| Box::new(macro_metavars_in_unsafe::ExprMetavarsInUnsafe::new(conf)));
801800
store.register_late_pass(move |_| Box::new(string_patterns::StringPatterns::new(conf)));
802801
store.register_early_pass(|| Box::new(field_scoped_visibility_modifiers::FieldScopedVisibilityModifiers));
Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
11
use clippy_utils::diagnostics::span_lint;
22
use rustc_ast::BinOpKind;
3-
use rustc_hir::{Expr, ExprKind};
4-
use rustc_lint::{LateContext, LateLintPass};
3+
use rustc_hir::Expr;
4+
use rustc_lint::LateContext;
55
use rustc_middle::ty::{self};
6-
use rustc_session::declare_lint_pass;
76

8-
declare_clippy_lint! {
9-
/// ### What it does
10-
/// Checks for the usage of division (`/`) and remainder (`%`) operations
11-
/// when performed on any integer types using the default `Div` and `Rem` trait implementations.
12-
///
13-
/// ### Why restrict this?
14-
/// In cryptographic contexts, division can result in timing sidechannel vulnerabilities,
15-
/// and needs to be replaced with constant-time code instead (e.g. Barrett reduction).
16-
///
17-
/// ### Example
18-
/// ```no_run
19-
/// let my_div = 10 / 2;
20-
/// ```
21-
/// Use instead:
22-
/// ```no_run
23-
/// let my_div = 10 >> 1;
24-
/// ```
25-
#[clippy::version = "1.79.0"]
26-
pub INTEGER_DIVISION_REMAINDER_USED,
27-
restriction,
28-
"use of disallowed default division and remainder operations"
29-
}
30-
31-
declare_lint_pass!(IntegerDivisionRemainderUsed => [INTEGER_DIVISION_REMAINDER_USED]);
7+
use super::INTEGER_DIVISION_REMAINDER_USED;
328

33-
impl LateLintPass<'_> for IntegerDivisionRemainderUsed {
34-
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
35-
if let ExprKind::Binary(op, lhs, rhs) = &expr.kind
36-
&& let BinOpKind::Div | BinOpKind::Rem = op.node
37-
&& let lhs_ty = cx.typeck_results().expr_ty(lhs)
38-
&& let rhs_ty = cx.typeck_results().expr_ty(rhs)
39-
&& let ty::Int(_) | ty::Uint(_) = lhs_ty.peel_refs().kind()
40-
&& let ty::Int(_) | ty::Uint(_) = rhs_ty.peel_refs().kind()
41-
{
42-
span_lint(
43-
cx,
44-
INTEGER_DIVISION_REMAINDER_USED,
45-
expr.span.source_callsite(),
46-
format!("use of {} has been disallowed in this context", op.node.as_str()),
47-
);
48-
}
9+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &Expr<'_>) {
10+
if let BinOpKind::Div | BinOpKind::Rem = op
11+
&& let lhs_ty = cx.typeck_results().expr_ty(lhs)
12+
&& let rhs_ty = cx.typeck_results().expr_ty(rhs)
13+
&& let ty::Int(_) | ty::Uint(_) = lhs_ty.peel_refs().kind()
14+
&& let ty::Int(_) | ty::Uint(_) = rhs_ty.peel_refs().kind()
15+
{
16+
span_lint(
17+
cx,
18+
INTEGER_DIVISION_REMAINDER_USED,
19+
expr.span.source_callsite(),
20+
format!("use of {} has been disallowed in this context", op.as_str()),
21+
);
4922
}
5023
}

clippy_lints/src/operators/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,29 @@ declare_clippy_lint! {
889889
"manually reimplementing `div_ceil`"
890890
}
891891

892+
declare_clippy_lint! {
893+
/// ### What it does
894+
/// Checks for the usage of division (`/`) and remainder (`%`) operations
895+
/// when performed on any integer types using the default `Div` and `Rem` trait implementations.
896+
///
897+
/// ### Why restrict this?
898+
/// In cryptographic contexts, division can result in timing sidechannel vulnerabilities,
899+
/// and needs to be replaced with constant-time code instead (e.g. Barrett reduction).
900+
///
901+
/// ### Example
902+
/// ```no_run
903+
/// let my_div = 10 / 2;
904+
/// ```
905+
/// Use instead:
906+
/// ```no_run
907+
/// let my_div = 10 >> 1;
908+
/// ```
909+
#[clippy::version = "1.79.0"]
910+
pub INTEGER_DIVISION_REMAINDER_USED,
911+
restriction,
912+
"use of disallowed default division and remainder operations"
913+
}
914+
892915
pub struct Operators {
893916
arithmetic_context: numeric_arithmetic::Context,
894917
verbose_bit_mask_threshold: u64,
@@ -963,6 +986,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
963986
duration_subsec::check(cx, e, op.node, lhs, rhs);
964987
float_equality_without_abs::check(cx, e, op.node, lhs, rhs);
965988
integer_division::check(cx, e, op.node, lhs, rhs);
989+
integer_division_remainder_used::check(cx, e, op.node, lhs, rhs);
966990
cmp_owned::check(cx, op.node, lhs, rhs);
967991
float_cmp::check(cx, e, op.node, lhs, rhs);
968992
modulo_one::check(cx, e, op.node, rhs);

0 commit comments

Comments
 (0)