Skip to content

Commit 448b6f4

Browse files
committed
Move Arithmetic into Operators lint pass
1 parent dd78ce7 commit 448b6f4

File tree

6 files changed

+213
-183
lines changed

6 files changed

+213
-183
lines changed

clippy_lints/src/lib.register_lints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,12 @@ store.register_lints(&[
431431
non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS,
432432
non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY,
433433
nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES,
434-
numeric_arithmetic::FLOAT_ARITHMETIC,
435-
numeric_arithmetic::INTEGER_ARITHMETIC,
436434
octal_escapes::OCTAL_ESCAPES,
437435
only_used_in_recursion::ONLY_USED_IN_RECURSION,
438436
open_options::NONSENSICAL_OPEN_OPTIONS,
439437
operators::ABSURD_EXTREME_COMPARISONS,
438+
operators::FLOAT_ARITHMETIC,
439+
operators::INTEGER_ARITHMETIC,
440440
option_env_unwrap::OPTION_ENV_UNWRAP,
441441
option_if_let_else::OPTION_IF_LET_ELSE,
442442
overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,

clippy_lints/src/lib.register_restriction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
5050
LintId::of(module_style::MOD_MODULE_FILES),
5151
LintId::of(module_style::SELF_NAMED_MODULE_FILES),
5252
LintId::of(modulo_arithmetic::MODULO_ARITHMETIC),
53-
LintId::of(numeric_arithmetic::FLOAT_ARITHMETIC),
54-
LintId::of(numeric_arithmetic::INTEGER_ARITHMETIC),
53+
LintId::of(operators::FLOAT_ARITHMETIC),
54+
LintId::of(operators::INTEGER_ARITHMETIC),
5555
LintId::of(panic_in_result_fn::PANIC_IN_RESULT_FN),
5656
LintId::of(panic_unimplemented::PANIC),
5757
LintId::of(panic_unimplemented::TODO),

clippy_lints/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ mod non_expressive_names;
331331
mod non_octal_unix_permissions;
332332
mod non_send_fields_in_send_ty;
333333
mod nonstandard_macro_braces;
334-
mod numeric_arithmetic;
335334
mod octal_escapes;
336335
mod only_used_in_recursion;
337336
mod open_options;
@@ -705,7 +704,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
705704
store.register_late_pass(move || Box::new(doc::DocMarkdown::new(doc_valid_idents.clone())));
706705
store.register_late_pass(|| Box::new(neg_multiply::NegMultiply));
707706
store.register_late_pass(|| Box::new(mem_forget::MemForget));
708-
store.register_late_pass(|| Box::new(numeric_arithmetic::NumericArithmetic::default()));
709707
store.register_late_pass(|| Box::new(assign_ops::AssignOps));
710708
store.register_late_pass(|| Box::new(let_if_seq::LetIfSeq));
711709
store.register_late_pass(|| Box::new(mixed_read_write_in_expression::EvalOrderDependence));
@@ -940,7 +938,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
940938
store.register_late_pass(|| Box::new(default_instead_of_iter_empty::DefaultIterEmpty));
941939
store.register_late_pass(move || Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv)));
942940
store.register_late_pass(move || Box::new(manual_retain::ManualRetain::new(msrv)));
943-
store.register_late_pass(|| Box::new(operators::Operators));
941+
store.register_late_pass(|| Box::new(operators::Operators::default()));
944942
// add lints here, do not remove this comment, it's used in `new_lint`
945943
}
946944

clippy_lints/src/numeric_arithmetic.rs

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

clippy_lints/src/operators/mod.rs

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use rustc_hir::{Expr, ExprKind};
1+
use rustc_hir::{Body, Expr, ExprKind, UnOp};
22
use rustc_lint::{LateContext, LateLintPass};
33
use rustc_session::{declare_tool_lint, impl_lint_pass};
44

55
mod absurd_extreme_comparisons;
6+
mod numeric_arithmetic;
67

78
declare_clippy_lint! {
89
/// ### What it does
@@ -36,16 +37,90 @@ declare_clippy_lint! {
3637
"a comparison with a maximum or minimum value that is always true or false"
3738
}
3839

39-
pub struct Operators;
40+
declare_clippy_lint! {
41+
/// ### What it does
42+
/// Checks for integer arithmetic operations which could overflow or panic.
43+
///
44+
/// Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable
45+
/// of overflowing according to the [Rust
46+
/// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
47+
/// or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is
48+
/// attempted.
49+
///
50+
/// ### Why is this bad?
51+
/// Integer overflow will trigger a panic in debug builds or will wrap in
52+
/// release mode. Division by zero will cause a panic in either mode. In some applications one
53+
/// wants explicitly checked, wrapping or saturating arithmetic.
54+
///
55+
/// ### Example
56+
/// ```rust
57+
/// # let a = 0;
58+
/// a + 1;
59+
/// ```
60+
#[clippy::version = "pre 1.29.0"]
61+
pub INTEGER_ARITHMETIC,
62+
restriction,
63+
"any integer arithmetic expression which could overflow or panic"
64+
}
65+
66+
declare_clippy_lint! {
67+
/// ### What it does
68+
/// Checks for float arithmetic.
69+
///
70+
/// ### Why is this bad?
71+
/// For some embedded systems or kernel development, it
72+
/// can be useful to rule out floating-point numbers.
73+
///
74+
/// ### Example
75+
/// ```rust
76+
/// # let a = 0.0;
77+
/// a + 1.0;
78+
/// ```
79+
#[clippy::version = "pre 1.29.0"]
80+
pub FLOAT_ARITHMETIC,
81+
restriction,
82+
"any floating-point arithmetic statement"
83+
}
84+
85+
#[derive(Default)]
86+
pub struct Operators {
87+
arithmetic_context: numeric_arithmetic::Context,
88+
}
4089
impl_lint_pass!(Operators => [
4190
ABSURD_EXTREME_COMPARISONS,
91+
INTEGER_ARITHMETIC,
92+
FLOAT_ARITHMETIC,
4293
]);
4394
impl<'tcx> LateLintPass<'tcx> for Operators {
4495
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
45-
if let ExprKind::Binary(op, lhs, rhs) = e.kind {
46-
if !e.span.from_expansion() {
47-
absurd_extreme_comparisons::check(cx, e, op.node, lhs, rhs);
48-
}
96+
match e.kind {
97+
ExprKind::Binary(op, lhs, rhs) => {
98+
if !e.span.from_expansion() {
99+
absurd_extreme_comparisons::check(cx, e, op.node, lhs, rhs);
100+
}
101+
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
102+
},
103+
ExprKind::AssignOp(op, lhs, rhs) => {
104+
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
105+
},
106+
ExprKind::Unary(op, arg) => {
107+
if op == UnOp::Neg {
108+
self.arithmetic_context.check_negate(cx, e, arg);
109+
}
110+
},
111+
_ => (),
49112
}
50113
}
114+
115+
fn check_expr_post(&mut self, _: &LateContext<'_>, e: &Expr<'_>) {
116+
self.arithmetic_context.expr_post(e.hir_id);
117+
}
118+
119+
fn check_body(&mut self, cx: &LateContext<'tcx>, b: &'tcx Body<'_>) {
120+
self.arithmetic_context.enter_body(cx, b);
121+
}
122+
123+
fn check_body_post(&mut self, cx: &LateContext<'tcx>, b: &'tcx Body<'_>) {
124+
self.arithmetic_context.body_post(cx, b);
125+
}
51126
}

0 commit comments

Comments
 (0)