mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 06:30:32 +00:00
Create new inverted_saturating_sub
lint
This commit is contained in:
parent
27c6343365
commit
d20fc38f0a
6 changed files with 37 additions and 6 deletions
|
@ -5500,6 +5500,7 @@ Released 2018-09-13
|
||||||
[`invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex
|
[`invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex
|
||||||
[`invalid_upcast_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_upcast_comparisons
|
[`invalid_upcast_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_upcast_comparisons
|
||||||
[`invalid_utf8_in_unchecked`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_utf8_in_unchecked
|
[`invalid_utf8_in_unchecked`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_utf8_in_unchecked
|
||||||
|
[`inverted_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
|
||||||
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
|
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
|
||||||
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
|
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
|
||||||
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
|
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
|
||||||
|
|
|
@ -216,6 +216,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
|
||||||
crate::implicit_return::IMPLICIT_RETURN_INFO,
|
crate::implicit_return::IMPLICIT_RETURN_INFO,
|
||||||
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
|
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
|
||||||
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
|
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
|
||||||
|
crate::implicit_saturating_sub::INVERTED_SATURATING_SUB_INFO,
|
||||||
crate::implied_bounds_in_impls::IMPLIED_BOUNDS_IN_IMPLS_INFO,
|
crate::implied_bounds_in_impls::IMPLIED_BOUNDS_IN_IMPLS_INFO,
|
||||||
crate::incompatible_msrv::INCOMPATIBLE_MSRV_INFO,
|
crate::incompatible_msrv::INCOMPATIBLE_MSRV_INFO,
|
||||||
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
|
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
|
||||||
|
|
|
@ -41,7 +41,37 @@ declare_clippy_lint! {
|
||||||
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
|
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
|
||||||
}
|
}
|
||||||
|
|
||||||
declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB]);
|
declare_clippy_lint! {
|
||||||
|
/// ### What it does
|
||||||
|
/// Checks for comparisons between integers, followed by subtracting the greater value from the
|
||||||
|
/// lower one.
|
||||||
|
///
|
||||||
|
/// ### Why is this bad?
|
||||||
|
/// This could result in an underflow and is most likely not what the user wants. If this was
|
||||||
|
/// intended to be a saturated subtraction, consider using the `saturating_sub` method directly.
|
||||||
|
///
|
||||||
|
/// ### Example
|
||||||
|
/// ```no_run
|
||||||
|
/// let a = 12u32;
|
||||||
|
/// let b = 13u32;
|
||||||
|
///
|
||||||
|
/// let result = if a > b { b - a } else { 0 };
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Use instead:
|
||||||
|
/// ```no_run
|
||||||
|
/// let a = 12u32;
|
||||||
|
/// let b = 13u32;
|
||||||
|
///
|
||||||
|
/// let result = a.saturating_sub(b);
|
||||||
|
/// ```
|
||||||
|
#[clippy::version = "1.44.0"]
|
||||||
|
pub INVERTED_SATURATING_SUB,
|
||||||
|
correctness,
|
||||||
|
"Check if a variable is smaller than another one and still subtract from it even if smaller"
|
||||||
|
}
|
||||||
|
|
||||||
|
declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
|
impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||||
|
@ -182,7 +212,7 @@ fn check_subtraction(
|
||||||
{
|
{
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
IMPLICIT_SATURATING_SUB,
|
INVERTED_SATURATING_SUB,
|
||||||
condition_span,
|
condition_span,
|
||||||
"inverted arithmetic check before subtraction",
|
"inverted arithmetic check before subtraction",
|
||||||
|diag| {
|
|diag| {
|
||||||
|
|
|
@ -9,8 +9,7 @@ note: this subtraction underflows when `b < a`
|
||||||
|
|
|
|
||||||
LL | let result = if a > b { b - a } else { 0 };
|
LL | let result = if a > b { b - a } else { 0 };
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
|
= note: `#[deny(clippy::inverted_saturating_sub)]` on by default
|
||||||
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
|
|
||||||
|
|
||||||
error: inverted arithmetic check before subtraction
|
error: inverted arithmetic check before subtraction
|
||||||
--> tests/ui/manual_arithmetic_check-2.rs:11:23
|
--> tests/ui/manual_arithmetic_check-2.rs:11:23
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#![warn(clippy::implicit_saturating_sub)]
|
#![warn(clippy::implicit_saturating_sub, clippy::inverted_saturating_sub)]
|
||||||
#![allow(clippy::if_same_then_else)]
|
#![allow(clippy::if_same_then_else)]
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#![warn(clippy::implicit_saturating_sub)]
|
#![warn(clippy::implicit_saturating_sub, clippy::inverted_saturating_sub)]
|
||||||
#![allow(clippy::if_same_then_else)]
|
#![allow(clippy::if_same_then_else)]
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in a new issue