Skip to content

Commit 94ca94f

Browse files
committed
Auto merge of #7898 - F3real:unit_struct, r=camsteffen
Don't show no_effect warning on unit structs implementing fn_once Fixes #7792 changelog: Don't show [`no_effect`] or [`unecessary_operation`] warning for unit struct implementing FnOnce
2 parents 46687f1 + e9bf5ec commit 94ca94f

File tree

3 files changed

+73
-33
lines changed

3 files changed

+73
-33
lines changed

clippy_lints/src/no_effect.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
133133
},
134134
ExprKind::Call(callee, args) => {
135135
if let ExprKind::Path(ref qpath) = callee.kind {
136-
let res = cx.qpath_res(qpath, callee.hir_id);
136+
if cx.typeck_results().type_dependent_def(expr.hir_id).is_some() {
137+
// type-dependent function call like `impl FnOnce for X`
138+
return false;
139+
}
137140
let def_matched = matches!(
138-
res,
141+
cx.qpath_res(qpath, callee.hir_id),
139142
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
140143
);
141144
if def_matched || is_range_literal(expr) {
@@ -238,6 +241,10 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec
238241
},
239242
ExprKind::Call(callee, args) => {
240243
if let ExprKind::Path(ref qpath) = callee.kind {
244+
if cx.typeck_results().type_dependent_def(expr.hir_id).is_some() {
245+
// type-dependent function call like `impl FnOnce for X`
246+
return None;
247+
}
241248
let res = cx.qpath_res(qpath, callee.hir_id);
242249
match res {
243250
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)

tests/ui/no_effect.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(box_syntax)]
1+
#![feature(box_syntax, fn_traits, unboxed_closures)]
22
#![warn(clippy::no_effect_underscore_binding)]
33
#![allow(dead_code)]
44
#![allow(path_statements)]
@@ -58,6 +58,36 @@ unsafe fn unsafe_fn() -> i32 {
5858
0
5959
}
6060

61+
struct GreetStruct1;
62+
63+
impl FnOnce<(&str,)> for GreetStruct1 {
64+
type Output = ();
65+
66+
extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
67+
println!("hello {}", who);
68+
}
69+
}
70+
71+
struct GreetStruct2();
72+
73+
impl FnOnce<(&str,)> for GreetStruct2 {
74+
type Output = ();
75+
76+
extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
77+
println!("hello {}", who);
78+
}
79+
}
80+
81+
struct GreetStruct3 {}
82+
83+
impl FnOnce<(&str,)> for GreetStruct3 {
84+
type Output = ();
85+
86+
extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
87+
println!("hello {}", who);
88+
}
89+
}
90+
6191
fn main() {
6292
let s = get_struct();
6393
let s2 = get_struct();
@@ -108,4 +138,7 @@ fn main() {
108138
DropTuple(0);
109139
DropEnum::Tuple(0);
110140
DropEnum::Struct { field: 0 };
141+
GreetStruct1("world");
142+
GreetStruct2()("world");
143+
GreetStruct3 {}("world");
111144
}

tests/ui/no_effect.stderr

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,183 @@
11
error: statement with no effect
2-
--> $DIR/no_effect.rs:65:5
2+
--> $DIR/no_effect.rs:95: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:66:5
10+
--> $DIR/no_effect.rs:96:5
1111
|
1212
LL | s2;
1313
| ^^^
1414

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

111111
error: statement with no effect
112-
--> $DIR/no_effect.rs:83:5
112+
--> $DIR/no_effect.rs:113:5
113113
|
114114
LL | 5..=6;
115115
| ^^^^^^
116116

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

123123
error: statement with no effect
124-
--> $DIR/no_effect.rs:85:5
124+
--> $DIR/no_effect.rs:115:5
125125
|
126126
LL | [42, 55][1];
127127
| ^^^^^^^^^^^^
128128

129129
error: statement with no effect
130-
--> $DIR/no_effect.rs:86:5
130+
--> $DIR/no_effect.rs:116:5
131131
|
132132
LL | (42, 55).1;
133133
| ^^^^^^^^^^^
134134

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

141141
error: statement with no effect
142-
--> $DIR/no_effect.rs:88:5
142+
--> $DIR/no_effect.rs:118:5
143143
|
144144
LL | [42; 55][13];
145145
| ^^^^^^^^^^^^^
146146

147147
error: statement with no effect
148-
--> $DIR/no_effect.rs:90:5
148+
--> $DIR/no_effect.rs:120:5
149149
|
150150
LL | || x += 5;
151151
| ^^^^^^^^^^
152152

153153
error: statement with no effect
154-
--> $DIR/no_effect.rs:92:5
154+
--> $DIR/no_effect.rs:122:5
155155
|
156156
LL | FooString { s: s };
157157
| ^^^^^^^^^^^^^^^^^^^
158158

159159
error: binding to `_` prefixed variable with no side-effect
160-
--> $DIR/no_effect.rs:93:5
160+
--> $DIR/no_effect.rs:123:5
161161
|
162162
LL | let _unused = 1;
163163
| ^^^^^^^^^^^^^^^^
164164
|
165165
= note: `-D clippy::no-effect-underscore-binding` implied by `-D warnings`
166166

167167
error: binding to `_` prefixed variable with no side-effect
168-
--> $DIR/no_effect.rs:94:5
168+
--> $DIR/no_effect.rs:124:5
169169
|
170170
LL | let _penguin = || println!("Some helpful closure");
171171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
172172

173173
error: binding to `_` prefixed variable with no side-effect
174-
--> $DIR/no_effect.rs:95:5
174+
--> $DIR/no_effect.rs:125:5
175175
|
176176
LL | let _duck = Struct { field: 0 };
177177
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
178178

179179
error: binding to `_` prefixed variable with no side-effect
180-
--> $DIR/no_effect.rs:96:5
180+
--> $DIR/no_effect.rs:126:5
181181
|
182182
LL | let _cat = [2, 4, 6, 8][2];
183183
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)