Skip to content

Commit d05f74e

Browse files
refactor(integer_division_remainder_used): move to under operators (#15953)
changelog: none
2 parents 5a90737 + 967d2b1 commit d05f74e

File tree

6 files changed

+60
-62
lines changed

6 files changed

+60
-62
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::item_name_repetitions::ENUM_VARIANT_NAMES_INFO,
232231
crate::item_name_repetitions::MODULE_INCEPTION_INFO,
233232
crate::item_name_repetitions::MODULE_NAME_REPETITIONS_INFO,
@@ -590,6 +589,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
590589
crate::operators::IMPOSSIBLE_COMPARISONS_INFO,
591590
crate::operators::INEFFECTIVE_BIT_MASK_INFO,
592591
crate::operators::INTEGER_DIVISION_INFO,
592+
crate::operators::INTEGER_DIVISION_REMAINDER_USED_INFO,
593593
crate::operators::INVALID_UPCAST_COMPARISONS_INFO,
594594
crate::operators::MANUAL_DIV_CEIL_INFO,
595595
crate::operators::MANUAL_IS_MULTIPLE_OF_INFO,

clippy_lints/src/integer_division_remainder_used.rs

Lines changed: 0 additions & 50 deletions
This file was deleted.

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ mod inherent_to_string;
171171
mod init_numbered_fields;
172172
mod inline_fn_without_body;
173173
mod int_plus_one;
174-
mod integer_division_remainder_used;
175174
mod item_name_repetitions;
176175
mod items_after_statements;
177176
mod items_after_test_module;
@@ -793,7 +792,6 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
793792
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
794793
store.register_late_pass(move |_| Box::new(assigning_clones::AssigningClones::new(conf)));
795794
store.register_late_pass(|_| Box::new(zero_repeat_side_effects::ZeroRepeatSideEffects));
796-
store.register_late_pass(|_| Box::new(integer_division_remainder_used::IntegerDivisionRemainderUsed));
797795
store.register_late_pass(move |_| Box::new(macro_metavars_in_unsafe::ExprMetavarsInUnsafe::new(conf)));
798796
store.register_late_pass(move |_| Box::new(string_patterns::StringPatterns::new(conf)));
799797
store.register_early_pass(|| Box::new(field_scoped_visibility_modifiers::FieldScopedVisibilityModifiers));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use rustc_ast::BinOpKind;
3+
use rustc_hir::Expr;
4+
use rustc_lint::LateContext;
5+
use rustc_middle::ty;
6+
use rustc_span::Span;
7+
8+
use super::INTEGER_DIVISION_REMAINDER_USED;
9+
10+
pub(super) fn check(cx: &LateContext<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &Expr<'_>, span: Span) {
11+
if let BinOpKind::Div | BinOpKind::Rem = op
12+
&& let lhs_ty = cx.typeck_results().expr_ty(lhs)
13+
&& let rhs_ty = cx.typeck_results().expr_ty(rhs)
14+
&& let ty::Int(_) | ty::Uint(_) = lhs_ty.peel_refs().kind()
15+
&& let ty::Int(_) | ty::Uint(_) = rhs_ty.peel_refs().kind()
16+
{
17+
span_lint(
18+
cx,
19+
INTEGER_DIVISION_REMAINDER_USED,
20+
span.source_callsite(),
21+
format!("use of `{}` has been disallowed in this context", op.as_str()),
22+
);
23+
}
24+
}

clippy_lints/src/operators/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod float_cmp;
1111
mod float_equality_without_abs;
1212
mod identity_op;
1313
mod integer_division;
14+
mod integer_division_remainder_used;
1415
mod invalid_upcast_comparisons;
1516
mod manual_div_ceil;
1617
mod manual_is_multiple_of;
@@ -911,6 +912,29 @@ declare_clippy_lint! {
911912
"a comparison involving an upcast which is always true or false"
912913
}
913914

915+
declare_clippy_lint! {
916+
/// ### What it does
917+
/// Checks for the usage of division (`/`) and remainder (`%`) operations
918+
/// when performed on any integer types using the default `Div` and `Rem` trait implementations.
919+
///
920+
/// ### Why restrict this?
921+
/// In cryptographic contexts, division can result in timing sidechannel vulnerabilities,
922+
/// and needs to be replaced with constant-time code instead (e.g. Barrett reduction).
923+
///
924+
/// ### Example
925+
/// ```no_run
926+
/// let my_div = 10 / 2;
927+
/// ```
928+
/// Use instead:
929+
/// ```no_run
930+
/// let my_div = 10 >> 1;
931+
/// ```
932+
#[clippy::version = "1.79.0"]
933+
pub INTEGER_DIVISION_REMAINDER_USED,
934+
restriction,
935+
"use of disallowed default division and remainder operations"
936+
}
937+
914938
pub struct Operators {
915939
arithmetic_context: numeric_arithmetic::Context,
916940
verbose_bit_mask_threshold: u64,
@@ -948,6 +972,7 @@ impl_lint_pass!(Operators => [
948972
FLOAT_EQUALITY_WITHOUT_ABS,
949973
IDENTITY_OP,
950974
INTEGER_DIVISION,
975+
INTEGER_DIVISION_REMAINDER_USED,
951976
CMP_OWNED,
952977
FLOAT_CMP,
953978
FLOAT_CMP_CONST,
@@ -987,6 +1012,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
9871012
duration_subsec::check(cx, e, op.node, lhs, rhs);
9881013
float_equality_without_abs::check(cx, e, op.node, lhs, rhs);
9891014
integer_division::check(cx, e, op.node, lhs, rhs);
1015+
integer_division_remainder_used::check(cx, op.node, lhs, rhs, e.span);
9901016
cmp_owned::check(cx, op.node, lhs, rhs);
9911017
float_cmp::check(cx, e, op.node, lhs, rhs);
9921018
modulo_one::check(cx, e, op.node, rhs);

tests/ui/integer_division_remainder_used.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: use of / has been disallowed in this context
1+
error: use of `/` has been disallowed in this context
22
--> tests/ui/integer_division_remainder_used.rs:10:14
33
|
44
LL | Self(self.0 / rhs.0)
@@ -7,49 +7,49 @@ LL | Self(self.0 / rhs.0)
77
= note: `-D clippy::integer-division-remainder-used` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::integer_division_remainder_used)]`
99

10-
error: use of % has been disallowed in this context
10+
error: use of `%` has been disallowed in this context
1111
--> tests/ui/integer_division_remainder_used.rs:18:14
1212
|
1313
LL | Self(self.0 % rhs.0)
1414
| ^^^^^^^^^^^^^^
1515

16-
error: use of / has been disallowed in this context
16+
error: use of `/` has been disallowed in this context
1717
--> tests/ui/integer_division_remainder_used.rs:27:13
1818
|
1919
LL | let c = a / b;
2020
| ^^^^^
2121

22-
error: use of % has been disallowed in this context
22+
error: use of `%` has been disallowed in this context
2323
--> tests/ui/integer_division_remainder_used.rs:29:13
2424
|
2525
LL | let d = a % b;
2626
| ^^^^^
2727

28-
error: use of / has been disallowed in this context
28+
error: use of `/` has been disallowed in this context
2929
--> tests/ui/integer_division_remainder_used.rs:31:13
3030
|
3131
LL | let e = &a / b;
3232
| ^^^^^^
3333

34-
error: use of % has been disallowed in this context
34+
error: use of `%` has been disallowed in this context
3535
--> tests/ui/integer_division_remainder_used.rs:33:13
3636
|
3737
LL | let f = a % &b;
3838
| ^^^^^^
3939

40-
error: use of / has been disallowed in this context
40+
error: use of `/` has been disallowed in this context
4141
--> tests/ui/integer_division_remainder_used.rs:35:13
4242
|
4343
LL | let g = &a / &b;
4444
| ^^^^^^^
4545

46-
error: use of % has been disallowed in this context
46+
error: use of `%` has been disallowed in this context
4747
--> tests/ui/integer_division_remainder_used.rs:37:13
4848
|
4949
LL | let h = &10 % b;
5050
| ^^^^^^^
5151

52-
error: use of / has been disallowed in this context
52+
error: use of `/` has been disallowed in this context
5353
--> tests/ui/integer_division_remainder_used.rs:39:13
5454
|
5555
LL | let i = a / &4;

0 commit comments

Comments
 (0)