Skip to content

Commit 2771378

Browse files
authored
Merge pull request #2136 from ykrivopalov/identity_op_fixing
Identity/erasing operation lints
2 parents b96639f + 7b16f4d commit 2771378

File tree

8 files changed

+139
-6
lines changed

8 files changed

+139
-6
lines changed

clippy_lints/src/erasing_op.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/// This is most likely not the intended outcome and should probably be
11+
/// corrected
12+
///
13+
/// **Known problems:** None.
14+
///
15+
/// **Example:**
16+
/// ```rust
17+
/// 0 / x; 0 * x; x & 0
18+
/// ```
19+
declare_lint! {
20+
pub ERASING_OP,
21+
Warn,
22+
"using erasing operations, e.g. `x * 0` or `y & 0`"
23+
}
24+
25+
#[derive(Copy, Clone)]
26+
pub struct ErasingOp;
27+
28+
impl LintPass for ErasingOp {
29+
fn get_lints(&self) -> LintArray {
30+
lint_array!(ERASING_OP)
31+
}
32+
}
33+
34+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
35+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36+
if in_macro(e.span) {
37+
return;
38+
}
39+
if let ExprBinary(ref cmp, ref left, ref right) = e.node {
40+
match cmp.node {
41+
BiMul | BiBitAnd => {
42+
check(cx, left, e.span);
43+
check(cx, right, e.span);
44+
},
45+
BiDiv => check(cx, left, e.span),
46+
_ => (),
47+
}
48+
}
49+
}
50+
}
51+
52+
fn check(cx: &LateContext, e: &Expr, span: Span) {
53+
if let Some(Constant::Int(v)) = constant_simple(cx, e) {
54+
if v.to_u128_unchecked() == 0 {
55+
span_lint(
56+
cx,
57+
ERASING_OP,
58+
span,
59+
"this operation will always return zero. This is likely not the intended outcome",
60+
);
61+
}
62+
}
63+
}

clippy_lints/src/identity_op.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use consts::{constant_simple, Constant};
2-
use rustc::lint::*;
32
use rustc::hir::*;
3+
use rustc::lint::*;
4+
use rustc_const_math::ConstInt;
45
use syntax::codemap::Span;
56
use utils::{in_macro, snippet, span_lint};
6-
use syntax::attr::IntType::{SignedInt, UnsignedInt};
77

88
/// **What it does:** Checks for identity operations, e.g. `x + 0`.
99
///
@@ -58,15 +58,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
5858
}
5959
}
6060

61+
fn all_ones(v: &ConstInt) -> bool {
62+
match *v {
63+
ConstInt::I8(i) => i == !0,
64+
ConstInt::I16(i) => i == !0,
65+
ConstInt::I32(i) => i == !0,
66+
ConstInt::I64(i) => i == !0,
67+
ConstInt::I128(i) => i == !0,
68+
ConstInt::U8(i) => i == !0,
69+
ConstInt::U16(i) => i == !0,
70+
ConstInt::U32(i) => i == !0,
71+
ConstInt::U64(i) => i == !0,
72+
ConstInt::U128(i) => i == !0,
73+
_ => false
74+
}
75+
}
76+
6177
#[allow(cast_possible_wrap)]
6278
fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
6379
if let Some(Constant::Int(v)) = constant_simple(cx, e) {
6480
if match m {
6581
0 => v.to_u128_unchecked() == 0,
66-
-1 => match v.int_type() {
67-
SignedInt(_) => (v.to_u128_unchecked() as i128 == -1),
68-
UnsignedInt(_) => false,
69-
},
82+
-1 => all_ones(&v),
7083
1 => v.to_u128_unchecked() == 1,
7184
_ => unreachable!(),
7285
} {

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub mod entry;
8888
pub mod enum_clike;
8989
pub mod enum_glob_use;
9090
pub mod enum_variants;
91+
pub mod erasing_op;
9192
pub mod eq_op;
9293
pub mod escape;
9394
pub mod eta_reduction;
@@ -253,6 +254,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
253254
reg.register_early_lint_pass(box needless_continue::NeedlessContinue);
254255
reg.register_late_lint_pass(box eta_reduction::EtaPass);
255256
reg.register_late_lint_pass(box identity_op::IdentityOp);
257+
reg.register_late_lint_pass(box erasing_op::ErasingOp);
256258
reg.register_early_lint_pass(box items_after_statements::ItemsAfterStatements);
257259
reg.register_late_lint_pass(box mut_mut::MutMut);
258260
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: this operation will always return zero. This is likely not the intended outcome
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: this operation will always return zero. This is likely not the intended outcome
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: this operation will always return zero. This is likely not the intended outcome
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: this operation will always return zero. This is likely not the intended outcome
10+
--> $DIR/erasing_op.rs:10:5
11+
|
12+
10 | 0 & x;
13+
| ^^^^^
14+
15+
error: this operation will always return zero. This is likely not the intended outcome
16+
--> $DIR/erasing_op.rs:11:5
17+
|
18+
11 | 0 / x;
19+
| ^^^^^
20+

tests/ui/identity_op.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ fn main() {
2727

2828
x & NEG_ONE; //no error, as we skip lookups (for now)
2929
-1 & x;
30+
31+
let u : u8 = 0;
32+
u & 255;
3033
}

tests/ui/identity_op.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ error: the operation is ineffective. Consider reducing it to `x`
4242
29 | -1 & x;
4343
| ^^^^^^
4444

45+
error: the operation is ineffective. Consider reducing it to `u`
46+
--> $DIR/identity_op.rs:32:5
47+
|
48+
32 | u & 255;
49+
| ^^^^^^^
50+

0 commit comments

Comments
 (0)