Skip to content

Commit 526fb6b

Browse files
committed
Auto merge of #8185 - dswij:8177, r=llogiq
`needless_return` suggest return unit type on void returns closes #8177 previously, `needless_return` suggests an empty block `{}` to replace void `return` on match arms, this PR improve the suggestion by suggesting a unit instead. changelog: `needless_return` suggests `()` instead of `{}` on match arms
2 parents 3c1b3ec + fc72e91 commit 526fb6b

File tree

4 files changed

+72
-26
lines changed

4 files changed

+72
-26
lines changed

clippy_lints/src/returns.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ declare_clippy_lint! {
7373
enum RetReplacement {
7474
Empty,
7575
Block,
76+
Unit,
7677
}
7778

7879
declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
@@ -212,7 +213,7 @@ fn check_final_expr<'tcx>(
212213
// (except for unit type functions) so we don't match it
213214
ExprKind::Match(_, arms, MatchSource::Normal) => {
214215
for arm in arms.iter() {
215-
check_final_expr(cx, arm.body, Some(arm.body.span), RetReplacement::Block);
216+
check_final_expr(cx, arm.body, Some(arm.body.span), RetReplacement::Unit);
216217
}
217218
},
218219
ExprKind::DropTemps(expr) => check_final_expr(cx, expr, None, RetReplacement::Empty),
@@ -259,6 +260,17 @@ fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Spa
259260
Applicability::MachineApplicable,
260261
);
261262
},
263+
RetReplacement::Unit => {
264+
span_lint_and_sugg(
265+
cx,
266+
NEEDLESS_RETURN,
267+
ret_span,
268+
"unneeded `return` statement",
269+
"replace `return` with a unit value",
270+
"()".to_string(),
271+
Applicability::MachineApplicable,
272+
);
273+
},
262274
},
263275
}
264276
}

