Skip to content

Commit eed9ec1

Browse files
committed
improve the no_effect lint
1 parent 1013026 commit eed9ec1

14 files changed

+67
-15
lines changed

src/no_effect.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
22
use rustc::middle::def::Def;
3-
use rustc_front::hir::{Expr, ExprCall, ExprLit, ExprPath, ExprStruct};
3+
use rustc_front::hir::{Expr, Expr_};
44
use rustc_front::hir::{Stmt, StmtSemi};
55

66
use utils::in_macro;
@@ -24,16 +24,33 @@ fn has_no_effect(cx: &LateContext, expr: &Expr) -> bool {
2424
return false;
2525
}
2626
match expr.node {
27-
ExprLit(..) |
28-
ExprPath(..) => true,
29-
ExprStruct(_, ref fields, ref base) => {
27+
Expr_::ExprLit(..) |
28+
Expr_::ExprClosure(..) |
29+
Expr_::ExprRange(None, None) |
30+
Expr_::ExprPath(..) => true,
31+
Expr_::ExprIndex(ref a, ref b) |
32+
Expr_::ExprRange(Some(ref a), Some(ref b)) |
33+
Expr_::ExprBinary(_, ref a, ref b) => has_no_effect(cx, a) && has_no_effect(cx, b),
34+
Expr_::ExprVec(ref v) |
35+
Expr_::ExprTup(ref v) => v.iter().all(|val| has_no_effect(cx, val)),
36+
Expr_::ExprRange(Some(ref inner), None) |
37+
Expr_::ExprRange(None, Some(ref inner)) |
38+
Expr_::ExprRepeat(ref inner, _) |
39+
Expr_::ExprCast(ref inner, _) |
40+
Expr_::ExprType(ref inner, _) |
41+
Expr_::ExprUnary(_, ref inner) |
42+
Expr_::ExprField(ref inner, _) |
43+
Expr_::ExprTupField(ref inner, _) |
44+
Expr_::ExprAddrOf(_, ref inner) |
45+
Expr_::ExprBox(ref inner) => has_no_effect(cx, inner),
46+
Expr_::ExprStruct(_, ref fields, ref base) => {
3047
fields.iter().all(|field| has_no_effect(cx, &field.expr)) &&
3148
match *base {
3249
Some(ref base) => has_no_effect(cx, base),
3350
None => true,
3451
}
3552
}
36-
ExprCall(ref callee, ref args) => {
53+
Expr_::ExprCall(ref callee, ref args) => {
3754
let def = cx.tcx.def_map.borrow().get(&callee.id).map(|d| d.full_def());
3855
match def {
3956
Some(Def::Struct(..)) |

tests/compile-fail/absurd-extreme-comparisons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![plugin(clippy)]
33

44
#![deny(absurd_extreme_comparisons)]
5-
#![allow(unused, eq_op)]
5+
#![allow(unused, eq_op, no_effect)]
66
fn main() {
77
const Z: u32 = 0;
88

tests/compile-fail/array_indexing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![plugin(clippy)]
33

44
#![deny(out_of_bounds_indexing)]
5+
#![allow(no_effect)]
56

67
fn main() {
78
let x = [1,2,3,4];

tests/compile-fail/bit_masks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const THREE_BITS : i64 = 7;
55
const EVEN_MORE_REDIRECTION : i64 = THREE_BITS;
66

77
#[deny(bad_bit_mask)]
8-
#[allow(ineffective_bit_mask, identity_op)]
8+
#[allow(ineffective_bit_mask, identity_op, no_effect)]
99
fn main() {
1010
let x = 5;
1111

@@ -45,7 +45,7 @@ fn main() {
4545
}
4646

4747
#[deny(ineffective_bit_mask)]
48-
#[allow(bad_bit_mask)]
48+
#[allow(bad_bit_mask, no_effect)]
4949
fn ineffective() {
5050
let x = 5;
5151

tests/compile-fail/cast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![plugin(clippy)]
33

44
#[deny(cast_precision_loss, cast_possible_truncation, cast_sign_loss, cast_possible_wrap)]
5+
#[allow(no_effect)]
56
fn main() {
67
// Test cast_precision_loss
78
1i32 as f32; //~ERROR casting i32 to f32 causes a loss of precision (i32 is 32 bits wide, but f32's mantissa is only 23 bits wide)

tests/compile-fail/cmp_nan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![plugin(clippy)]
33

44
#[deny(cmp_nan)]
5-
#[allow(float_cmp)]
5+
#[allow(float_cmp, no_effect)]
66
fn main() {
77
let x = 5f32;
88
x == std::f32::NAN; //~ERROR doomed comparison with NAN

tests/compile-fail/copies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(plugin)]
22
#![plugin(clippy)]
33

4-
#![allow(dead_code)]
4+
#![allow(dead_code, no_effect)]
55
#![allow(let_and_return)]
66
#![allow(needless_return)]
77
#![allow(unused_variables)]

tests/compile-fail/eq_op.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#[deny(eq_op)]
55
#[allow(identity_op)]
6+
#[allow(no_effect)]
67
fn main() {
78
// simple values and comparisons
89
1 == 1; //~ERROR equal expressions

tests/compile-fail/eta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(plugin)]
22
#![plugin(clippy)]
3-
#![allow(unknown_lints, unused)]
3+
#![allow(unknown_lints, unused, no_effect)]
44
#![deny(redundant_closure)]
55

66
fn main() {

tests/compile-fail/float_cmp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![plugin(clippy)]
33

44
#![deny(float_cmp)]
5-
#![allow(unused)]
5+
#![allow(unused, no_effect)]
66

77
use std::ops::Add;
88

tests/compile-fail/identity_op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const ONE : i64 = 1;
55
const NEG_ONE : i64 = -1;
66
const ZERO : i64 = 0;
77

8-
#[allow(eq_op)]
8+
#[allow(eq_op, no_effect)]
99
#[deny(identity_op)]
1010
fn main() {
1111
let x = 0;

tests/compile-fail/modulo_one.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(plugin)]
22
#![plugin(clippy)]
33
#![deny(modulo_one)]
4+
#![allow(no_effect)]
45

56
fn main() {
67
10 % 1; //~ERROR any number modulo 1 will be 0

tests/compile-fail/mut_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(plugin)]
22
#![plugin(clippy)]
33

4-
#![allow(unused)]
4+
#![allow(unused, no_effect)]
55

66
//#![plugin(regex_macros)]
77
//extern crate regex;

tests/compile-fail/no_effect.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(plugin)]
1+
#![feature(plugin, box_syntax)]
22
#![plugin(clippy)]
33

44
#![deny(no_effect)]
@@ -20,14 +20,32 @@ fn get_struct() -> Struct { Struct { field: 0 } }
2020

2121
fn main() {
2222
let s = get_struct();
23+
let s2 = get_struct();
2324

2425
0; //~ERROR statement with no effect
26+
s2; //~ERROR statement with no effect
2527
Unit; //~ERROR statement with no effect
2628
Tuple(0); //~ERROR statement with no effect
2729
Struct { field: 0 }; //~ERROR statement with no effect
2830
Struct { ..s }; //~ERROR statement with no effect
2931
Enum::Tuple(0); //~ERROR statement with no effect
3032
Enum::Struct { field: 0 }; //~ERROR statement with no effect
33+
5 + 6; //~ERROR statement with no effect
34+
*&42; //~ERROR statement with no effect
35+
&6; //~ERROR statement with no effect
36+
(5, 6, 7); //~ERROR statement with no effect
37+
box 42; //~ERROR statement with no effect
38+
..; //~ERROR statement with no effect
39+
5..; //~ERROR statement with no effect
40+
..5; //~ERROR statement with no effect
41+
5..6; //~ERROR statement with no effect
42+
[42, 55]; //~ERROR statement with no effect
43+
[42, 55][1]; //~ERROR statement with no effect
44+
(42, 55).1; //~ERROR statement with no effect
45+
[42; 55]; //~ERROR statement with no effect
46+
[42; 55][13]; //~ERROR statement with no effect
47+
let mut x = 0;
48+
|| x += 5; //~ERROR statement with no effect
3149

3250
// Do not warn
3351
get_number();
@@ -36,4 +54,17 @@ fn main() {
3654
Struct { ..get_struct() };
3755
Enum::Tuple(get_number());
3856
Enum::Struct { field: get_number() };
57+
5 + get_number();
58+
*&get_number();
59+
&get_number();
60+
(5, 6, get_number());
61+
box get_number();
62+
get_number()..;
63+
..get_number();
64+
5..get_number();
65+
[42, get_number()];
66+
[42, 55][get_number() as usize];
67+
(42, get_number()).1;
68+
[get_number(); 55];
69+
[42; 55][get_number() as usize];
3970
}

0 commit comments

Comments
 (0)