Skip to content

Commit 9fe8a3e

Browse files
Support array indexing expressions in unused write to a constant
1 parent 847898f commit 9fe8a3e

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![warn(rust_2018_idioms, trivial_casts, trivial_numeric_casts)]
2121
#![feature(crate_visibility_modifier)]
2222
#![feature(try_from)]
23+
#![feature(if_while_or_patterns)]
2324

2425
// FIXME: switch to something more ergonomic here, once available.
2526
// (currently there is no way to opt into sysroot crates w/o `extern crate`)

clippy_lints/src/no_effect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
101101
ExprKind::Assign(ref left, ref right) => {
102102
if has_no_effect(cx, left) {
103103
let mut left = left;
104-
while let ExprKind::Field(f, _) = &left.node {
104+
while let ExprKind::Field(f, _) | ExprKind::Index(f, _) = &left.node {
105105
left = f;
106106
}
107107
if let ExprKind::Path(qpath) = &left.node {

tests/ui/no_effect.rs

+7
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ struct B {
7474
struct C {
7575
b: B,
7676
}
77+
struct D {
78+
arr: [i32; 1],
79+
}
7780
const A_CONST: A = A(1);
7881
const B: B = B { field: 1 };
7982
const C: C = C { b: B { field: 1 } };
83+
const D: D = D { arr: [1] };
8084

8185
fn main() {
8286
let s = get_struct();
@@ -113,6 +117,7 @@ fn main() {
113117
A_CONST.0 = 2;
114118
B.field = 2;
115119
C.b.field = 2;
120+
D.arr[0] = 2;
116121

117122
// Do not warn
118123
get_number();
@@ -128,4 +133,6 @@ fn main() {
128133
b_mut.field = 2;
129134
let mut c_mut = C { b: B { field: 1 } };
130135
c_mut.b.field = 2;
136+
let mut d_mut = D { arr: [1] };
137+
d_mut.arr[0] = 2;
131138
}

tests/ui/no_effect.stderr

+35-29
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,178 @@
11
error: statement with no effect
2-
--> $DIR/no_effect.rs:85:5
2+
--> $DIR/no_effect.rs:89:5
33
|
44
LL | 0;
55
| ^^
66
|
77
= note: `-D clippy::no-effect` implied by `-D warnings`
88

99
error: statement with no effect
10-
--> $DIR/no_effect.rs:86:5
10+
--> $DIR/no_effect.rs:90:5
1111
|
1212
LL | s2;
1313
| ^^^
1414

1515
error: statement with no effect
16-
--> $DIR/no_effect.rs:87:5
16+
--> $DIR/no_effect.rs:91:5
1717
|
1818
LL | Unit;
1919
| ^^^^^
2020

2121
error: statement with no effect
22-
--> $DIR/no_effect.rs:88:5
22+
--> $DIR/no_effect.rs:92:5
2323
|
2424
LL | Tuple(0);
2525
| ^^^^^^^^^
2626

2727
error: statement with no effect
28-
--> $DIR/no_effect.rs:89:5
28+
--> $DIR/no_effect.rs:93:5
2929
|
3030
LL | Struct { field: 0 };
3131
| ^^^^^^^^^^^^^^^^^^^^
3232

3333
error: statement with no effect
34-
--> $DIR/no_effect.rs:90:5
34+
--> $DIR/no_effect.rs:94:5
3535
|
3636
LL | Struct { ..s };
3737
| ^^^^^^^^^^^^^^^
3838

3939
error: statement with no effect
40-
--> $DIR/no_effect.rs:91:5
40+
--> $DIR/no_effect.rs:95:5
4141
|
4242
LL | Union { a: 0 };
4343
| ^^^^^^^^^^^^^^^
4444

4545
error: statement with no effect
46-
--> $DIR/no_effect.rs:92:5
46+
--> $DIR/no_effect.rs:96:5
4747
|
4848
LL | Enum::Tuple(0);
4949
| ^^^^^^^^^^^^^^^
5050

5151
error: statement with no effect
52-
--> $DIR/no_effect.rs:93:5
52+
--> $DIR/no_effect.rs:97:5
5353
|
5454
LL | Enum::Struct { field: 0 };
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: statement with no effect
58-
--> $DIR/no_effect.rs:94:5
58+
--> $DIR/no_effect.rs:98:5
5959
|
6060
LL | 5 + 6;
6161
| ^^^^^^
6262

6363
error: statement with no effect
64-
--> $DIR/no_effect.rs:95:5
64+
--> $DIR/no_effect.rs:99:5
6565
|
6666
LL | *&42;
6767
| ^^^^^
6868

6969
error: statement with no effect
70-
--> $DIR/no_effect.rs:96:5
70+
--> $DIR/no_effect.rs:100:5
7171
|
7272
LL | &6;
7373
| ^^^
7474

7575
error: statement with no effect
76-
--> $DIR/no_effect.rs:97:5
76+
--> $DIR/no_effect.rs:101:5
7777
|
7878
LL | (5, 6, 7);
7979
| ^^^^^^^^^^
8080

8181
error: statement with no effect
82-
--> $DIR/no_effect.rs:98:5
82+
--> $DIR/no_effect.rs:102:5
8383
|
8484
LL | box 42;
8585
| ^^^^^^^
8686

8787
error: statement with no effect
88-
--> $DIR/no_effect.rs:99:5
88+
--> $DIR/no_effect.rs:103:5
8989
|
9090
LL | ..;
9191
| ^^^
9292

9393
error: statement with no effect
94-
--> $DIR/no_effect.rs:100:5
94+
--> $DIR/no_effect.rs:104:5
9595
|
9696
LL | 5..;
9797
| ^^^^
9898

9999
error: statement with no effect
100-
--> $DIR/no_effect.rs:101:5
100+
--> $DIR/no_effect.rs:105:5
101101
|
102102
LL | ..5;
103103
| ^^^^
104104

105105
error: statement with no effect
106-
--> $DIR/no_effect.rs:102:5
106+
--> $DIR/no_effect.rs:106:5
107107
|
108108
LL | 5..6;
109109
| ^^^^^
110110

111111
error: statement with no effect
112-
--> $DIR/no_effect.rs:104:5
112+
--> $DIR/no_effect.rs:108:5
113113
|
114114
LL | [42, 55];
115115
| ^^^^^^^^^
116116

117117
error: statement with no effect
118-
--> $DIR/no_effect.rs:105:5
118+
--> $DIR/no_effect.rs:109:5
119119
|
120120
LL | [42, 55][1];
121121
| ^^^^^^^^^^^^
122122

123123
error: statement with no effect
124-
--> $DIR/no_effect.rs:106:5
124+
--> $DIR/no_effect.rs:110:5
125125
|
126126
LL | (42, 55).1;
127127
| ^^^^^^^^^^^
128128

129129
error: statement with no effect
130-
--> $DIR/no_effect.rs:107:5
130+
--> $DIR/no_effect.rs:111:5
131131
|
132132
LL | [42; 55];
133133
| ^^^^^^^^^
134134

135135
error: statement with no effect
136-
--> $DIR/no_effect.rs:108:5
136+
--> $DIR/no_effect.rs:112:5
137137
|
138138
LL | [42; 55][13];
139139
| ^^^^^^^^^^^^^
140140

141141
error: statement with no effect
142-
--> $DIR/no_effect.rs:110:5
142+
--> $DIR/no_effect.rs:114:5
143143
|
144144
LL | || x += 5;
145145
| ^^^^^^^^^^
146146

147147
error: statement with no effect
148-
--> $DIR/no_effect.rs:112:5
148+
--> $DIR/no_effect.rs:116:5
149149
|
150150
LL | FooString { s: s };
151151
| ^^^^^^^^^^^^^^^^^^^
152152

153153
error: statement with no effect
154-
--> $DIR/no_effect.rs:113:5
154+
--> $DIR/no_effect.rs:117:5
155155
|
156156
LL | A_CONST.0 = 2;
157157
| ^^^^^^^^^^^^^^
158158

159159
error: statement with no effect
160-
--> $DIR/no_effect.rs:114:5
160+
--> $DIR/no_effect.rs:118:5
161161
|
162162
LL | B.field = 2;
163163
| ^^^^^^^^^^^^
164164

165165
error: statement with no effect
166-
--> $DIR/no_effect.rs:115:5
166+
--> $DIR/no_effect.rs:119:5
167167
|
168168
LL | C.b.field = 2;
169169
| ^^^^^^^^^^^^^^
170170

171-
error: aborting due to 28 previous errors
171+
error: statement with no effect
172+
--> $DIR/no_effect.rs:120:5
173+
|
174+
LL | D.arr[0] = 2;
175+
| ^^^^^^^^^^^^^
176+
177+
error: aborting due to 29 previous errors
172178

0 commit comments

Comments
 (0)