tests/ui/needless_return.fixed

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,18 @@ fn test_void_if_fun(b: bool) {
7171
fn test_void_match(x: u32) {
7272
match x {
7373
0 => (),
74-
_ => {},
74+
_ => (),
75+
}
76+
}
77+
78+
fn test_nested_match(x: u32) {
79+
match x {
80+
0 => (),
81+
1 => {
82+
let _ = 42;
83+
84+
},
85+
_ => (),
7586
}
7687
}
7788

@@ -182,7 +193,7 @@ async fn async_test_void_if_fun(b: bool) {
182193
async fn async_test_void_match(x: u32) {
183194
match x {
184195
0 => (),
185-
_ => {},
196+
_ => (),
186197
}
187198
}
188199

tests/ui/needless_return.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ fn test_void_match(x: u32) {
7575
}
7676
}
7777

78+
fn test_nested_match(x: u32) {
79+
match x {
80+
0 => (),
81+
1 => {
82+
let _ = 42;
83+
return;
84+
},
85+
_ => return,
86+
}
87+
}
88+
7889
fn read_line() -> String {
7990
use std::io::BufRead;
8091
let stdin = ::std::io::stdin();

tests/ui/needless_return.stderr

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,127 +70,139 @@ error: unneeded `return` statement
7070
--> $DIR/needless_return.rs:74:14
7171
|
7272
LL | _ => return,
73-
| ^^^^^^ help: replace `return` with an empty block: `{}`
73+
| ^^^^^^ help: replace `return` with a unit value: `()`
7474

7575
error: unneeded `return` statement
76-
--> $DIR/needless_return.rs:89:9
76+
--> $DIR/needless_return.rs:83:13
77+
|
78+
LL | return;
79+
| ^^^^^^^ help: remove `return`
80+
81+
error: unneeded `return` statement
82+
--> $DIR/needless_return.rs:85:14
83+
|
84+
LL | _ => return,
85+
| ^^^^^^ help: replace `return` with a unit value: `()`
86+
87+
error: unneeded `return` statement
88+
--> $DIR/needless_return.rs:100:9
7789
|
7890
LL | return String::from("test");
7991
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`
8092

8193
error: unneeded `return` statement
82-
--> $DIR/needless_return.rs:91:9
94+
--> $DIR/needless_return.rs:102:9
8395
|
8496
LL | return String::new();
8597
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
8698

8799
error: unneeded `return` statement
88-
--> $DIR/needless_return.rs:113:32
100+
--> $DIR/needless_return.rs:124:32
89101
|
90102
LL | bar.unwrap_or_else(|_| return)
91103
| ^^^^^^ help: replace `return` with an empty block: `{}`
92104

93105
error: unneeded `return` statement
94-
--> $DIR/needless_return.rs:118:13
106+
--> $DIR/needless_return.rs:129:13
95107
|
96108
LL | return;
97109
| ^^^^^^^ help: remove `return`
98110

99111
error: unneeded `return` statement
100-
--> $DIR/needless_return.rs:120:20
112+
--> $DIR/needless_return.rs:131:20
101113
|
102114
LL | let _ = || return;
103115
| ^^^^^^ help: replace `return` with an empty block: `{}`
104116

105117
error: unneeded `return` statement
106-
--> $DIR/needless_return.rs:126:32
118+
--> $DIR/needless_return.rs:137:32
107119
|
108120
LL | res.unwrap_or_else(|_| return Foo)
109121
| ^^^^^^^^^^ help: remove `return`: `Foo`
110122

111123
error: unneeded `return` statement
112-
--> $DIR/needless_return.rs:135:5
124+
--> $DIR/needless_return.rs:146:5
113125
|
114126
LL | return true;
115127
| ^^^^^^^^^^^^ help: remove `return`: `true`
116128

117129
error: unneeded `return` statement
118-
--> $DIR/needless_return.rs:139:5
130+
--> $DIR/needless_return.rs:150:5
119131
|
120132
LL | return true;
121133
| ^^^^^^^^^^^^ help: remove `return`: `true`
122134

123135
error: unneeded `return` statement
124-
--> $DIR/needless_return.rs:144:9
136+
--> $DIR/needless_return.rs:155:9
125137
|
126138
LL | return true;
127139
| ^^^^^^^^^^^^ help: remove `return`: `true`
128140

129141
error: unneeded `return` statement
130-
--> $DIR/needless_return.rs:146:9
142+
--> $DIR/needless_return.rs:157:9
131143
|
132144
LL | return false;
133145
| ^^^^^^^^^^^^^ help: remove `return`: `false`
134146

135147
error: unneeded `return` statement
136-
--> $DIR/needless_return.rs:152:17
148+
--> $DIR/needless_return.rs:163:17
137149
|
138150
LL | true => return false,
139151
| ^^^^^^^^^^^^ help: remove `return`: `false`
140152

141153
error: unneeded `return` statement
142-
--> $DIR/needless_return.rs:154:13
154+
--> $DIR/needless_return.rs:165:13
143155
|
144156
LL | return true;
145157
| ^^^^^^^^^^^^ help: remove `return`: `true`
146158

147159
error: unneeded `return` statement
148-
--> $DIR/needless_return.rs:161:9
160+
--> $DIR/needless_return.rs:172:9
149161
|
150162
LL | return true;
151163
| ^^^^^^^^^^^^ help: remove `return`: `true`
152164

153165
error: unneeded `return` statement
154-
--> $DIR/needless_return.rs:163:16
166+
--> $DIR/needless_return.rs:174:16
155167
|
156168
LL | let _ = || return true;
157169
| ^^^^^^^^^^^ help: remove `return`: `true`
158170

159171
error: unneeded `return` statement
160-
--> $DIR/needless_return.rs:171:5
172+
--> $DIR/needless_return.rs:182:5
161173
|
162174
LL | return;
163175
| ^^^^^^^ help: remove `return`
164176

165177
error: unneeded `return` statement
166-
--> $DIR/needless_return.rs:176:9
178+
--> $DIR/needless_return.rs:187:9
167179
|
168180
LL | return;
169181
| ^^^^^^^ help: remove `return`
170182

171183
error: unneeded `return` statement
172-
--> $DIR/needless_return.rs:178:9
184+
--> $DIR/needless_return.rs:189:9
173185
|
174186
LL | return;
175187
| ^^^^^^^ help: remove `return`
176188

177189
error: unneeded `return` statement
178-
--> $DIR/needless_return.rs:185:14
190+
--> $DIR/needless_return.rs:196:14
179191
|
180192
LL | _ => return,
181-
| ^^^^^^ help: replace `return` with an empty block: `{}`
193+
| ^^^^^^ help: replace `return` with a unit value: `()`
182194

183195
error: unneeded `return` statement
184-
--> $DIR/needless_return.rs:200:9
196+
--> $DIR/needless_return.rs:211:9
185197
|
186198
LL | return String::from("test");
187199
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::from("test")`
188200

189201
error: unneeded `return` statement
190-
--> $DIR/needless_return.rs:202:9
202+
--> $DIR/needless_return.rs:213:9
191203
|
192204
LL | return String::new();
193205
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
194206

195-
error: aborting due to 32 previous errors
207+
error: aborting due to 34 previous errors
196208

0 commit comments

Comments
 (0)