Skip to content

Commit ee2354b

Browse files
committed
Add check for unary-operator
Fix typo and add test for unary-opeator
1 parent 2e3c031 commit ee2354b

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

clippy_lints/src/no_effect.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
9393
}
9494
let expr = peel_blocks(expr);
9595
// assume nontrivial oprand of `Binary` Expr can skip `check_unnecessary_operation`
96-
if is_operator_overrided(cx, expr) {
96+
if is_operator_overriden(cx, expr) {
9797
return true;
9898
}
9999
if has_no_effect(cx, expr) {
@@ -162,18 +162,18 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
162162
false
163163
}
164164

165-
fn is_operator_overrided(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
165+
fn is_operator_overriden(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
166166
// It's very hard or impossable to check whether overrided operator have side-effect this lint.
167-
// So, this function assume user-defined binary operator is overrided with an side-effect.
167+
// So, this function assume user-defined operator is overrided with an side-effect.
168168
// The definition of user-defined structure here is ADT-type,
169169
// Althrough this will weaken the ability of this lint, less error lint-fix happen.
170170
match expr.kind {
171-
ExprKind::Binary(..) => {
171+
ExprKind::Binary(..) | ExprKind::Unary(..) => {
172172
// No need to check type of `lhs` and `rhs`
173173
// because if the operator is overrided, at least one operand is ADT type
174174

175175
// reference: rust/compiler/rustc_middle/src/ty/typeck_results.rs: `is_method_call`.
176-
// use this function to check whether operator is overrided in `ExprKind::Binary`.
176+
// use this function to check whether operator is overrided in `ExprKind::{Binary, Unary}`.
177177
cx.typeck_results().is_method_call(expr)
178178
},
179179
_ => false,

tests/ui/no_effect.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)]
1111

1212
use std::fmt::Display;
13-
use std::ops::Shl;
13+
use std::ops::{Neg, Shl};
1414

1515
struct Cout;
1616

@@ -25,6 +25,14 @@ where
2525
}
2626
}
2727

28+
impl Neg for Cout {
29+
type Output = Self;
30+
fn neg(self) -> Self::Output {
31+
println!("hello world");
32+
self
33+
}
34+
}
35+
2836
struct Unit;
2937
struct Tuple(i32);
3038
struct Struct {
@@ -196,5 +204,5 @@ fn main() {
196204
}
197205

198206
Cout << 142;
199-
Cout << n();
207+
-Cout;
200208
}

tests/ui/no_effect.stderr

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: statement with no effect
2-
--> $DIR/no_effect.rs:114:5
2+
--> $DIR/no_effect.rs:122:5
33
|
44
LL | 0;
55
| ^^
@@ -8,151 +8,151 @@ LL | 0;
88
= help: to override `-D warnings` add `#[allow(clippy::no_effect)]`
99

1010
error: statement with no effect
11-
--> $DIR/no_effect.rs:117:5
11+
--> $DIR/no_effect.rs:125:5
1212
|
1313
LL | s2;
1414
| ^^^
1515

1616
error: statement with no effect
17-
--> $DIR/no_effect.rs:119:5
17+
--> $DIR/no_effect.rs:127:5
1818
|
1919
LL | Unit;
2020
| ^^^^^
2121

2222
error: statement with no effect
23-
--> $DIR/no_effect.rs:121:5
23+
--> $DIR/no_effect.rs:129:5
2424
|
2525
LL | Tuple(0);
2626
| ^^^^^^^^^
2727

2828
error: statement with no effect
29-
--> $DIR/no_effect.rs:123:5
29+
--> $DIR/no_effect.rs:131:5
3030
|
3131
LL | Struct { field: 0 };
3232
| ^^^^^^^^^^^^^^^^^^^^
3333

3434
error: statement with no effect
35-
--> $DIR/no_effect.rs:125:5
35+
--> $DIR/no_effect.rs:133:5
3636
|
3737
LL | Struct { ..s };
3838
| ^^^^^^^^^^^^^^^
3939

4040
error: statement with no effect
41-
--> $DIR/no_effect.rs:127:5
41+
--> $DIR/no_effect.rs:135:5
4242
|
4343
LL | Union { a: 0 };
4444
| ^^^^^^^^^^^^^^^
4545

4646
error: statement with no effect
47-
--> $DIR/no_effect.rs:129:5
47+
--> $DIR/no_effect.rs:137:5
4848
|
4949
LL | Enum::Tuple(0);
5050
| ^^^^^^^^^^^^^^^
5151

5252
error: statement with no effect
53-
--> $DIR/no_effect.rs:131:5
53+
--> $DIR/no_effect.rs:139:5
5454
|
5555
LL | Enum::Struct { field: 0 };
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5757

5858
error: statement with no effect
59-
--> $DIR/no_effect.rs:133:5
59+
--> $DIR/no_effect.rs:141:5
6060
|
6161
LL | 5 + 6;
6262
| ^^^^^^
6363

6464
error: statement with no effect
65-
--> $DIR/no_effect.rs:135:5
65+
--> $DIR/no_effect.rs:143:5
6666
|
6767
LL | *&42;
6868
| ^^^^^
6969

7070
error: statement with no effect
71-
--> $DIR/no_effect.rs:137:5
71+
--> $DIR/no_effect.rs:145:5
7272
|
7373
LL | &6;
7474
| ^^^
7575

7676
error: statement with no effect
77-
--> $DIR/no_effect.rs:139:5
77+
--> $DIR/no_effect.rs:147:5
7878
|
7979
LL | (5, 6, 7);
8080
| ^^^^^^^^^^
8181

8282
error: statement with no effect
83-
--> $DIR/no_effect.rs:141:5
83+
--> $DIR/no_effect.rs:149:5
8484
|
8585
LL | ..;
8686
| ^^^
8787

8888
error: statement with no effect
89-
--> $DIR/no_effect.rs:143:5
89+
--> $DIR/no_effect.rs:151:5
9090
|
9191
LL | 5..;
9292
| ^^^^
9393

9494
error: statement with no effect
95-
--> $DIR/no_effect.rs:145:5
95+
--> $DIR/no_effect.rs:153:5
9696
|
9797
LL | ..5;
9898
| ^^^^
9999

100100
error: statement with no effect
101-
--> $DIR/no_effect.rs:147:5
101+
--> $DIR/no_effect.rs:155:5
102102
|
103103
LL | 5..6;
104104
| ^^^^^
105105

106106
error: statement with no effect
107-
--> $DIR/no_effect.rs:149:5
107+
--> $DIR/no_effect.rs:157:5
108108
|
109109
LL | 5..=6;
110110
| ^^^^^^
111111

112112
error: statement with no effect
113-
--> $DIR/no_effect.rs:151:5
113+
--> $DIR/no_effect.rs:159:5
114114
|
115115
LL | [42, 55];
116116
| ^^^^^^^^^
117117

118118
error: statement with no effect
119-
--> $DIR/no_effect.rs:153:5
119+
--> $DIR/no_effect.rs:161:5
120120
|
121121
LL | [42, 55][1];
122122
| ^^^^^^^^^^^^
123123

124124
error: statement with no effect
125-
--> $DIR/no_effect.rs:155:5
125+
--> $DIR/no_effect.rs:163:5
126126
|
127127
LL | (42, 55).1;
128128
| ^^^^^^^^^^^
129129

130130
error: statement with no effect
131-
--> $DIR/no_effect.rs:157:5
131+
--> $DIR/no_effect.rs:165:5
132132
|
133133
LL | [42; 55];
134134
| ^^^^^^^^^
135135

136136
error: statement with no effect
137-
--> $DIR/no_effect.rs:159:5
137+
--> $DIR/no_effect.rs:167:5
138138
|
139139
LL | [42; 55][13];
140140
| ^^^^^^^^^^^^^
141141

142142
error: statement with no effect
143-
--> $DIR/no_effect.rs:162:5
143+
--> $DIR/no_effect.rs:170:5
144144
|
145145
LL | || x += 5;
146146
| ^^^^^^^^^^
147147

148148
error: statement with no effect
149-
--> $DIR/no_effect.rs:165:5
149+
--> $DIR/no_effect.rs:173:5
150150
|
151151
LL | FooString { s: s };
152152
| ^^^^^^^^^^^^^^^^^^^
153153

154154
error: binding to `_` prefixed variable with no side-effect
155-
--> $DIR/no_effect.rs:167:5
155+
--> $DIR/no_effect.rs:175:5
156156
|
157157
LL | let _unused = 1;
158158
| ^^^^^^^^^^^^^^^^
@@ -161,19 +161,19 @@ LL | let _unused = 1;
161161
= help: to override `-D warnings` add `#[allow(clippy::no_effect_underscore_binding)]`
162162

163163
error: binding to `_` prefixed variable with no side-effect
164-
--> $DIR/no_effect.rs:170:5
164+
--> $DIR/no_effect.rs:178:5
165165
|
166166
LL | let _penguin = || println!("Some helpful closure");
167167
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168168

169169
error: binding to `_` prefixed variable with no side-effect
170-
--> $DIR/no_effect.rs:172:5
170+
--> $DIR/no_effect.rs:180:5
171171
|
172172
LL | let _duck = Struct { field: 0 };
173173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
174174

175175
error: binding to `_` prefixed variable with no side-effect
176-
--> $DIR/no_effect.rs:174:5
176+
--> $DIR/no_effect.rs:182:5
177177
|
178178
LL | let _cat = [2, 4, 6, 8][2];
179179
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)