Skip to content

Commit 14cd457

Browse files
committed
Made all requested changes
1 parent 6290d9d commit 14cd457

File tree

4 files changed

+207
-33
lines changed

4 files changed

+207
-33
lines changed

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
5959

6060
// Check if the true condition block has only one statement
6161
if let ExprKind::Block(ref block, _) = then.kind;
62-
if block.stmts.len() == 1;
62+
if block.stmts.len() == 1 && block.expr.is_none();
6363

6464
// Check if assign operation is done
6565
if let StmtKind::Semi(ref e) = block.stmts[0].kind;
66-
if let ExprKind::AssignOp(ref op1, ref target, ref value) = e.kind;
67-
if BinOpKind::Sub == op1.node;
66+
if subtracts_one(e);
67+
if let ExprKind::AssignOp(_, ref target, _) = e.kind;
6868
if let ExprKind::Path(ref assign_path) = target.kind;
6969

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

73-
// Get the literal being subtracted
74-
if let ExprKind::Lit(ref lit1) = value.kind;
75-
if let LitKind::Int(1, _) = lit1.node;
76-
7773
then {
7874
// Handle symmetric conditions in the if statement
7975
let (cond_var, cond_num_val) = if SpanlessEq::new(cx).eq_expr(cond_left, target) {
@@ -99,6 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
9995

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

103100
match cond_num_val.kind {
104101
ExprKind::Lit(ref cond_lit) => {
@@ -114,15 +111,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
114111
}
115112
},
116113
ExprKind::Path(ref cond_num_path) => {
117-
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"]) {
114+
if INT_TYPES.iter().any( |int_type| match_qpath(cond_num_path, &[int_type, "MIN"])) {
118115
print_lint_and_sugg(cx, &var_name, expr);
119116
} else {
120117
return;
121118
}
122119
},
123120
ExprKind::Call(ref func, _) => {
124121
if let ExprKind::Path(ref cond_num_path) = func.kind {
125-
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"]) {
122+
if INT_TYPES.iter().any( |int_type| match_qpath(cond_num_path, &[int_type, "min_value"])) {
126123
print_lint_and_sugg(cx, &var_name, expr);
127124
} else {
128125
return;
@@ -138,8 +135,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
138135
}
139136
}
140137

138+
fn subtracts_one(expr: &Expr<'_>) -> bool {
139+
if_chain! {
140+
if let ExprKind::AssignOp(ref op1, _, ref value) = expr.kind;
141+
if BinOpKind::Sub == op1.node;
142+
143+
// Check if literal being subtracted is one
144+
if let ExprKind::Lit(ref lit1) = value.kind;
145+
if let LitKind::Int(1, _) = lit1.node;
146+
then {
147+
return true;
148+
}
149+
}
150+
false
151+
}
152+
141153
fn print_lint_and_sugg(cx: &LateContext<'_, '_>, var_name: &str, expr: &Expr<'_>) {
142-
let applicability = Applicability::MaybeIncorrect;
154+
let applicability = Applicability::MachineApplicable;
143155
span_lint_and_sugg(
144156
cx,
145157
IMPLICIT_SATURATING_SUB,
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// run-rustfix
2+
#![allow(unused_assignments, unused_mut)]
3+
#![warn(clippy::implicit_saturating_sub)]
4+
5+
fn main() {
6+
// Tests for unsigned integers
7+
8+
let end_8: u8 = 10;
9+
let start_8: u8 = 5;
10+
let mut u_8: u8 = end_8 - start_8;
11+
12+
// Lint
13+
u_8 = u_8.saturating_sub(1);
14+
15+
match end_8 {
16+
10 => {
17+
// Lint
18+
u_8 = u_8.saturating_sub(1);
19+
},
20+
11 => u_8 += 1,
21+
_ => u_8 = 0,
22+
}
23+
24+
let end_16: u16 = 35;
25+
let start_16: u16 = 40;
26+
27+
let mut u_16: u16 = end_16 - start_16;
28+
29+
// Lint
30+
u_16 = u_16.saturating_sub(1);
31+
32+
let mut end_32: u32 = 7000;
33+
let mut start_32: u32 = 7010;
34+
35+
let mut u_32: u32 = end_32 - start_32;
36+
37+
// Lint
38+
u_32 = u_32.saturating_sub(1);
39+
40+
// No Lint
41+
if u_32 > 0 {
42+
u_16 += 1;
43+
}
44+
45+
// No Lint
46+
if u_32 != 0 {
47+
end_32 -= 1;
48+
start_32 += 1;
49+
}
50+
51+
let mut end_64: u64 = 75001;
52+
let mut start_64: u64 = 75000;
53+
54+
let mut u_64: u64 = end_64 - start_64;
55+
56+
// Lint
57+
u_64 = u_64.saturating_sub(1);
58+
59+
// Lint
60+
u_64 = u_64.saturating_sub(1);
61+
62+
// Lint
63+
u_64 = u_64.saturating_sub(1);
64+
65+
// No Lint
66+
if u_64 >= 1 {
67+
u_64 -= 1;
68+
}
69+
70+
// No Lint
71+
if u_64 > 0 {
72+
end_64 -= 1;
73+
}
74+
75+
// Tests for usize
76+
let end_usize: usize = 8054;
77+
let start_usize: usize = 8050;
78+
79+
let mut u_usize: usize = end_usize - start_usize;
80+
81+
// Lint
82+
u_usize = u_usize.saturating_sub(1);
83+
84+
// Tests for signed integers
85+
86+
let endi_8: i8 = 10;
87+
let starti_8: i8 = 50;
88+
89+
let mut i_8: i8 = endi_8 - starti_8;
90+
91+
// Lint
92+
i_8 = i_8.saturating_sub(1);
93+
94+
// Lint
95+
i_8 = i_8.saturating_sub(1);
96+
97+
// Lint
98+
i_8 = i_8.saturating_sub(1);
99+
100+
// Lint
101+
i_8 = i_8.saturating_sub(1);
102+
103+
let endi_16: i16 = 45;
104+
let starti_16: i16 = 44;
105+
106+
let mut i_16: i16 = endi_16 - starti_16;
107+
108+
// Lint
109+
i_16 = i_16.saturating_sub(1);
110+
111+
// Lint
112+
i_16 = i_16.saturating_sub(1);
113+
114+
// Lint
115+
i_16 = i_16.saturating_sub(1);
116+
117+
// Lint
118+
i_16 = i_16.saturating_sub(1);
119+
120+
let endi_32: i32 = 45;
121+
let starti_32: i32 = 44;
122+
123+
let mut i_32: i32 = endi_32 - starti_32;
124+
125+
// Lint
126+
i_32 = i_32.saturating_sub(1);
127+
128+
// Lint
129+
i_32 = i_32.saturating_sub(1);
130+
131+
// Lint
132+
i_32 = i_32.saturating_sub(1);
133+
134+
// Lint
135+
i_32 = i_32.saturating_sub(1);
136+
137+
let endi_64: i64 = 45;
138+
let starti_64: i64 = 44;
139+
140+
let mut i_64: i64 = endi_64 - starti_64;
141+
142+
// Lint
143+
i_64 = i_64.saturating_sub(1);
144+
145+
// Lint
146+
i_64 = i_64.saturating_sub(1);
147+
148+
// Lint
149+
i_64 = i_64.saturating_sub(1);
150+
151+
// No Lint
152+
if i_64 > 0 {
153+
i_64 -= 1;
154+
}
155+
156+
// No Lint
157+
if i_64 != 0 {
158+
i_64 -= 1;
159+
}
160+
}

tests/ui/implicit_saturating_sub.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
#![allow(unused_assignments, unused_mut)]
13
#![warn(clippy::implicit_saturating_sub)]
24

35
fn main() {

0 commit comments

Comments
 (0)