Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
crate::init_numbered_fields::INIT_NUMBERED_FIELDS_INFO,
crate::inline_fn_without_body::INLINE_FN_WITHOUT_BODY_INFO,
crate::int_plus_one::INT_PLUS_ONE_INFO,
crate::integer_division_remainder_used::INTEGER_DIVISION_REMAINDER_USED_INFO,
crate::invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS_INFO,
crate::item_name_repetitions::ENUM_VARIANT_NAMES_INFO,
crate::item_name_repetitions::MODULE_INCEPTION_INFO,
Expand Down Expand Up @@ -591,6 +590,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
crate::operators::IMPOSSIBLE_COMPARISONS_INFO,
crate::operators::INEFFECTIVE_BIT_MASK_INFO,
crate::operators::INTEGER_DIVISION_INFO,
crate::operators::INTEGER_DIVISION_REMAINDER_USED_INFO,
crate::operators::MANUAL_DIV_CEIL_INFO,
crate::operators::MANUAL_IS_MULTIPLE_OF_INFO,
crate::operators::MANUAL_MIDPOINT_INFO,
Expand Down
50 changes: 0 additions & 50 deletions clippy_lints/src/integer_division_remainder_used.rs

This file was deleted.

2 changes: 0 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ mod inherent_to_string;
mod init_numbered_fields;
mod inline_fn_without_body;
mod int_plus_one;
mod integer_division_remainder_used;
mod invalid_upcast_comparisons;
mod item_name_repetitions;
mod items_after_statements;
Expand Down Expand Up @@ -797,7 +796,6 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
store.register_late_pass(move |_| Box::new(assigning_clones::AssigningClones::new(conf)));
store.register_late_pass(|_| Box::new(zero_repeat_side_effects::ZeroRepeatSideEffects));
store.register_late_pass(|_| Box::new(integer_division_remainder_used::IntegerDivisionRemainderUsed));
store.register_late_pass(move |_| Box::new(macro_metavars_in_unsafe::ExprMetavarsInUnsafe::new(conf)));
store.register_late_pass(move |_| Box::new(string_patterns::StringPatterns::new(conf)));
store.register_early_pass(|| Box::new(field_scoped_visibility_modifiers::FieldScopedVisibilityModifiers));
Expand Down
24 changes: 24 additions & 0 deletions clippy_lints/src/operators/integer_division_remainder_used.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clippy_utils::diagnostics::span_lint;
use rustc_ast::BinOpKind;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::Span;

use super::INTEGER_DIVISION_REMAINDER_USED;

pub(super) fn check(cx: &LateContext<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &Expr<'_>, span: Span) {
if let BinOpKind::Div | BinOpKind::Rem = op
&& let lhs_ty = cx.typeck_results().expr_ty(lhs)
&& let rhs_ty = cx.typeck_results().expr_ty(rhs)
&& let ty::Int(_) | ty::Uint(_) = lhs_ty.peel_refs().kind()
&& let ty::Int(_) | ty::Uint(_) = rhs_ty.peel_refs().kind()
{
span_lint(
cx,
INTEGER_DIVISION_REMAINDER_USED,
span.source_callsite(),
format!("use of `{}` has been disallowed in this context", op.as_str()),
);
}
}
26 changes: 26 additions & 0 deletions clippy_lints/src/operators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod float_cmp;
mod float_equality_without_abs;
mod identity_op;
mod integer_division;
mod integer_division_remainder_used;
mod manual_div_ceil;
mod manual_is_multiple_of;
mod manual_midpoint;
Expand Down Expand Up @@ -888,6 +889,29 @@ declare_clippy_lint! {
"manually reimplementing `div_ceil`"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for the usage of division (`/`) and remainder (`%`) operations
/// when performed on any integer types using the default `Div` and `Rem` trait implementations.
///
/// ### Why restrict this?
/// In cryptographic contexts, division can result in timing sidechannel vulnerabilities,
/// and needs to be replaced with constant-time code instead (e.g. Barrett reduction).
///
/// ### Example
/// ```no_run
/// let my_div = 10 / 2;
/// ```
/// Use instead:
/// ```no_run
/// let my_div = 10 >> 1;
/// ```
#[clippy::version = "1.79.0"]
pub INTEGER_DIVISION_REMAINDER_USED,
restriction,
"use of disallowed default division and remainder operations"
}

pub struct Operators {
arithmetic_context: numeric_arithmetic::Context,
verbose_bit_mask_threshold: u64,
Expand Down Expand Up @@ -925,6 +949,7 @@ impl_lint_pass!(Operators => [
FLOAT_EQUALITY_WITHOUT_ABS,
IDENTITY_OP,
INTEGER_DIVISION,
INTEGER_DIVISION_REMAINDER_USED,
CMP_OWNED,
FLOAT_CMP,
FLOAT_CMP_CONST,
Expand Down Expand Up @@ -962,6 +987,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
duration_subsec::check(cx, e, op.node, lhs, rhs);
float_equality_without_abs::check(cx, e, op.node, lhs, rhs);
integer_division::check(cx, e, op.node, lhs, rhs);
integer_division_remainder_used::check(cx, op.node, lhs, rhs, e.span);
cmp_owned::check(cx, op.node, lhs, rhs);
float_cmp::check(cx, e, op.node, lhs, rhs);
modulo_one::check(cx, e, op.node, rhs);
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/integer_division_remainder_used.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: use of / has been disallowed in this context
error: use of `/` has been disallowed in this context
--> tests/ui/integer_division_remainder_used.rs:10:14
|
LL | Self(self.0 / rhs.0)
Expand All @@ -7,49 +7,49 @@ LL | Self(self.0 / rhs.0)
= note: `-D clippy::integer-division-remainder-used` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::integer_division_remainder_used)]`

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

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

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

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

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

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

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

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