Skip to content

Commit 0d2f1ae

Browse files
committed
Auto merge of rust-lang#6247 - giraffate:fix_suggestion_to_add_space_in_unused_unit, r=ebroto
Fix suggestion to add unneeded space in `unused_unit` Fix rust-lang/rust-clippy#6230 changelog: Fix suggestion to add unneeded space in `unused_unit`
2 parents 5b52474 + 4c58860 commit 0d2f1ae

File tree

4 files changed

+83
-36
lines changed

4 files changed

+83
-36
lines changed

clippy_lints/src/unused_unit.rs

+11
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
123123
fn_source
124124
.rfind("->")
125125
.map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
126+
let mut rpos = rpos;
127+
let chars: Vec<char> = fn_source.chars().collect();
128+
while rpos > 1 {
129+
if let Some(c) = chars.get(rpos - 1) {
130+
if c.is_whitespace() {
131+
rpos -= 1;
132+
continue;
133+
}
134+
}
135+
break;
136+
}
126137
(
127138
#[allow(clippy::cast_possible_truncation)]
128139
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),

tests/ui/unused_unit.fixed

+20-11
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,35 @@
1515
struct Unitter;
1616
impl Unitter {
1717
#[allow(clippy::no_effect)]
18-
pub fn get_unit<F: Fn() , G>(&self, f: F, _g: G)
19-
where G: Fn() {
20-
let _y: &dyn Fn() = &f;
18+
pub fn get_unit<F: Fn(), G>(&self, f: F, _g: G)
19+
where G: Fn() {
20+
let _y: &dyn Fn() = &f;
2121
(); // this should not lint, as it's not in return type position
2222
}
2323
}
2424

2525
impl Into<()> for Unitter {
2626
#[rustfmt::skip]
27-
fn into(self) {
27+
fn into(self) {
2828

2929
}
3030
}
3131

3232
trait Trait {
33-
fn redundant<F: FnOnce() , G, H>(&self, _f: F, _g: G, _h: H)
33+
fn redundant<F: FnOnce(), G, H>(&self, _f: F, _g: G, _h: H)
3434
where
35-
G: FnMut() ,
36-
H: Fn() ;
35+
G: FnMut(),
36+
H: Fn();
3737
}
3838

3939
impl Trait for Unitter {
40-
fn redundant<F: FnOnce() , G, H>(&self, _f: F, _g: G, _h: H)
40+
fn redundant<F: FnOnce(), G, H>(&self, _f: F, _g: G, _h: H)
4141
where
42-
G: FnMut() ,
43-
H: Fn() {}
42+
G: FnMut(),
43+
H: Fn() {}
4444
}
4545

46-
fn return_unit() { }
46+
fn return_unit() { }
4747

4848
#[allow(clippy::needless_return)]
4949
#[allow(clippy::never_loop)]
@@ -70,3 +70,12 @@ fn foo() {
7070
recv(rx) -> _x => ()
7171
}
7272
}
73+
74+
#[rustfmt::skip]
75+
fn test(){}
76+
77+
#[rustfmt::skip]
78+
fn test2(){}
79+
80+
#[rustfmt::skip]
81+
fn test3(){}

tests/ui/unused_unit.rs

+9
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,12 @@ fn foo() {
7070
recv(rx) -> _x => ()
7171
}
7272
}
73+
74+
#[rustfmt::skip]
75+
fn test()->(){}
76+
77+
#[rustfmt::skip]
78+
fn test2() ->(){}
79+
80+
#[rustfmt::skip]
81+
fn test3()-> (){}

tests/ui/unused_unit.stderr

+43-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: unneeded unit return type
2-
--> $DIR/unused_unit.rs:18:29
2+
--> $DIR/unused_unit.rs:18:28
33
|
44
LL | pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) -> ()
5-
| ^^^^^ help: remove the `-> ()`
5+
| ^^^^^^ help: remove the `-> ()`
66
|
77
note: the lint level is defined here
88
--> $DIR/unused_unit.rs:12:9
@@ -11,28 +11,28 @@ LL | #![deny(clippy::unused_unit)]
1111
| ^^^^^^^^^^^^^^^^^^^
1212

1313
error: unneeded unit return type
14-
--> $DIR/unused_unit.rs:19:19
14+
--> $DIR/unused_unit.rs:19:18
1515
|
1616
LL | where G: Fn() -> () {
17-
| ^^^^^ help: remove the `-> ()`
17+
| ^^^^^^ help: remove the `-> ()`
1818

1919
error: unneeded unit return type
20-
--> $DIR/unused_unit.rs:18:59
20+
--> $DIR/unused_unit.rs:18:58
2121
|
2222
LL | pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) -> ()
23-
| ^^^^^ help: remove the `-> ()`
23+
| ^^^^^^ help: remove the `-> ()`
2424

