forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#12451 - Jacherr:issue-12391, r=llogiq
new restriction lint: `integer_division_remainder_used` Fixes rust-lang/rust-clippy#12391 Introduces a restriction lint which disallows the use of `/` and `%` operators on any `Int` or `Uint` types (i.e., any that use the default `Div` or `Rem` trait implementations). Custom implementations of these traits are ignored. ---- changelog: Add new restriction lint [`integer_division_remainder_used`]
- Loading branch information
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use rustc_ast::BinOpKind; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::{self}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
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 is this bad? | ||
/// 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.78.0"] | ||
pub INTEGER_DIVISION_REMAINDER_USED, | ||
restriction, | ||
"use of disallowed default division and remainder operations" | ||
} | ||
|
||
declare_lint_pass!(IntegerDivisionRemainderUsed => [INTEGER_DIVISION_REMAINDER_USED]); | ||
|
||
impl LateLintPass<'_> for IntegerDivisionRemainderUsed { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let ExprKind::Binary(op, lhs, rhs) = &expr.kind | ||
&& let BinOpKind::Div | BinOpKind::Rem = op.node | ||
&& 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, | ||
expr.span.source_callsite(), | ||
&format!("use of {} has been disallowed in this context", op.node.as_str()), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#![warn(clippy::integer_division_remainder_used)] | ||
#![allow(unused_variables)] | ||
#![allow(clippy::op_ref)] | ||
|
||
struct CustomOps(pub i32); | ||
impl std::ops::Div for CustomOps { | ||
type Output = Self; | ||
|
||
fn div(self, rhs: Self) -> Self::Output { | ||
Self(self.0 / rhs.0) | ||
} | ||
} | ||
impl std::ops::Rem for CustomOps { | ||
type Output = Self; | ||
|
||
fn rem(self, rhs: Self) -> Self::Output { | ||
Self(self.0 % rhs.0) | ||
} | ||
} | ||
|
||
fn main() { | ||
// should trigger | ||
let a = 10; | ||
let b = 5; | ||
let c = a / b; | ||
let d = a % b; | ||
let e = &a / b; | ||
let f = a % &b; | ||
let g = &a / &b; | ||
let h = &10 % b; | ||
let i = a / &4; | ||
|
||
// should not trigger on custom Div and Rem | ||
let w = CustomOps(3); | ||
let x = CustomOps(4); | ||
let y = w / x; | ||
|
||
let w = CustomOps(3); | ||
let x = CustomOps(4); | ||
let z = w % x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
error: use of / has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:10:14 | ||
| | ||
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 | ||
--> tests/ui/integer_division_remainder_used.rs:17:14 | ||
| | ||
LL | Self(self.0 % rhs.0) | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: use of / has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:25:13 | ||
| | ||
LL | let c = a / b; | ||
| ^^^^^ | ||
|
||
error: use of % has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:26:13 | ||
| | ||
LL | let d = a % b; | ||
| ^^^^^ | ||
|
||
error: use of / has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:27:13 | ||
| | ||
LL | let e = &a / b; | ||
| ^^^^^^ | ||
|
||
error: use of % has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:28:13 | ||
| | ||
LL | let f = a % &b; | ||
| ^^^^^^ | ||
|
||
error: use of / has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:29:13 | ||
| | ||
LL | let g = &a / &b; | ||
| ^^^^^^^ | ||
|
||
error: use of % has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:30:13 | ||
| | ||
LL | let h = &10 % b; | ||
| ^^^^^^^ | ||
|
||
error: use of / has been disallowed in this context | ||
--> tests/ui/integer_division_remainder_used.rs:31:13 | ||
| | ||
LL | let i = a / &4; | ||
| ^^^^^^ | ||
|
||
error: aborting due to 9 previous errors | ||
|