From 7c52e51d79696ae99e4f8499bb51f81b464d5a1a Mon Sep 17 00:00:00 2001 From: pmk21 Date: Mon, 6 Apr 2020 23:37:57 +0530 Subject: [PATCH 1/3] Added basic lint and tests --- CHANGELOG.md | 1 + clippy_lints/src/implicit_saturating_sub.rs | 90 +++++++++++++++++++++ clippy_lints/src/lib.rs | 4 + src/lintlist/mod.rs | 7 ++ tests/ui/implicit_saturating_sub.rs | 21 +++++ 5 files changed, 123 insertions(+) create mode 100644 clippy_lints/src/implicit_saturating_sub.rs create mode 100644 tests/ui/implicit_saturating_sub.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 60ad855d7f87e..e513787a53a65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1293,6 +1293,7 @@ Released 2018-09-13 [`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond [`implicit_hasher`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_hasher [`implicit_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_return +[`implicit_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_saturating_sub [`imprecise_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#imprecise_flops [`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping [`indexing_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing diff --git a/clippy_lints/src/implicit_saturating_sub.rs b/clippy_lints/src/implicit_saturating_sub.rs new file mode 100644 index 0000000000000..e2dff92834d5a --- /dev/null +++ b/clippy_lints/src/implicit_saturating_sub.rs @@ -0,0 +1,90 @@ +use crate::utils::{higher, in_macro, span_lint_and_sugg}; +use if_chain::if_chain; +use rustc_ast::ast::LitKind; +use rustc_errors::Applicability; +use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, StmtKind}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; + +declare_clippy_lint! { + /// **What it does:** Checks for implicit saturating subtraction. + /// + /// **Why is this bad?** Simplicity and readability. Instead we can easily use an inbuilt function. + /// + /// **Known problems:** None. + /// + /// **Example:** + /// + /// ```rust + /// let end = 10; + /// let start = 5; + /// + /// let mut i = end - start; + /// + /// // Bad + /// if i != 0 { + /// i -= 1; + /// } + /// ``` + /// Use instead: + /// ```rust + /// let end = 10; + /// let start = 5; + /// + /// let mut i = end - start; + /// + /// // Good + /// i.saturating_sub(1); + /// ``` + pub IMPLICIT_SATURATING_SUB, + pedantic, + "Perform saturating subtraction instead of implicitly checking lower bound of data type" +} + +declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB]); + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub { + fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) { + if in_macro(expr.span) { + return; + } + if_chain! { + if let Some((ref cond, ref then, None)) = higher::if_block(&expr); + // Check if the conditional expression is a binary operation + if let ExprKind::Binary(ref op, ref left, ref right) = cond.kind; + // Ensure that the binary operator is > or != + if BinOpKind::Ne == op.node || BinOpKind::Gt == op.node; + if let ExprKind::Path(ref cond_path) = left.kind; + // Get the literal on the right hand side + if let ExprKind::Lit(ref lit) = right.kind; + if let LitKind::Int(0, _) = lit.node; + // Check if the true condition block has only one statement + if let ExprKind::Block(ref block, _) = then.kind; + if block.stmts.len() == 1; + // Check if assign operation is done + if let StmtKind::Semi(ref e) = block.stmts[0].kind; + if let ExprKind::AssignOp(ref op1, ref target, ref value) = e.kind; + if BinOpKind::Sub == op1.node; + if let ExprKind::Path(ref assign_path) = target.kind; + // Check if the variable in the condition and assignment statement are the same + if let (QPath::Resolved(_, ref cres_path), QPath::Resolved(_, ref ares_path)) = (cond_path, assign_path); + if cres_path.res == ares_path.res; + if let ExprKind::Lit(ref lit1) = value.kind; + if let LitKind::Int(assign_lit, _) = lit1.node; + then { + // Get the variable name + let var_name = ares_path.segments[0].ident.name.as_str(); + let applicability = Applicability::MaybeIncorrect; + span_lint_and_sugg( + cx, + IMPLICIT_SATURATING_SUB, + expr.span, + "Implicitly performing saturating subtraction", + "try", + format!("{}.saturating_sub({});", var_name, assign_lit.to_string()), + applicability + ); + } + } + } +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index a5b55d2ab704b..b8415fa3af125 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -225,6 +225,7 @@ mod identity_op; mod if_let_some_result; mod if_not_else; mod implicit_return; +mod implicit_saturating_sub; mod indexing_slicing; mod infinite_iter; mod inherent_impl; @@ -574,6 +575,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &if_let_some_result::IF_LET_SOME_RESULT, &if_not_else::IF_NOT_ELSE, &implicit_return::IMPLICIT_RETURN, + &implicit_saturating_sub::IMPLICIT_SATURATING_SUB, &indexing_slicing::INDEXING_SLICING, &indexing_slicing::OUT_OF_BOUNDS_INDEXING, &infinite_iter::INFINITE_ITER, @@ -888,6 +890,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|| box unicode::Unicode); store.register_late_pass(|| box strings::StringAdd); store.register_late_pass(|| box implicit_return::ImplicitReturn); + store.register_late_pass(|| box implicit_saturating_sub::ImplicitSaturatingSub); store.register_late_pass(|| box methods::Methods); store.register_late_pass(|| box map_clone::MapClone); store.register_late_pass(|| box shadow::Shadow); @@ -1111,6 +1114,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&functions::MUST_USE_CANDIDATE), LintId::of(&functions::TOO_MANY_LINES), LintId::of(&if_not_else::IF_NOT_ELSE), + LintId::of(&implicit_saturating_sub::IMPLICIT_SATURATING_SUB), LintId::of(&infinite_iter::MAYBE_INFINITE_ITER), LintId::of(&items_after_statements::ITEMS_AFTER_STATEMENTS), LintId::of(&large_stack_arrays::LARGE_STACK_ARRAYS), diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index cf2537e6d6697..213d054e403da 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -773,6 +773,13 @@ pub static ref ALL_LINTS: Vec = vec![ deprecation: None, module: "implicit_return", }, + Lint { + name: "implicit_saturating_sub", + group: "pedantic", + desc: "Perform saturating subtraction instead of implicitly checking lower bound of data type", + deprecation: None, + module: "implicit_saturating_sub", + }, Lint { name: "imprecise_flops", group: "nursery", diff --git a/tests/ui/implicit_saturating_sub.rs b/tests/ui/implicit_saturating_sub.rs new file mode 100644 index 0000000000000..c1cc00bb68563 --- /dev/null +++ b/tests/ui/implicit_saturating_sub.rs @@ -0,0 +1,21 @@ +#![warn(clippy::implicit_saturating_sub)] + +fn main() { + let mut end = 10; + let mut start = 5; + let mut i: u32 = end - start; + + if i > 0 { + i -= 1; + } + + match end { + 10 => { + if i > 0 { + i -= 1; + } + }, + 11 => i += 1, + _ => i = 0, + } +} From 4d03b4d2f0e967b0681bbdb59fa8271ed3be034f Mon Sep 17 00:00:00 2001 From: pmk21 Date: Mon, 6 Apr 2020 23:40:41 +0530 Subject: [PATCH 2/3] Added final lint and tests --- clippy_lints/src/implicit_saturating_sub.rs | 151 ++++++++++---- tests/ui/implicit_saturating_sub.fixed | 160 +++++++++++++++ tests/ui/implicit_saturating_sub.rs | 205 +++++++++++++++++++- tests/ui/implicit_saturating_sub.stderr | 188 ++++++++++++++++++ 4 files changed, 660 insertions(+), 44 deletions(-) create mode 100644 tests/ui/implicit_saturating_sub.fixed create mode 100644 tests/ui/implicit_saturating_sub.stderr diff --git a/clippy_lints/src/implicit_saturating_sub.rs b/clippy_lints/src/implicit_saturating_sub.rs index e2dff92834d5a..155a93de4facf 100644 --- a/clippy_lints/src/implicit_saturating_sub.rs +++ b/clippy_lints/src/implicit_saturating_sub.rs @@ -1,4 +1,4 @@ -use crate::utils::{higher, in_macro, span_lint_and_sugg}; +use crate::utils::{higher, in_macro, match_qpath, span_lint_and_sugg, SpanlessEq}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; @@ -9,17 +9,17 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; declare_clippy_lint! { /// **What it does:** Checks for implicit saturating subtraction. /// - /// **Why is this bad?** Simplicity and readability. Instead we can easily use an inbuilt function. + /// **Why is this bad?** Simplicity and readability. Instead we can easily use an builtin function. /// /// **Known problems:** None. /// /// **Example:** /// /// ```rust - /// let end = 10; - /// let start = 5; + /// let end: u32 = 10; + /// let start: u32 = 5; /// - /// let mut i = end - start; + /// let mut i: u32 = end - start; /// /// // Bad /// if i != 0 { @@ -28,13 +28,13 @@ declare_clippy_lint! { /// ``` /// Use instead: /// ```rust - /// let end = 10; - /// let start = 5; + /// let end: u32 = 10; + /// let start: u32 = 5; /// - /// let mut i = end - start; + /// let mut i: u32 = end - start; /// /// // Good - /// i.saturating_sub(1); + /// i = i.saturating_sub(1); /// ``` pub IMPLICIT_SATURATING_SUB, pedantic, @@ -50,41 +50,124 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub { } if_chain! { if let Some((ref cond, ref then, None)) = higher::if_block(&expr); + // Check if the conditional expression is a binary operation - if let ExprKind::Binary(ref op, ref left, ref right) = cond.kind; - // Ensure that the binary operator is > or != - if BinOpKind::Ne == op.node || BinOpKind::Gt == op.node; - if let ExprKind::Path(ref cond_path) = left.kind; - // Get the literal on the right hand side - if let ExprKind::Lit(ref lit) = right.kind; - if let LitKind::Int(0, _) = lit.node; + if let ExprKind::Binary(ref cond_op, ref cond_left, ref cond_right) = cond.kind; + + // Ensure that the binary operator is >, != and < + if BinOpKind::Ne == cond_op.node || BinOpKind::Gt == cond_op.node || BinOpKind::Lt == cond_op.node; + // Check if the true condition block has only one statement if let ExprKind::Block(ref block, _) = then.kind; - if block.stmts.len() == 1; + if block.stmts.len() == 1 && block.expr.is_none(); + // Check if assign operation is done if let StmtKind::Semi(ref e) = block.stmts[0].kind; - if let ExprKind::AssignOp(ref op1, ref target, ref value) = e.kind; - if BinOpKind::Sub == op1.node; + if let Some(target) = subtracts_one(cx, e); + + // Extracting out the variable name if let ExprKind::Path(ref assign_path) = target.kind; - // Check if the variable in the condition and assignment statement are the same - if let (QPath::Resolved(_, ref cres_path), QPath::Resolved(_, ref ares_path)) = (cond_path, assign_path); - if cres_path.res == ares_path.res; - if let ExprKind::Lit(ref lit1) = value.kind; - if let LitKind::Int(assign_lit, _) = lit1.node; + if let QPath::Resolved(_, ref ares_path) = assign_path; + then { + // Handle symmetric conditions in the if statement + let (cond_var, cond_num_val) = if SpanlessEq::new(cx).eq_expr(cond_left, target) { + if BinOpKind::Gt == cond_op.node || BinOpKind::Ne == cond_op.node { + (cond_left, cond_right) + } else { + return; + } + } else if SpanlessEq::new(cx).eq_expr(cond_right, target) { + if BinOpKind::Lt == cond_op.node || BinOpKind::Ne == cond_op.node { + (cond_right, cond_left) + } else { + return; + } + } else { + return; + }; + + // Check if the variable in the condition statement is an integer + if !cx.tables.expr_ty(cond_var).is_integral() { + return; + } + // Get the variable name let var_name = ares_path.segments[0].ident.name.as_str(); - let applicability = Applicability::MaybeIncorrect; - span_lint_and_sugg( - cx, - IMPLICIT_SATURATING_SUB, - expr.span, - "Implicitly performing saturating subtraction", - "try", - format!("{}.saturating_sub({});", var_name, assign_lit.to_string()), - applicability - ); + const INT_TYPES: [&str; 5] = ["i8", "i16", "i32", "i64", "i128"]; + + match cond_num_val.kind { + ExprKind::Lit(ref cond_lit) => { + // Check if the constant is zero + if let LitKind::Int(0, _) = cond_lit.node { + if cx.tables.expr_ty(cond_left).is_signed() { + } else { + print_lint_and_sugg(cx, &var_name, expr); + }; + } + }, + ExprKind::Path(ref cond_num_path) => { + if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "MIN"])) { + print_lint_and_sugg(cx, &var_name, expr); + }; + }, + ExprKind::Call(ref func, _) => { + if let ExprKind::Path(ref cond_num_path) = func.kind { + if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "min_value"])) { + print_lint_and_sugg(cx, &var_name, expr); + } + }; + }, + _ => (), + } } } } } + +fn subtracts_one<'a>(cx: &LateContext<'_, '_>, expr: &Expr<'a>) -> Option<&'a Expr<'a>> { + match expr.kind { + ExprKind::AssignOp(ref op1, ref target, ref value) => { + if_chain! { + if BinOpKind::Sub == op1.node; + // Check if literal being subtracted is one + if let ExprKind::Lit(ref lit1) = value.kind; + if let LitKind::Int(1, _) = lit1.node; + then { + Some(target) + } else { + None + } + } + }, + ExprKind::Assign(ref target, ref value, _) => { + if_chain! { + if let ExprKind::Binary(ref op1, ref left1, ref right1) = value.kind; + if BinOpKind::Sub == op1.node; + + if SpanlessEq::new(cx).eq_expr(left1, target); + + if let ExprKind::Lit(ref lit1) = right1.kind; + if let LitKind::Int(1, _) = lit1.node; + then { + Some(target) + } else { + None + } + } + }, + _ => None, + } +} + +fn print_lint_and_sugg(cx: &LateContext<'_, '_>, var_name: &str, expr: &Expr<'_>) { + span_lint_and_sugg( + cx, + IMPLICIT_SATURATING_SUB, + expr.span, + "Implicitly performing saturating subtraction", + "try", + format!("{} = {}.saturating_sub({});", var_name, var_name, 1.to_string()), + Applicability::MachineApplicable, + ); +} diff --git a/tests/ui/implicit_saturating_sub.fixed b/tests/ui/implicit_saturating_sub.fixed new file mode 100644 index 0000000000000..830d180442e57 --- /dev/null +++ b/tests/ui/implicit_saturating_sub.fixed @@ -0,0 +1,160 @@ +// run-rustfix +#![allow(unused_assignments, unused_mut)] +#![warn(clippy::implicit_saturating_sub)] + +fn main() { + // Tests for unsigned integers + + let end_8: u8 = 10; + let start_8: u8 = 5; + let mut u_8: u8 = end_8 - start_8; + + // Lint + u_8 = u_8.saturating_sub(1); + + match end_8 { + 10 => { + // Lint + u_8 = u_8.saturating_sub(1); + }, + 11 => u_8 += 1, + _ => u_8 = 0, + } + + let end_16: u16 = 35; + let start_16: u16 = 40; + + let mut u_16: u16 = end_16 - start_16; + + // Lint + u_16 = u_16.saturating_sub(1); + + let mut end_32: u32 = 7000; + let mut start_32: u32 = 7010; + + let mut u_32: u32 = end_32 - start_32; + + // Lint + u_32 = u_32.saturating_sub(1); + + // No Lint + if u_32 > 0 { + u_16 += 1; + } + + // No Lint + if u_32 != 0 { + end_32 -= 1; + start_32 += 1; + } + + let mut end_64: u64 = 75001; + let mut start_64: u64 = 75000; + + let mut u_64: u64 = end_64 - start_64; + + // Lint + u_64 = u_64.saturating_sub(1); + + // Lint + u_64 = u_64.saturating_sub(1); + + // Lint + u_64 = u_64.saturating_sub(1); + + // No Lint + if u_64 >= 1 { + u_64 -= 1; + } + + // No Lint + if u_64 > 0 { + end_64 -= 1; + } + + // Tests for usize + let end_usize: usize = 8054; + let start_usize: usize = 8050; + + let mut u_usize: usize = end_usize - start_usize; + + // Lint + u_usize = u_usize.saturating_sub(1); + + // Tests for signed integers + + let endi_8: i8 = 10; + let starti_8: i8 = 50; + + let mut i_8: i8 = endi_8 - starti_8; + + // Lint + i_8 = i_8.saturating_sub(1); + + // Lint + i_8 = i_8.saturating_sub(1); + + // Lint + i_8 = i_8.saturating_sub(1); + + // Lint + i_8 = i_8.saturating_sub(1); + + let endi_16: i16 = 45; + let starti_16: i16 = 44; + + let mut i_16: i16 = endi_16 - starti_16; + + // Lint + i_16 = i_16.saturating_sub(1); + + // Lint + i_16 = i_16.saturating_sub(1); + + // Lint + i_16 = i_16.saturating_sub(1); + + // Lint + i_16 = i_16.saturating_sub(1); + + let endi_32: i32 = 45; + let starti_32: i32 = 44; + + let mut i_32: i32 = endi_32 - starti_32; + + // Lint + i_32 = i_32.saturating_sub(1); + + // Lint + i_32 = i_32.saturating_sub(1); + + // Lint + i_32 = i_32.saturating_sub(1); + + // Lint + i_32 = i_32.saturating_sub(1); + + let endi_64: i64 = 45; + let starti_64: i64 = 44; + + let mut i_64: i64 = endi_64 - starti_64; + + // Lint + i_64 = i_64.saturating_sub(1); + + // Lint + i_64 = i_64.saturating_sub(1); + + // Lint + i_64 = i_64.saturating_sub(1); + + // No Lint + if i_64 > 0 { + i_64 -= 1; + } + + // No Lint + if i_64 != 0 { + i_64 -= 1; + } +} diff --git a/tests/ui/implicit_saturating_sub.rs b/tests/ui/implicit_saturating_sub.rs index c1cc00bb68563..36da4f97163a4 100644 --- a/tests/ui/implicit_saturating_sub.rs +++ b/tests/ui/implicit_saturating_sub.rs @@ -1,21 +1,206 @@ +// run-rustfix +#![allow(unused_assignments, unused_mut)] #![warn(clippy::implicit_saturating_sub)] fn main() { - let mut end = 10; - let mut start = 5; - let mut i: u32 = end - start; + // Tests for unsigned integers - if i > 0 { - i -= 1; + let end_8: u8 = 10; + let start_8: u8 = 5; + let mut u_8: u8 = end_8 - start_8; + + // Lint + if u_8 > 0 { + u_8 -= 1; } - match end { + match end_8 { 10 => { - if i > 0 { - i -= 1; + // Lint + if u_8 > 0 { + u_8 -= 1; } }, - 11 => i += 1, - _ => i = 0, + 11 => u_8 += 1, + _ => u_8 = 0, + } + + let end_16: u16 = 35; + let start_16: u16 = 40; + + let mut u_16: u16 = end_16 - start_16; + + // Lint + if u_16 > 0 { + u_16 -= 1; + } + + let mut end_32: u32 = 7000; + let mut start_32: u32 = 7010; + + let mut u_32: u32 = end_32 - start_32; + + // Lint + if u_32 != 0 { + u_32 -= 1; + } + + // No Lint + if u_32 > 0 { + u_16 += 1; + } + + // No Lint + if u_32 != 0 { + end_32 -= 1; + start_32 += 1; + } + + let mut end_64: u64 = 75001; + let mut start_64: u64 = 75000; + + let mut u_64: u64 = end_64 - start_64; + + // Lint + if u_64 > 0 { + u_64 -= 1; + } + + // Lint + if 0 < u_64 { + u_64 -= 1; + } + + // Lint + if 0 != u_64 { + u_64 -= 1; + } + + // No Lint + if u_64 >= 1 { + u_64 -= 1; + } + + // No Lint + if u_64 > 0 { + end_64 -= 1; + } + + // Tests for usize + let end_usize: usize = 8054; + let start_usize: usize = 8050; + + let mut u_usize: usize = end_usize - start_usize; + + // Lint + if u_usize > 0 { + u_usize -= 1; + } + + // Tests for signed integers + + let endi_8: i8 = 10; + let starti_8: i8 = 50; + + let mut i_8: i8 = endi_8 - starti_8; + + // Lint + if i_8 > i8::MIN { + i_8 -= 1; + } + + // Lint + if i_8 > i8::min_value() { + i_8 -= 1; + } + + // Lint + if i_8 != i8::MIN { + i_8 -= 1; + } + + // Lint + if i_8 != i8::min_value() { + i_8 -= 1; + } + + let endi_16: i16 = 45; + let starti_16: i16 = 44; + + let mut i_16: i16 = endi_16 - starti_16; + + // Lint + if i_16 > i16::MIN { + i_16 -= 1; + } + + // Lint + if i_16 > i16::min_value() { + i_16 -= 1; + } + + // Lint + if i_16 != i16::MIN { + i_16 -= 1; + } + + // Lint + if i_16 != i16::min_value() { + i_16 -= 1; + } + + let endi_32: i32 = 45; + let starti_32: i32 = 44; + + let mut i_32: i32 = endi_32 - starti_32; + + // Lint + if i_32 > i32::MIN { + i_32 -= 1; + } + + // Lint + if i_32 > i32::min_value() { + i_32 -= 1; + } + + // Lint + if i_32 != i32::MIN { + i_32 -= 1; + } + + // Lint + if i_32 != i32::min_value() { + i_32 -= 1; + } + + let endi_64: i64 = 45; + let starti_64: i64 = 44; + + let mut i_64: i64 = endi_64 - starti_64; + + // Lint + if i64::min_value() < i_64 { + i_64 -= 1; + } + + // Lint + if i64::MIN != i_64 { + i_64 -= 1; + } + + // Lint + if i64::MIN < i_64 { + i_64 -= 1; + } + + // No Lint + if i_64 > 0 { + i_64 -= 1; + } + + // No Lint + if i_64 != 0 { + i_64 -= 1; } } diff --git a/tests/ui/implicit_saturating_sub.stderr b/tests/ui/implicit_saturating_sub.stderr new file mode 100644 index 0000000000000..256dbe0c245f4 --- /dev/null +++ b/tests/ui/implicit_saturating_sub.stderr @@ -0,0 +1,188 @@ +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:13:5 + | +LL | / if u_8 > 0 { +LL | | u_8 -= 1; +LL | | } + | |_____^ help: try: `u_8 = u_8.saturating_sub(1);` + | + = note: `-D clippy::implicit-saturating-sub` implied by `-D warnings` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:20:13 + | +LL | / if u_8 > 0 { +LL | | u_8 -= 1; +LL | | } + | |_____________^ help: try: `u_8 = u_8.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:34:5 + | +LL | / if u_16 > 0 { +LL | | u_16 -= 1; +LL | | } + | |_____^ help: try: `u_16 = u_16.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:44:5 + | +LL | / if u_32 != 0 { +LL | | u_32 -= 1; +LL | | } + | |_____^ help: try: `u_32 = u_32.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:65:5 + | +LL | / if u_64 > 0 { +LL | | u_64 -= 1; +LL | | } + | |_____^ help: try: `u_64 = u_64.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:70:5 + | +LL | / if 0 < u_64 { +LL | | u_64 -= 1; +LL | | } + | |_____^ help: try: `u_64 = u_64.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:75:5 + | +LL | / if 0 != u_64 { +LL | | u_64 -= 1; +LL | | } + | |_____^ help: try: `u_64 = u_64.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:96:5 + | +LL | / if u_usize > 0 { +LL | | u_usize -= 1; +LL | | } + | |_____^ help: try: `u_usize = u_usize.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:108:5 + | +LL | / if i_8 > i8::MIN { +LL | | i_8 -= 1; +LL | | } + | |_____^ help: try: `i_8 = i_8.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:113:5 + | +LL | / if i_8 > i8::min_value() { +LL | | i_8 -= 1; +LL | | } + | |_____^ help: try: `i_8 = i_8.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:118:5 + | +LL | / if i_8 != i8::MIN { +LL | | i_8 -= 1; +LL | | } + | |_____^ help: try: `i_8 = i_8.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:123:5 + | +LL | / if i_8 != i8::min_value() { +LL | | i_8 -= 1; +LL | | } + | |_____^ help: try: `i_8 = i_8.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:133:5 + | +LL | / if i_16 > i16::MIN { +LL | | i_16 -= 1; +LL | | } + | |_____^ help: try: `i_16 = i_16.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:138:5 + | +LL | / if i_16 > i16::min_value() { +LL | | i_16 -= 1; +LL | | } + | |_____^ help: try: `i_16 = i_16.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:143:5 + | +LL | / if i_16 != i16::MIN { +LL | | i_16 -= 1; +LL | | } + | |_____^ help: try: `i_16 = i_16.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:148:5 + | +LL | / if i_16 != i16::min_value() { +LL | | i_16 -= 1; +LL | | } + | |_____^ help: try: `i_16 = i_16.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:158:5 + | +LL | / if i_32 > i32::MIN { +LL | | i_32 -= 1; +LL | | } + | |_____^ help: try: `i_32 = i_32.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:163:5 + | +LL | / if i_32 > i32::min_value() { +LL | | i_32 -= 1; +LL | | } + | |_____^ help: try: `i_32 = i_32.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:168:5 + | +LL | / if i_32 != i32::MIN { +LL | | i_32 -= 1; +LL | | } + | |_____^ help: try: `i_32 = i_32.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:173:5 + | +LL | / if i_32 != i32::min_value() { +LL | | i_32 -= 1; +LL | | } + | |_____^ help: try: `i_32 = i_32.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:183:5 + | +LL | / if i64::min_value() < i_64 { +LL | | i_64 -= 1; +LL | | } + | |_____^ help: try: `i_64 = i_64.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:188:5 + | +LL | / if i64::MIN != i_64 { +LL | | i_64 -= 1; +LL | | } + | |_____^ help: try: `i_64 = i_64.saturating_sub(1);` + +error: Implicitly performing saturating subtraction + --> $DIR/implicit_saturating_sub.rs:193:5 + | +LL | / if i64::MIN < i_64 { +LL | | i_64 -= 1; +LL | | } + | |_____^ help: try: `i_64 = i_64.saturating_sub(1);` + +error: aborting due to 23 previous errors + From 1c1103561ea3ce6d9ddba97061496a25884ee671 Mon Sep 17 00:00:00 2001 From: pmk21 Date: Sat, 18 Apr 2020 01:16:32 +0530 Subject: [PATCH 3/3] Polished lint and tests --- tests/ui/implicit_saturating_sub.fixed | 2 +- tests/ui/implicit_saturating_sub.rs | 4 ++-- tests/ui/implicit_saturating_sub.stderr | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ui/implicit_saturating_sub.fixed b/tests/ui/implicit_saturating_sub.fixed index 830d180442e57..fd4ba71f6afcb 100644 --- a/tests/ui/implicit_saturating_sub.fixed +++ b/tests/ui/implicit_saturating_sub.fixed @@ -1,5 +1,5 @@ // run-rustfix -#![allow(unused_assignments, unused_mut)] +#![allow(unused_assignments, unused_mut, clippy::assign_op_pattern)] #![warn(clippy::implicit_saturating_sub)] fn main() { diff --git a/tests/ui/implicit_saturating_sub.rs b/tests/ui/implicit_saturating_sub.rs index 36da4f97163a4..56c1be005c044 100644 --- a/tests/ui/implicit_saturating_sub.rs +++ b/tests/ui/implicit_saturating_sub.rs @@ -1,5 +1,5 @@ // run-rustfix -#![allow(unused_assignments, unused_mut)] +#![allow(unused_assignments, unused_mut, clippy::assign_op_pattern)] #![warn(clippy::implicit_saturating_sub)] fn main() { @@ -11,7 +11,7 @@ fn main() { // Lint if u_8 > 0 { - u_8 -= 1; + u_8 = u_8 - 1; } match end_8 { diff --git a/tests/ui/implicit_saturating_sub.stderr b/tests/ui/implicit_saturating_sub.stderr index 256dbe0c245f4..a8ba870b1dda6 100644 --- a/tests/ui/implicit_saturating_sub.stderr +++ b/tests/ui/implicit_saturating_sub.stderr @@ -2,7 +2,7 @@ error: Implicitly performing saturating subtraction --> $DIR/implicit_saturating_sub.rs:13:5 | LL | / if u_8 > 0 { -LL | | u_8 -= 1; +LL | | u_8 = u_8 - 1; LL | | } | |_____^ help: try: `u_8 = u_8.saturating_sub(1);` |