Skip to content

Commit

Permalink
Made all requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pmk21 committed Apr 16, 2020
1 parent 6290d9d commit 14cd457
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 33 deletions.
32 changes: 22 additions & 10 deletions clippy_lints/src/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {

// 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 subtracts_one(e);
if let ExprKind::AssignOp(_, ref target, _) = e.kind;
if let ExprKind::Path(ref assign_path) = target.kind;

// Extracting out the variable name
if let QPath::Resolved(_, ref ares_path) = assign_path;

// Get the literal being subtracted
if let ExprKind::Lit(ref lit1) = value.kind;
if let LitKind::Int(1, _) = lit1.node;

then {
// Handle symmetric conditions in the if statement
let (cond_var, cond_num_val) = if SpanlessEq::new(cx).eq_expr(cond_left, target) {
Expand All @@ -99,6 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {

// Get the variable name
let var_name = ares_path.segments[0].ident.name.as_str();
const INT_TYPES: [&str; 5] = ["i8", "i16", "i32", "i64", "i128"];

match cond_num_val.kind {
ExprKind::Lit(ref cond_lit) => {
Expand All @@ -114,15 +111,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
}
},
ExprKind::Path(ref cond_num_path) => {
if match_qpath(cond_num_path, &["i8", "MIN"]) || match_qpath(cond_num_path, &["i16", "MIN"]) || match_qpath(cond_num_path, &["i32", "MIN"]) || match_qpath(cond_num_path, &["i64", "MIN"]) {
if INT_TYPES.iter().any( |int_type| match_qpath(cond_num_path, &[int_type, "MIN"])) {
print_lint_and_sugg(cx, &var_name, expr);
} else {
return;
}
},
ExprKind::Call(ref func, _) => {
if let ExprKind::Path(ref cond_num_path) = func.kind {
if match_qpath(cond_num_path, &["i8", "min_value"]) || match_qpath(cond_num_path, &["i16", "min_value"]) || match_qpath(cond_num_path, &["i32", "min_value"]) || match_qpath(cond_num_path, &["i64", "min_value"]) {
if INT_TYPES.iter().any( |int_type| match_qpath(cond_num_path, &[int_type, "min_value"])) {
print_lint_and_sugg(cx, &var_name, expr);
} else {
return;
Expand All @@ -138,8 +135,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
}
}

fn subtracts_one(expr: &Expr<'_>) -> bool {
if_chain! {
if let ExprKind::AssignOp(ref op1, _, ref value) = expr.kind;
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 {
return true;
}
}
false
}

fn print_lint_and_sugg(cx: &LateContext<'_, '_>, var_name: &str, expr: &Expr<'_>) {
let applicability = Applicability::MaybeIncorrect;
let applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
IMPLICIT_SATURATING_SUB,
Expand Down
160 changes: 160 additions & 0 deletions tests/ui/implicit_saturating_sub.fixed
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 2 additions & 0 deletions tests/ui/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// run-rustfix
#![allow(unused_assignments, unused_mut)]
#![warn(clippy::implicit_saturating_sub)]

fn main() {
Expand Down
Loading

0 comments on commit 14cd457

Please sign in to comment.