Skip to content

Commit 5d66f83

Browse files
Mark writes to constants as side-effect-less
1 parent c937024 commit 5d66f83

File tree

4 files changed

+79
-26
lines changed

4 files changed

+79
-26
lines changed

clippy_lints/src/no_effect.rs

+12
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
9898
false
9999
}
100100
},
101+
ExprKind::Assign(ref left, ref right) => {
102+
let mut left = left;
103+
while let ExprKind::Field(f, _) = &left.node {
104+
left = f;
105+
}
106+
if let ExprKind::Path(qpath) = &left.node {
107+
if let Def::Const(..) = cx.tables.qpath_def(qpath, left.hir_id) {
108+
return has_no_effect(cx, right);
109+
}
110+
}
111+
false
112+
},
101113
_ => false,
102114
}
103115
}

tests/ui/no_effect.rs

+20
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ unsafe fn unsafe_fn() -> i32 {
6767
0
6868
}
6969

70+
struct A(i32);
71+
struct B {
72+
field: i32,
73+
}
74+
struct C {
75+
b: B,
76+
}
77+
const A_CONST: A = A(1);
78+
const B: B = B { field: 1 };
79+
const C: C = C { b: B { field: 1 } };
80+
7081
fn main() {
7182
let s = get_struct();
7283
let s2 = get_struct();
@@ -99,6 +110,9 @@ fn main() {
99110
|| x += 5;
100111
let s: String = "foo".into();
101112
FooString { s: s };
113+
A_CONST.0 = 2;
114+
B.field = 2;
115+
C.b.field = 2;
102116

103117
// Do not warn
104118
get_number();
@@ -108,4 +122,10 @@ fn main() {
108122
DropTuple(0);
109123
DropEnum::Tuple(0);
110124
DropEnum::Struct { field: 0 };
125+
let mut a_mut = A(1);
126+
a_mut.0 = 2;
127+
let mut b_mut = B { field: 1 };
128+
b_mut.field = 2;
129+
let mut c_mut = C { b: B { field: 1 } };
130+
c_mut.b.field = 2;
111131
}

tests/ui/no_effect.stderr

+44-26
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,172 @@
11
error: statement with no effect
2-
--> $DIR/no_effect.rs:74:5
2+
--> $DIR/no_effect.rs:85: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:75:5
10+
--> $DIR/no_effect.rs:86:5
1111
|
1212
LL | s2;
1313
| ^^^
1414

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

153-
error: aborting due to 25 previous errors
153+
error: statement with no effect
154+
--> $DIR/no_effect.rs:113:5
155+
|
156+
LL | A_CONST.0 = 2;
157+
| ^^^^^^^^^^^^^^
158+
159+
error: statement with no effect
160+
--> $DIR/no_effect.rs:114:5
161+
|
162+
LL | B.field = 2;
163+
| ^^^^^^^^^^^^
164+
165+
error: statement with no effect
166+
--> $DIR/no_effect.rs:115:5
167+
|
168+
LL | C.b.field = 2;
169+
| ^^^^^^^^^^^^^^
170+
171+
error: aborting due to 28 previous errors
154172

tests/ui/no_effect.stdout

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Resolved(None, path(A_CONST))
2+
Resolved(None, path(B))
3+
Resolved(None, path(C))

0 commit comments

Comments
 (0)