Skip to content

Commit 9cd43ae

Browse files
committed
Added lint for checking iXX::min_value()
1 parent c6cee03 commit 9cd43ae

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
100100
return;
101101
}
102102
},
103+
ExprKind::Call(ref func, _) => {
104+
if let ExprKind::Path(ref cond_right_path) = func.kind {
105+
if match_qpath(cond_right_path, &["i8", "min_value"]) || match_qpath(cond_right_path, &["i16", "min_value"]) || match_qpath(cond_right_path, &["i32", "min_value"]) || match_qpath(cond_right_path, &["i64", "min_value"]) {
106+
print_lint_and_sugg(cx, &var_name, expr);
107+
} else {
108+
return;
109+
}
110+
} else {
111+
return;
112+
}
113+
},
103114
_ => (),
104115
}
105116
}

tests/ui/implicit_saturating_sub.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ fn main() {
9797
i_8 -= 1;
9898
}
9999

100+
// Lint
101+
if i_8 > i8::min_value() {
102+
i_8 -= 1;
103+
}
104+
100105
let endi_16: i16 = 45;
101106
let starti_16: i16 = 44;
102107

@@ -107,6 +112,11 @@ fn main() {
107112
i_16 -= 1;
108113
}
109114

115+
// Lint
116+
if i_16 > i16::min_value() {
117+
i_16 -= 1;
118+
}
119+
110120
let endi_32: i32 = 45;
111121
let starti_32: i32 = 44;
112122

@@ -117,6 +127,11 @@ fn main() {
117127
i_32 -= 1;
118128
}
119129

130+
// Lint
131+
if i_32 > i32::min_value() {
132+
i_32 -= 1;
133+
}
134+
120135
let endi_64: i64 = 45;
121136
let starti_64: i64 = 44;
122137

@@ -126,4 +141,9 @@ fn main() {
126141
if i_64 > i64::MIN {
127142
i_64 -= 1;
128143
}
144+
145+
// Lint
146+
if i_64 > i64::min_value() {
147+
i_64 -= 1;
148+
}
129149
}

tests/ui/implicit_saturating_sub.stderr

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ LL | | }
5757
| |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
5858

5959
error: Implicitly performing saturating subtraction
60-
--> $DIR/implicit_saturating_sub.rs:106:5
60+
--> $DIR/implicit_saturating_sub.rs:101:5
61+
|
62+
LL | / if i_8 > i8::min_value() {
63+
LL | | i_8 -= 1;
64+
LL | | }
65+
| |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
66+
67+
error: Implicitly performing saturating subtraction
68+
--> $DIR/implicit_saturating_sub.rs:111:5
6169
|
6270
LL | / if i_16 > i16::MIN {
6371
LL | | i_16 -= 1;
@@ -67,18 +75,42 @@ LL | | }
6775
error: Implicitly performing saturating subtraction
6876
--> $DIR/implicit_saturating_sub.rs:116:5
6977
|
78+
LL | / if i_16 > i16::min_value() {
79+
LL | | i_16 -= 1;
80+
LL | | }
81+
| |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
82+
83+
error: Implicitly performing saturating subtraction
84+
--> $DIR/implicit_saturating_sub.rs:126:5
85+
|
7086
LL | / if i_32 > i32::MIN {
7187
LL | | i_32 -= 1;
7288
LL | | }
7389
| |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
7490

7591
error: Implicitly performing saturating subtraction
76-
--> $DIR/implicit_saturating_sub.rs:126:5
92+
--> $DIR/implicit_saturating_sub.rs:131:5
93+
|
94+
LL | / if i_32 > i32::min_value() {
95+
LL | | i_32 -= 1;
96+
LL | | }
97+
| |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
98+
99+
error: Implicitly performing saturating subtraction
100+
--> $DIR/implicit_saturating_sub.rs:141:5
77101
|
78102
LL | / if i_64 > i64::MIN {
79103
LL | | i_64 -= 1;
80104
LL | | }
81105
| |_____^ help: try: `i_64 = i_64.saturating_sub(1);`
82106

83-
error: aborting due to 10 previous errors
107+
error: Implicitly performing saturating subtraction
108+
--> $DIR/implicit_saturating_sub.rs:146:5
109+
|
110+
LL | / if i_64 > i64::min_value() {
111+
LL | | i_64 -= 1;
112+
LL | | }
113+
| |_____^ help: try: `i_64 = i_64.saturating_sub(1);`
114+
115+
error: aborting due to 14 previous errors
84116

0 commit comments

Comments
 (0)