Skip to content

Commit 8aa4e51

Browse files
Create new inverted_saturating_sub lint
1 parent eb37b31 commit 8aa4e51

6 files changed

+36
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5458,6 +5458,7 @@ Released 2018-09-13
54585458
[`invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex
54595459
[`invalid_upcast_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_upcast_comparisons
54605460
[`invalid_utf8_in_unchecked`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_utf8_in_unchecked
5461+
[`inverted_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
54615462
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
54625463
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
54635464
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
220220
crate::implicit_return::IMPLICIT_RETURN_INFO,
221221
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
222222
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
223+
crate::implicit_saturating_sub::INVERTED_SATURATING_SUB_INFO,
223224
crate::implied_bounds_in_impls::IMPLIED_BOUNDS_IN_IMPLS_INFO,
224225
crate::incompatible_msrv::INCOMPATIBLE_MSRV_INFO,
225226
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,

clippy_lints/src/implicit_saturating_sub.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,36 @@ declare_clippy_lint! {
4141
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
4242
}
4343

44-
declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB]);
44+
declare_clippy_lint! {
45+
/// ### What it does
46+
/// Checks for inverted subtraction operations after check.
47+
///
48+
/// ### Why is this bad?
49+
/// If a variable is smaller than another one, but the other one is still subtracted, then
50+
/// it will end in an underflow.
51+
///
52+
/// ### Example
53+
/// ```no_run
54+
/// let a = 12u32;
55+
/// let b = 13u32;
56+
///
57+
/// let result = if a > b { b - a } else { 0 };
58+
/// ```
59+
///
60+
/// Use instead:
61+
/// ```no_run
62+
/// let a = 12u32;
63+
/// let b = 13u32;
64+
///
65+
/// let result = a.saturating_sub(b);
66+
/// ```
67+
#[clippy::version = "1.44.0"]
68+
pub INVERTED_SATURATING_SUB,
69+
correctness,
70+
"Check if a variable is smaller than another one and still subtract from it even if smaller"
71+
}
72+
73+
declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]);
4574

4675
impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
4776
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
@@ -182,7 +211,7 @@ fn check_subtraction(
182211
{
183212
span_lint_and_then(
184213
cx,
185-
IMPLICIT_SATURATING_SUB,
214+
INVERTED_SATURATING_SUB,
186215
condition_span,
187216
"inverted arithmetic check before subtraction",
188217
|diag| {

tests/ui/manual_arithmetic_check-2.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ note: this subtraction underflows when `b < a`
99
|
1010
LL | let result = if a > b { b - a } else { 0 };
1111
| ^^^^^
12-
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
13-
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
12+
= note: `#[deny(clippy::inverted_saturating_sub)]` on by default
1413

1514
error: inverted arithmetic check before subtraction
1615
--> tests/ui/manual_arithmetic_check-2.rs:11:23

tests/ui/manual_arithmetic_check.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::implicit_saturating_sub)]
1+
#![warn(clippy::implicit_saturating_sub, clippy::inverted_saturating_sub)]
22
#![allow(clippy::if_same_then_else)]
33

44
fn main() {

tests/ui/manual_arithmetic_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![warn(clippy::implicit_saturating_sub)]
1+
#![warn(clippy::implicit_saturating_sub, clippy::inverted_saturating_sub)]
22
#![allow(clippy::if_same_then_else)]
33

44
fn main() {

0 commit comments

Comments
 (0)