Skip to content

Commit 957dedb

Browse files
committed
Fix unused_unit false positive
Fixes #4076
1 parent 56f51b3 commit 957dedb

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

clippy_lints/src/returns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl EarlyLintPass for Return {
277277
if_chain! {
278278
if let Some(ref stmt) = block.stmts.last();
279279
if let ast::StmtKind::Expr(ref expr) = stmt.node;
280-
if is_unit_expr(expr) && !expr.span.from_expansion();
280+
if is_unit_expr(expr) && !stmt.span.from_expansion();
281281
then {
282282
let sp = expr.span;
283283
span_lint_and_then(cx, UNUSED_UNIT, sp, "unneeded unit expression", |db| {

tests/ui/unused_unit.fixed

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![rustfmt::skip]
1111

1212
#![deny(clippy::unused_unit)]
13+
#![allow(dead_code)]
1314

1415
struct Unitter;
1516
impl Unitter {
@@ -42,3 +43,16 @@ fn main() {
4243
}
4344
return;
4445
}
46+
47+
// https://github.com/rust-lang/rust-clippy/issues/4076
48+
fn foo() {
49+
macro_rules! foo {
50+
(recv($r:expr) -> $res:pat => $body:expr) => {
51+
$body
52+
}
53+
}
54+
55+
foo! {
56+
recv(rx) -> _x => ()
57+
}
58+
}

tests/ui/unused_unit.rs

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![rustfmt::skip]
1111

1212
#![deny(clippy::unused_unit)]
13+
#![allow(dead_code)]
1314

1415
struct Unitter;
1516
impl Unitter {
@@ -43,3 +44,16 @@ fn main() {
4344
}
4445
return();
4546
}
47+
48+
// https://github.com/rust-lang/rust-clippy/issues/4076
49+
fn foo() {
50+
macro_rules! foo {
51+
(recv($r:expr) -> $res:pat => $body:expr) => {
52+
$body
53+
}
54+
}
55+
56+
foo! {
57+
recv(rx) -> _x => ()
58+
}
59+
}

tests/ui/unused_unit.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unneeded unit return type
2-
--> $DIR/unused_unit.rs:18:59
2+
--> $DIR/unused_unit.rs:19:59
33
|
44
LL | pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) ->
55
| ___________________________________________________________^
@@ -13,37 +13,37 @@ LL | #![deny(clippy::unused_unit)]
1313
| ^^^^^^^^^^^^^^^^^^^
1414

1515
error: unneeded unit return type
16-
--> $DIR/unused_unit.rs:28:19
16+
--> $DIR/unused_unit.rs:29:19
1717
|
1818
LL | fn into(self) -> () {
1919
| ^^^^^ help: remove the `-> ()`
2020

2121
error: unneeded unit expression
22-
--> $DIR/unused_unit.rs:29:9
22+
--> $DIR/unused_unit.rs:30:9
2323
|
2424
LL | ()
2525
| ^^ help: remove the final `()`
2626

2727
error: unneeded unit return type
28-
--> $DIR/unused_unit.rs:33:18
28+
--> $DIR/unused_unit.rs:34:18
2929
|
3030
LL | fn return_unit() -> () { () }
3131
| ^^^^^ help: remove the `-> ()`
3232

3333
error: unneeded unit expression
34-
--> $DIR/unused_unit.rs:33:26
34+
--> $DIR/unused_unit.rs:34:26
3535
|
3636
LL | fn return_unit() -> () { () }
3737
| ^^ help: remove the final `()`
3838

3939
error: unneeded `()`
40-
--> $DIR/unused_unit.rs:42:14
40+
--> $DIR/unused_unit.rs:43:14
4141
|
4242
LL | break();
4343
| ^^ help: remove the `()`
4444

4545
error: unneeded `()`
46-
--> $DIR/unused_unit.rs:44:11
46+
--> $DIR/unused_unit.rs:45:11
4747
|
4848
LL | return();
4949
| ^^ help: remove the `()`

0 commit comments

Comments
 (0)