Skip to content

Commit 31e5465

Browse files
committed
Add Arithmetic lint
1 parent cce6171 commit 31e5465

File tree

12 files changed

+247
-0
lines changed

12 files changed

+247
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,7 @@ Released 2018-09-13
34373437
[`almost_complete_letter_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_letter_range
34383438
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
34393439
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
3440+
[`arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic
34403441
[`as_conversions`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_conversions
34413442
[`as_underscore`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_underscore
34423443
[`assertions_on_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants

clippy_lints/src/lib.register_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ store.register_lints(&[
418418
only_used_in_recursion::ONLY_USED_IN_RECURSION,
419419
open_options::NONSENSICAL_OPEN_OPTIONS,
420420
operators::ABSURD_EXTREME_COMPARISONS,
421+
operators::ARITHMETIC,
421422
operators::ASSIGN_OP_PATTERN,
422423
operators::BAD_BIT_MASK,
423424
operators::CMP_NAN,

clippy_lints/src/lib.register_restriction.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
4848
LintId::of(mixed_read_write_in_expression::MIXED_READ_WRITE_IN_EXPRESSION),
4949
LintId::of(module_style::MOD_MODULE_FILES),
5050
LintId::of(module_style::SELF_NAMED_MODULE_FILES),
51+
LintId::of(operators::ARITHMETIC),
5152
LintId::of(operators::FLOAT_ARITHMETIC),
5253
LintId::of(operators::FLOAT_CMP_CONST),
5354
LintId::of(operators::INTEGER_ARITHMETIC),

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
548548
store.register_late_pass(|| Box::new(utils::internal_lints::MsrvAttrImpl));
549549
}
550550

551+
let arithmetic_allowed = conf.arithmetic_allowed.clone();
552+
store.register_late_pass(move || Box::new(operators::arithmetic::Arithmetic::new(arithmetic_allowed.clone())));
551553
store.register_late_pass(|| Box::new(utils::dump_hir::DumpHir));
552554
store.register_late_pass(|| Box::new(utils::author::Author));
553555
let await_holding_invalid_types = conf.await_holding_invalid_types.clone();
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#![allow(
2+
// False positive
3+
clippy::match_same_arms
4+
)]
5+
6+
use super::ARITHMETIC;
7+
use clippy_utils::{consts::constant_simple, diagnostics::span_lint};
8+
use rustc_data_structures::fx::FxHashSet;
9+
use rustc_hir as hir;
10+
use rustc_lint::{LateContext, LateLintPass};
11+
use rustc_session::impl_lint_pass;
12+
use rustc_span::source_map::Span;
13+
14+
const HARD_CODED_ALLOWED: &[&str] = &["std::num::Saturating", "std::string::String", "std::num::Wrapping"];
15+
16+
#[derive(Debug)]
17+
pub struct Arithmetic {
18+
allowed: FxHashSet<String>,
19+
// Used to check whether expressions are constants, such as in enum discriminants and consts
20+
const_span: Option<Span>,
21+
expr_span: Option<Span>,
22+
}
23+
24+
impl_lint_pass!(Arithmetic => [ARITHMETIC]);
25+
26+
impl Arithmetic {
27+
#[must_use]
28+
pub fn new(mut allowed: FxHashSet<String>) -> Self {
29+
allowed.extend(HARD_CODED_ALLOWED.iter().copied().map(String::from));
30+
Self {
31+
allowed,
32+
const_span: None,
33+
expr_span: None,
34+
}
35+
}
36+
37+
/// Checks if the given `expr` has any of the inner `allowed` elements.
38+
fn is_allowed_ty(&self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
39+
self.allowed.contains(
40+
cx.typeck_results()
41+
.expr_ty(expr)
42+
.to_string()
43+
.split('<')
44+
.next()
45+
.unwrap_or_default(),
46+
)
47+
}
48+
49+
fn issue_lint(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
50+
span_lint(cx, ARITHMETIC, expr.span, "arithmetic detected");
51+
self.expr_span = Some(expr.span);
52+
}
53+
}
54+
55+
impl<'tcx> LateLintPass<'tcx> for Arithmetic {
56+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
57+
if self.expr_span.is_some() {
58+
return;
59+
}
60+
if let Some(span) = self.const_span && span.contains(expr.span) {
61+
return;
62+
}
63+
match &expr.kind {
64+
hir::ExprKind::Binary(op, lhs, rhs) | hir::ExprKind::AssignOp(op, lhs, rhs) => {
65+
let (
66+
hir::BinOpKind::Add
67+
| hir::BinOpKind::Sub
68+
| hir::BinOpKind::Mul
69+
| hir::BinOpKind::Div
70+
| hir::BinOpKind::Rem
71+
| hir::BinOpKind::Shl
72+
| hir::BinOpKind::Shr
73+
) = op.node else {
74+
return;
75+
};
76+
if self.is_allowed_ty(cx, lhs) || self.is_allowed_ty(cx, rhs) {
77+
return;
78+
}
79+
self.issue_lint(cx, expr);
80+
},
81+
hir::ExprKind::Unary(hir::UnOp::Neg, _) => {
82+
// CTFE already takes care of things like `-1` that do not overflow.
83+
if constant_simple(cx, cx.typeck_results(), expr).is_none() {
84+
self.issue_lint(cx, expr);
85+
}
86+
},
87+
_ => {},
88+
}
89+
}
90+
91+
fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
92+
let body_owner = cx.tcx.hir().body_owner_def_id(body.id());
93+
match cx.tcx.hir().body_owner_kind(body_owner) {
94+
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => {
95+
let body_span = cx.tcx.def_span(body_owner);
96+
if let Some(span) = self.const_span && span.contains(body_span) {
97+
return;
98+
}
99+
self.const_span = Some(body_span);
100+
},
101+
hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => {},
102+
}
103+
}
104+
105+
fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
106+
let body_owner = cx.tcx.hir().body_owner(body.id());
107+
let body_span = cx.tcx.hir().span(body_owner);
108+
if let Some(span) = self.const_span && span.contains(body_span) {
109+
return;
110+
}
111+
self.const_span = None;
112+
}
113+
114+
fn check_expr_post(&mut self, _: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
115+
if Some(expr.span) == self.expr_span {
116+
self.expr_span = None;
117+
}
118+
}
119+
}

clippy_lints/src/operators/mod.rs

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use rustc_hir::{Body, Expr, ExprKind, UnOp};
22
use rustc_lint::{LateContext, LateLintPass};
33
use rustc_session::{declare_tool_lint, impl_lint_pass};
44

5+
pub(crate) mod arithmetic;
6+
57
mod absurd_extreme_comparisons;
68
mod assign_op_pattern;
79
mod bit_mask;
@@ -57,6 +59,42 @@ declare_clippy_lint! {
5759
"a comparison with a maximum or minimum value that is always true or false"
5860
}
5961

62+
declare_clippy_lint! {
63+
/// ### What it does
64+
/// Checks for any kind of arithmetic operation of any type.
65+
///
66+
/// Operators like `+`, `-`, `*` or `<<` are usually capable of overflowing according to the [Rust
67+
/// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
68+
/// or can panic (`/`, `%`). Known safe built-in types like `Wrapping` or `Saturing` are filtered
69+
/// away.
70+
///
71+
/// ### Why is this bad?
72+
/// Integer overflow will trigger a panic in debug builds or will wrap in
73+
/// release mode. Division by zero will cause a panic in either mode. In some applications one
74+
/// wants explicitly checked, wrapping or saturating arithmetic.
75+
///
76+
/// #### Example
77+
/// ```rust
78+
/// # let a = 0;
79+
/// a + 1;
80+
/// ```
81+
///
82+
/// Third-party types also tend to overflow.
83+
///
84+
/// #### Example
85+
/// ```ignore,rust
86+
/// use rust_decimal::Decimal;
87+
/// let _n = Decimal::MAX + Decimal::MAX;
88+
/// ```
89+
///
90+
/// ### Allowed types
91+
/// Custom allowed types can be specified through the "arithmetic-allowed" filter.
92+
#[clippy::version = "1.64.0"]
93+
pub ARITHMETIC,
94+
restriction,
95+
"any arithmetic expression that could overflow or panic"
96+
}
97+
6098
declare_clippy_lint! {
6199
/// ### What it does
62100
/// Checks for integer arithmetic operations which could overflow or panic.
@@ -747,6 +785,7 @@ pub struct Operators {
747785
}
748786
impl_lint_pass!(Operators => [
749787
ABSURD_EXTREME_COMPARISONS,
788+
ARITHMETIC,
750789
INTEGER_ARITHMETIC,
751790
FLOAT_ARITHMETIC,
752791
ASSIGN_OP_PATTERN,

clippy_lints/src/utils/conf.rs

+4
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ macro_rules! define_Conf {
191191
}
192192

193193
define_Conf! {
194+
/// Lint: Arithmetic.
195+
///
196+
/// Suppress checking of the passed type names.
197+
(arithmetic_allowed: rustc_data_structures::fx::FxHashSet<String> = <_>::default()),
194198
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UNUSED_SELF, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX.
195199
///
196200
/// Suppress lints whenever the suggested change would cause breakage for other crates.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![warn(clippy::arithmetic)]
2+
3+
use core::ops::Add;
4+
5+
#[derive(Clone, Copy)]
6+
struct Point {
7+
x: i32,
8+
y: i32,
9+
}
10+
11+
impl Add for Point {
12+
type Output = Self;
13+
14+
fn add(self, other: Self) -> Self {
15+
todo!()
16+
}
17+
}
18+
19+
fn main() {
20+
let _ = Point { x: 1, y: 0 } + Point { x: 2, y: 3 };
21+
22+
let point: Point = Point { x: 1, y: 0 };
23+
let _ = point + point;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arithmetic-allowed = ["Point"]

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
33
allow-expect-in-tests
44
allow-unwrap-in-tests
55
allowed-scripts
6+
arithmetic-allowed
67
array-size-threshold
78
avoid-breaking-exported-api
89
await-holding-invalid-types

tests/ui/arithmetic.fixed

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// run-rustfix
2+
3+
#![allow(clippy::unnecessary_owned_empty_strings)]
4+
#![feature(saturating_int_impl)]
5+
#![warn(clippy::arithmetic)]
6+
7+
use core::num::{Saturating, Wrapping};
8+
9+
pub fn hard_coded_allowed() {
10+
let _ = Saturating(0u32) + Saturating(0u32);
11+
let _ = String::new() + "";
12+
let _ = Wrapping(0u32) + Wrapping(0u32);
13+
14+
let saturating: Saturating<u32> = Saturating(0u32);
15+
let string: String = String::new();
16+
let wrapping: Wrapping<u32> = Wrapping(0u32);
17+
18+
let inferred_saturating = saturating + saturating;
19+
let inferred_string = string + "";
20+
let inferred_wrapping = wrapping + wrapping;
21+
22+
let _ = inferred_saturating + inferred_saturating;
23+
let _ = inferred_string + "";
24+
let _ = inferred_wrapping + inferred_wrapping;
25+
}
26+
27+
fn main() {}

tests/ui/arithmetic.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// run-rustfix
2+
3+
#![allow(clippy::unnecessary_owned_empty_strings)]
4+
#![feature(saturating_int_impl)]
5+
#![warn(clippy::arithmetic)]
6+
7+
use core::num::{Saturating, Wrapping};
8+
9+
pub fn hard_coded_allowed() {
10+
let _ = Saturating(0u32) + Saturating(0u32);
11+
let _ = String::new() + "";
12+
let _ = Wrapping(0u32) + Wrapping(0u32);
13+
14+
let saturating: Saturating<u32> = Saturating(0u32);
15+
let string: String = String::new();
16+
let wrapping: Wrapping<u32> = Wrapping(0u32);
17+
18+
let inferred_saturating = saturating + saturating;
19+
let inferred_string = string + "";
20+
let inferred_wrapping = wrapping + wrapping;
21+
22+
let _ = inferred_saturating + inferred_saturating;
23+
let _ = inferred_string + "";
24+
let _ = inferred_wrapping + inferred_wrapping;
25+
}
26+
27+
fn main() {}

0 commit comments

Comments
 (0)