Skip to content

Commit 033c99b

Browse files
author
Yury Krivopalov
committed
Add erasing_op lint
For expressions that can be replaced by a zero.
1 parent f68e408 commit 033c99b

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

clippy_lints/src/erasing_op.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use consts::{constant_simple, Constant};
2+
use rustc::hir::*;
3+
use rustc::lint::*;
4+
use syntax::codemap::Span;
5+
use utils::{in_macro, span_lint};
6+
7+
/// **What it does:** Checks for erasing operations, e.g. `x * 0`.
8+
///
9+
/// **Why is this bad?** The whole expression can be replaced by zero.
10+
/// Most likely mistake was made and code should be reviewed or simplified.
11+
///
12+
/// **Known problems:** None.
13+
///
14+
/// **Example:**
15+
/// ```rust
16+
/// 0 / x; 0 * x; x & 0
17+
/// ```
18+
declare_lint! {
19+
pub ERASING_OP,
20+
Warn,
21+
"using erasing operations, e.g. `x * 0` or `y & 0`"
22+
}
23+
24+
#[derive(Copy, Clone)]
25+
pub struct ErasingOp;
26+
27+
impl LintPass for ErasingOp {
28+
fn get_lints(&self) -> LintArray {
29+
lint_array!(ERASING_OP)
30+
}
31+
}
32+
33+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
34+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
35+
if in_macro(e.span) {
36+
return;
37+
}
38+
if let ExprBinary(ref cmp, ref left, ref right) = e.node {
39+
match cmp.node {
40+
BiMul | BiBitAnd => {
41+
check(cx, left, e.span);
42+
check(cx, right, e.span);
43+
},
44+
BiDiv => check(cx, left, e.span),
45+
_ => (),
46+
}
47+
}
48+
}
49+
}
50+
51+
fn check(cx: &LateContext, e: &Expr, span: Span) {
52+
if let Some(Constant::Int(v)) = constant_simple(cx, e) {
53+
if v.to_u128_unchecked() == 0 {
54+
span_lint(
55+
cx,
56+
ERASING_OP,
57+
span,
58+
"the operation is ineffective. Consider reducing it to `0`",
59+
);
60+
}
61+
}
62+
}

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub mod entry;
8686
pub mod enum_clike;
8787
pub mod enum_glob_use;
8888
pub mod enum_variants;
89+
pub mod erasing_op;
8990
pub mod eq_op;
9091
pub mod escape;
9192
pub mod eta_reduction;
@@ -246,6 +247,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
246247
reg.register_early_lint_pass(box needless_continue::NeedlessContinue);
247248
reg.register_late_lint_pass(box eta_reduction::EtaPass);
248249
reg.register_late_lint_pass(box identity_op::IdentityOp);
250+
reg.register_late_lint_pass(box erasing_op::ErasingOp);
249251
reg.register_early_lint_pass(box items_after_statements::ItemsAfterStatements);
250252
reg.register_late_lint_pass(box mut_mut::MutMut);
251253
reg.register_late_lint_pass(box mut_reference::UnnecessaryMutPassed);

tests/ui/bit_masks.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ error: &-masking with zero
66
|
77
= note: `-D bad-bit-mask` implied by `-D warnings`
88

9+
error: the operation is ineffective. Consider reducing it to `0`
10+
--> $DIR/bit_masks.rs:12:5
11+
|
12+
12 | x & 0 == 0;
13+
| ^^^^^
14+
|
15+
= note: `-D erasing-op` implied by `-D warnings`
16+
917
error: incompatible bit mask: `_ & 2` can never be equal to `1`
1018
--> $DIR/bit_masks.rs:15:5
1119
|
@@ -48,6 +56,12 @@ error: &-masking with zero
4856
35 | 0 & x == 0;
4957
| ^^^^^^^^^^
5058

59+
error: the operation is ineffective. Consider reducing it to `0`
60+
--> $DIR/bit_masks.rs:35:5
61+
|
62+
35 | 0 & x == 0;
63+
| ^^^^^
64+
5165
error: incompatible bit mask: `_ | 2` will always be higher than `1`
5266
--> $DIR/bit_masks.rs:39:5
5367
|

tests/ui/erasing_op.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
4+
#[allow(no_effect)]
5+
#[warn(erasing_op)]
6+
fn main() {
7+
let x: u8 = 0;
8+
9+
x * 0;
10+
0 & x;
11+
0 / x;
12+
}

tests/ui/erasing_op.stderr

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: the operation is ineffective. Consider reducing it to `0`
2+
--> $DIR/erasing_op.rs:9:5
3+
|
4+
9 | x * 0;
5+
| ^^^^^
6+
|
7+
= note: `-D erasing-op` implied by `-D warnings`
8+
9+
error: the operation is ineffective. Consider reducing it to `0`
10+
--> $DIR/erasing_op.rs:10:5
11+
|
12+
10 | 0 & x;
13+
| ^^^^^
14+
15+
error: the operation is ineffective. Consider reducing it to `0`
16+
--> $DIR/erasing_op.rs:11:5
17+
|
18+
11 | 0 / x;
19+
| ^^^^^
20+

0 commit comments

Comments
 (0)