Skip to content

Commit 9a99979

Browse files
committed
fix #887: New lints for integer/floating-point arithmetic
1 parent 5150088 commit 9a99979

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ All notable changes to this project will be documented in this file.
107107
[`explicit_iter_loop`]: https://github.com/Manishearth/rust-clippy/wiki#explicit_iter_loop
108108
[`extend_from_slice`]: https://github.com/Manishearth/rust-clippy/wiki#extend_from_slice
109109
[`filter_next`]: https://github.com/Manishearth/rust-clippy/wiki#filter_next
110+
[`float_arithmetic`]: https://github.com/Manishearth/rust-clippy/wiki#float_arithmetic
110111
[`float_cmp`]: https://github.com/Manishearth/rust-clippy/wiki#float_cmp
111112
[`for_kv_map`]: https://github.com/Manishearth/rust-clippy/wiki#for_kv_map
112113
[`for_loop_over_option`]: https://github.com/Manishearth/rust-clippy/wiki#for_loop_over_option
@@ -118,6 +119,7 @@ All notable changes to this project will be documented in this file.
118119
[`indexing_slicing`]: https://github.com/Manishearth/rust-clippy/wiki#indexing_slicing
119120
[`ineffective_bit_mask`]: https://github.com/Manishearth/rust-clippy/wiki#ineffective_bit_mask
120121
[`inline_always`]: https://github.com/Manishearth/rust-clippy/wiki#inline_always
122+
[`integer_arithmetic`]: https://github.com/Manishearth/rust-clippy/wiki#integer_arithmetic
121123
[`invalid_regex`]: https://github.com/Manishearth/rust-clippy/wiki#invalid_regex
122124
[`invalid_upcast_comparisons`]: https://github.com/Manishearth/rust-clippy/wiki#invalid_upcast_comparisons
123125
[`items_after_statements`]: https://github.com/Manishearth/rust-clippy/wiki#items_after_statements

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Table of contents:
1414
* [License](#license)
1515

1616
##Lints
17-
There are 144 lints included in this crate:
17+
There are 146 lints included in this crate:
1818

1919
name | default | meaning
2020
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -56,6 +56,7 @@ name
5656
[explicit_iter_loop](https://github.com/Manishearth/rust-clippy/wiki#explicit_iter_loop) | warn | for-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do
5757
[extend_from_slice](https://github.com/Manishearth/rust-clippy/wiki#extend_from_slice) | warn | `.extend_from_slice(_)` is a faster way to extend a Vec by a slice
5858
[filter_next](https://github.com/Manishearth/rust-clippy/wiki#filter_next) | warn | using `filter(p).next()`, which is more succinctly expressed as `.find(p)`
59+
[float_arithmetic](https://github.com/Manishearth/rust-clippy/wiki#float_arithmetic) | allow | Any floating-point arithmetic statement
5960
[float_cmp](https://github.com/Manishearth/rust-clippy/wiki#float_cmp) | warn | using `==` or `!=` on float values (as floating-point operations usually involve rounding errors, it is always better to check for approximate equality within small bounds)
6061
[for_kv_map](https://github.com/Manishearth/rust-clippy/wiki#for_kv_map) | warn | looping on a map using `iter` when `keys` or `values` would do
6162
[for_loop_over_option](https://github.com/Manishearth/rust-clippy/wiki#for_loop_over_option) | warn | for-looping over an `Option`, which is more clearly expressed as an `if let`
@@ -67,6 +68,7 @@ name
6768
[indexing_slicing](https://github.com/Manishearth/rust-clippy/wiki#indexing_slicing) | allow | indexing/slicing usage
6869
[ineffective_bit_mask](https://github.com/Manishearth/rust-clippy/wiki#ineffective_bit_mask) | warn | expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`
6970
[inline_always](https://github.com/Manishearth/rust-clippy/wiki#inline_always) | warn | `#[inline(always)]` is a bad idea in most cases
71+
[integer_arithmetic](https://github.com/Manishearth/rust-clippy/wiki#integer_arithmetic) | allow | Any integer arithmetic statement
7072
[invalid_regex](https://github.com/Manishearth/rust-clippy/wiki#invalid_regex) | deny | finds invalid regular expressions in `Regex::new(_)` invocations
7173
[invalid_upcast_comparisons](https://github.com/Manishearth/rust-clippy/wiki#invalid_upcast_comparisons) | warn | a comparison involving an upcast which is always true or false
7274
[items_after_statements](https://github.com/Manishearth/rust-clippy/wiki#items_after_statements) | allow | finds blocks where an item comes after a statement

src/arithmetic.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use rustc::hir;
2+
use rustc::lint::*;
3+
use syntax::codemap::Span;
4+
use utils::span_lint;
5+
6+
/// **What it does:** This lint checks for plain integer arithmetic
7+
///
8+
/// **Why is this bad?** This is only checked against overflow in debug builds.
9+
/// In some applications one wants explicitly checked, wrapping or saturating
10+
/// arithmetic.
11+
///
12+
/// **Known problems:** None
13+
///
14+
/// **Example:**
15+
/// ```
16+
/// a + 1
17+
/// ```
18+
declare_lint! {
19+
pub INTEGER_ARITHMETIC,
20+
Allow,
21+
"Any integer arithmetic statement"
22+
}
23+
24+
/// **What it does:** This lint checks for float arithmetic
25+
///
26+
/// **Why is this bad?** For some embedded systems or kernel development, it
27+
/// can be useful to rule out floating-point numbers
28+
///
29+
/// **Known problems:** None
30+
///
31+
/// **Example:**
32+
/// ```
33+
/// a + 1.0
34+
/// ```
35+
declare_lint! {
36+
pub FLOAT_ARITHMETIC,
37+
Allow,
38+
"Any floating-point arithmetic statement"
39+
}
40+
41+
#[derive(Copy, Clone, Default)]
42+
pub struct Arithmetic {
43+
span: Option<Span>
44+
}
45+
46+
impl LintPass for Arithmetic {
47+
fn get_lints(&self) -> LintArray {
48+
lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
49+
}
50+
}
51+
52+
impl LateLintPass for Arithmetic {
53+
fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) {
54+
if let Some(_) = self.span { return; }
55+
match expr.node {
56+
hir::ExprBinary(ref op, ref l, ref r) => {
57+
match op.node {
58+
hir::BiRem | hir::BiAnd | hir::BiOr | hir::BiBitAnd |
59+
hir::BiBitOr | hir::BiBitXor | hir::BiShl | hir::BiShr |
60+
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe |
61+
hir::BiGt => return,
62+
_ => ()
63+
}
64+
let (l_ty, r_ty) = (cx.tcx.expr_ty(l), cx.tcx.expr_ty(r));
65+
if l_ty.is_integral() && r_ty.is_integral() {
66+
span_lint(cx,
67+
INTEGER_ARITHMETIC,
68+
expr.span,
69+
"integer arithmetic detected");
70+
self.span = Some(expr.span);
71+
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {
72+
span_lint(cx,
73+
FLOAT_ARITHMETIC,
74+
expr.span,
75+
"floating-point arithmetic detected");
76+
self.span = Some(expr.span);
77+
}
78+
},
79+
hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
80+
let ty = cx.tcx.expr_ty(arg);
81+
if ty.is_integral() {
82+
span_lint(cx,
83+
INTEGER_ARITHMETIC,
84+
expr.span,
85+
"integer arithmetic detected");
86+
self.span = Some(expr.span);
87+
} else if ty.is_floating_point() {
88+
span_lint(cx,
89+
FLOAT_ARITHMETIC,
90+
expr.span,
91+
"floating-point arithmetic detected");
92+
self.span = Some(expr.span);
93+
}
94+
},
95+
_ => ()
96+
}
97+
}
98+
99+
fn check_expr_post(&mut self, _: &LateContext, expr: &hir::Expr) {
100+
if Some(expr.span) == self.span {
101+
self.span = None;
102+
}
103+
}
104+
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(question_mark)]
88
#![feature(stmt_expr_attributes)]
99
#![allow(indexing_slicing, shadow_reuse, unknown_lints)]
10+
#![allow(float_arithmetic, integer_arithmetic)]
1011

1112
// this only exists to allow the "dogfood" integration test to work
1213
#[allow(dead_code)]
@@ -49,6 +50,7 @@ pub mod utils;
4950

5051
// begin lints modules, do not remove this comment, it’s used in `update_lints`
5152
pub mod approx_const;
53+
pub mod arithmetic;
5254
pub mod array_indexing;
5355
pub mod attrs;
5456
pub mod bit_mask;
@@ -238,8 +240,11 @@ pub fn plugin_registrar(reg: &mut Registry) {
238240
reg.register_late_lint_pass(box neg_multiply::NegMultiply);
239241
reg.register_late_lint_pass(box unsafe_removed_from_name::UnsafeNameRemoval);
240242
reg.register_late_lint_pass(box mem_forget::MemForget);
243+
reg.register_late_lint_pass(box arithmetic::Arithmetic::default());
241244

242245
reg.register_lint_group("clippy_pedantic", vec![
246+
arithmetic::FLOAT_ARITHMETIC,
247+
arithmetic::INTEGER_ARITHMETIC,
243248
array_indexing::INDEXING_SLICING,
244249
booleans::NONMINIMAL_BOOL,
245250
enum_glob_use::ENUM_GLOB_USE,

tests/compile-fail/arithmetic.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
#![deny(integer_arithmetic, float_arithmetic)]
5+
#![allow(unused, shadow_reuse, shadow_unrelated, no_effect)]
6+
fn main() {
7+
let i = 1i32;
8+
1 + i; //~ERROR integer arithmetic detected
9+
i * 2; //~ERROR integer arithmetic detected
10+
1 % //~ERROR integer arithmetic detected
11+
i / 2;
12+
i - 2 + 2 - i; //~ERROR integer arithmetic detected
13+
-i; //~ERROR integer arithmetic detected
14+
15+
i & 1; // no wrapping
16+
i | 1;
17+
i ^ 1;
18+
i % 7;
19+
i >> 1;
20+
i << 1;
21+
22+
let f = 1.0f32;
23+
24+
f * 2.0; //~ERROR floating-point arithmetic detected
25+
26+
1.0 + f; //~ERROR floating-point arithmetic detected
27+
f * 2.0; //~ERROR floating-point arithmetic detected
28+
f / 2.0; //~ERROR floating-point arithmetic detected
29+
f - 2.0 * 4.2; //~ERROR floating-point arithmetic detected
30+
-f; //~ERROR floating-point arithmetic detected
31+
}

0 commit comments

Comments
 (0)