2525
error: unneeded unit return type
26-
--> $DIR/unused_unit.rs:20:27
26+
--> $DIR/unused_unit.rs:20:26
2727
|
2828
LL | let _y: &dyn Fn() -> () = &f;
29-
| ^^^^^ help: remove the `-> ()`
29+
| ^^^^^^ help: remove the `-> ()`
3030

3131
error: unneeded unit return type
32-
--> $DIR/unused_unit.rs:27:19
32+
--> $DIR/unused_unit.rs:27:18
3333
|
3434
LL | fn into(self) -> () {
35-
| ^^^^^ help: remove the `-> ()`
35+
| ^^^^^^ help: remove the `-> ()`
3636

3737
error: unneeded unit expression
3838
--> $DIR/unused_unit.rs:28:9
@@ -41,46 +41,46 @@ LL | ()
4141
| ^^ help: remove the final `()`
4242

4343
error: unneeded unit return type
44-
--> $DIR/unused_unit.rs:33:30
44+
--> $DIR/unused_unit.rs:33:29
4545
|
4646
LL | fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
47-
| ^^^^^ help: remove the `-> ()`
47+
| ^^^^^^ help: remove the `-> ()`
4848

4949
error: unneeded unit return type
50-
--> $DIR/unused_unit.rs:35:20
50+
--> $DIR/unused_unit.rs:35:19
5151
|
5252
LL | G: FnMut() -> (),
53-
| ^^^^^ help: remove the `-> ()`
53+
| ^^^^^^ help: remove the `-> ()`
5454

5555
error: unneeded unit return type
56-
--> $DIR/unused_unit.rs:36:17
56+
--> $DIR/unused_unit.rs:36:16
5757
|
5858
LL | H: Fn() -> ();
59-
| ^^^^^ help: remove the `-> ()`
59+
| ^^^^^^ help: remove the `-> ()`
6060

6161
error: unneeded unit return type
62-
--> $DIR/unused_unit.rs:40:30
62+
--> $DIR/unused_unit.rs:40:29
6363
|
6464
LL | fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
65-
| ^^^^^ help: remove the `-> ()`
65+
| ^^^^^^ help: remove the `-> ()`
6666

6767
error: unneeded unit return type
68-
--> $DIR/unused_unit.rs:42:20
68+
--> $DIR/unused_unit.rs:42:19
6969
|
7070
LL | G: FnMut() -> (),
71-
| ^^^^^ help: remove the `-> ()`
71+
| ^^^^^^ help: remove the `-> ()`
7272

7373
error: unneeded unit return type
74-
--> $DIR/unused_unit.rs:43:17
74+
--> $DIR/unused_unit.rs:43:16
7575
|
7676
LL | H: Fn() -> () {}
77-
| ^^^^^ help: remove the `-> ()`
77+
| ^^^^^^ help: remove the `-> ()`
7878

7979
error: unneeded unit return type
80-
--> $DIR/unused_unit.rs:46:18
80+
--> $DIR/unused_unit.rs:46:17
8181
|
8282
LL | fn return_unit() -> () { () }
83-
| ^^^^^ help: remove the `-> ()`
83+
| ^^^^^^ help: remove the `-> ()`
8484

8585
error: unneeded unit expression
8686
--> $DIR/unused_unit.rs:46:26
@@ -100,5 +100,23 @@ error: unneeded `()`
100100
LL | return();
101101
| ^^ help: remove the `()`
102102

103-
error: aborting due to 16 previous errors
103+
error: unneeded unit return type
104+
--> $DIR/unused_unit.rs:75:10
105+
|
106+
LL | fn test()->(){}
107+
| ^^^^ help: remove the `-> ()`
108+
109+
error: unneeded unit return type
110+
--> $DIR/unused_unit.rs:78:11
111+
|
112+
LL | fn test2() ->(){}
113+
| ^^^^^ help: remove the `-> ()`
114+
115+
error: unneeded unit return type
116+
--> $DIR/unused_unit.rs:81:11
117+
|
118+
LL | fn test3()-> (){}
119+
| ^^^^^ help: remove the `-> ()`
120+
121+
error: aborting due to 19 previous errors
104122

0 commit comments

Comments
 (0)