Skip to content

Commit d20fc38

Browse files
Create new inverted_saturating_sub lint
1 parent 27c6343 commit d20fc38

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5500,6 +5500,7 @@ Released 2018-09-13
55005500
[`invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex
55015501
[`invalid_upcast_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_upcast_comparisons
55025502
[`invalid_utf8_in_unchecked`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_utf8_in_unchecked
5503+
[`inverted_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
55035504
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
55045505
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
55055506
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
216216
crate::implicit_return::IMPLICIT_RETURN_INFO,
217217
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
218218
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
219+
crate::implicit_saturating_sub::INVERTED_SATURATING_SUB_INFO,
219220
crate::implied_bounds_in_impls::IMPLIED_BOUNDS_IN_IMPLS_INFO,
220221
crate::incompatible_msrv::INCOMPATIBLE_MSRV_INFO,
221222
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,37 @@ 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 comparisons between integers, followed by subtracting the greater value from the
47+
/// lower one.
48+
///
49+
/// ### Why is this bad?
50+
/// This could result in an underflow and is most likely not what the user wants. If this was
51+
/// intended to be a saturated subtraction, consider using the `saturating_sub` method directly.
52+
///
53+
/// ### Example
54+
/// ```no_run
55+
/// let a = 12u32;
56+
/// let b = 13u32;
57+
///
58+
/// let result = if a > b { b - a } else { 0 };
59+
/// ```
60+
///
61+
/// Use instead:
62+
/// ```no_run
63+
/// let a = 12u32;
64+
/// let b = 13u32;
65+
///
66+
/// let result = a.saturating_sub(b);
67+
/// ```
68+
#[clippy::version = "1.44.0"]
69+
pub INVERTED_SATURATING_SUB,
70+
correctness,
71+
"Check if a variable is smaller than another one and still subtract from it even if smaller"
72+
}
73+
74+
declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]);
4575

4676
impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
4777
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
@@ -182,7 +212,7 @@ fn check_subtraction(
182212
{
183213
span_lint_and_then(
184214
cx,
185-
IMPLICIT_SATURATING_SUB,
215+
INVERTED_SATURATING_SUB,
186216
condition_span,
187217
"inverted arithmetic check before subtraction",
188218
|diag| {

tests/ui/manual_arithmetic_check-2.stderr

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)