Skip to content

Commit e22ccf5

Browse files
committed
Auto merge of rust-lang#5592 - ebroto:extend_unused_unit, r=flip1995
unused_unit: lint also in type parameters and where clauses changelog: unused_unit now also lints in type parameters and where clauses Fixes rust-lang#5585
2 parents e1842b0 + f20b962 commit e22ccf5

File tree

4 files changed

+138
-41
lines changed

4 files changed

+138
-41
lines changed

clippy_lints/src/returns.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -248,28 +248,7 @@ impl EarlyLintPass for Return {
248248
if let ast::TyKind::Tup(ref vals) = ty.kind;
249249
if vals.is_empty() && !ty.span.from_expansion() && get_def(span) == get_def(ty.span);
250250
then {
251-
let (rspan, appl) = if let Ok(fn_source) =
252-
cx.sess().source_map()
253-
.span_to_snippet(span.with_hi(ty.span.hi())) {
254-
if let Some(rpos) = fn_source.rfind("->") {
255-
#[allow(clippy::cast_possible_truncation)]
256-
(ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
257-
Applicability::MachineApplicable)
258-
} else {
259-
(ty.span, Applicability::MaybeIncorrect)
260-
}
261-
} else {
262-
(ty.span, Applicability::MaybeIncorrect)
263-
};
264-
span_lint_and_sugg(
265-
cx,
266-
UNUSED_UNIT,
267-
rspan,
268-
"unneeded unit return type",
269-
"remove the `-> ()`",
270-
String::new(),
271-
appl,
272-
);
251+
lint_unneeded_unit_return(cx, ty, span);
273252
}
274253
}
275254
}
@@ -313,6 +292,22 @@ impl EarlyLintPass for Return {
313292
_ => (),
314293
}
315294
}
295+
296+
fn check_poly_trait_ref(&mut self, cx: &EarlyContext<'_>, poly: &ast::PolyTraitRef, _: &ast::TraitBoundModifier) {
297+
let segments = &poly.trait_ref.path.segments;
298+
299+
if_chain! {
300+
if segments.len() == 1;
301+
if ["Fn", "FnMut", "FnOnce"].contains(&&*segments[0].ident.name.as_str());
302+
if let Some(args) = &segments[0].args;
303+
if let ast::GenericArgs::Parenthesized(generic_args) = &**args;
304+
if let ast::FnRetTy::Ty(ty) = &generic_args.output;
305+
if ty.kind.is_unit();
306+
then {
307+
lint_unneeded_unit_return(cx, ty, generic_args.span);
308+
}
309+
}
310+
}
316311
}
317312

318313
fn attr_is_cfg(attr: &ast::Attribute) -> bool {
@@ -337,3 +332,28 @@ fn is_unit_expr(expr: &ast::Expr) -> bool {
337332
false
338333
}
339334
}
335+
336+
fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
337+
let (ret_span, appl) = if let Ok(fn_source) = cx.sess().source_map().span_to_snippet(span.with_hi(ty.span.hi())) {
338+
if let Some(rpos) = fn_source.rfind("->") {
339+
#[allow(clippy::cast_possible_truncation)]
340+
(
341+
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
342+
Applicability::MachineApplicable,
343+
)
344+
} else {
345+
(ty.span, Applicability::MaybeIncorrect)
346+
}
347+
} else {
348+
(ty.span, Applicability::MaybeIncorrect)
349+
};
350+
span_lint_and_sugg(
351+
cx,
352+
UNUSED_UNIT,
353+
ret_span,
354+
"unneeded unit return type",
355+
"remove the `-> ()`",
356+
String::new(),
357+
appl,
358+
);
359+
}

tests/ui/unused_unit.fixed

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414

1515
struct Unitter;
1616
impl Unitter {
17-
// try to disorient the lint with multiple unit returns and newlines
1817
#[allow(clippy::no_effect)]
19-
pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G)
20-
where G: Fn() -> () {
21-
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;
2221
(); // this should not lint, as it's not in return type position
2322
}
2423
}
@@ -30,6 +29,20 @@ impl Into<()> for Unitter {
3029
}
3130
}
3231

32+
trait Trait {
33+
fn redundant<F: FnOnce() , G, H>(&self, _f: F, _g: G, _h: H)
34+
where
35+
G: FnMut() ,
36+
H: Fn() ;
37+
}
38+
39+
impl Trait for Unitter {
40+
fn redundant<F: FnOnce() , G, H>(&self, _f: F, _g: G, _h: H)
41+
where
42+
G: FnMut() ,
43+
H: Fn() {}
44+
}
45+
3346
fn return_unit() { }
3447

3548
#[allow(clippy::needless_return)]

tests/ui/unused_unit.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
struct Unitter;
1616
impl Unitter {
17-
// try to disorient the lint with multiple unit returns and newlines
1817
#[allow(clippy::no_effect)]
19-
pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) ->
20-
()
18+
pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) -> ()
2119
where G: Fn() -> () {
2220
let _y: &dyn Fn() -> () = &f;
2321
(); // this should not lint, as it's not in return type position
@@ -31,6 +29,20 @@ impl Into<()> for Unitter {
3129
}
3230
}
3331

32+
trait Trait {
33+
fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
34+
where
35+
G: FnMut() -> (),
36+
H: Fn() -> ();
37+
}
38+
39+
impl Trait for Unitter {
40+
fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
41+
where
42+
G: FnMut() -> (),
43+
H: Fn() -> () {}
44+
}
45+
3446
fn return_unit() -> () { () }
3547

3648
#[allow(clippy::needless_return)]

tests/ui/unused_unit.stderr

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error: unneeded unit return type
2-
--> $DIR/unused_unit.rs:19:59
2+
--> $DIR/unused_unit.rs:18:29
33
|
4-
LL | pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) ->
5-
| ___________________________________________________________^
6-
LL | | ()
7-
| |__________^ help: remove the `-> ()`
4+
LL | pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) -> ()
5+
| ^^^^^ help: remove the `-> ()`
86
|
97
note: the lint level is defined here
108
--> $DIR/unused_unit.rs:12:9
@@ -13,40 +11,94 @@ LL | #![deny(clippy::unused_unit)]
1311
| ^^^^^^^^^^^^^^^^^^^
1412

1513
error: unneeded unit return type
16-
--> $DIR/unused_unit.rs:29:19
14+
--> $DIR/unused_unit.rs:19:19
15+
|
16+
LL | where G: Fn() -> () {
17+
| ^^^^^ help: remove the `-> ()`
18+
19+
error: unneeded unit return type
20+
--> $DIR/unused_unit.rs:18:59
21+
|
22+
LL | pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) -> ()
23+
| ^^^^^ help: remove the `-> ()`
24+
25+
error: unneeded unit return type
26+
--> $DIR/unused_unit.rs:20:27
27+
|
28+
LL | let _y: &dyn Fn() -> () = &f;
29+
| ^^^^^ help: remove the `-> ()`
30+
31+
error: unneeded unit return type
32+
--> $DIR/unused_unit.rs:27:19
1733
|
1834
LL | fn into(self) -> () {
1935
| ^^^^^ help: remove the `-> ()`
2036

2137
error: unneeded unit expression
22-
--> $DIR/unused_unit.rs:30:9
38+
--> $DIR/unused_unit.rs:28:9
2339
|
2440
LL | ()
2541
| ^^ help: remove the final `()`
2642

2743
error: unneeded unit return type
28-
--> $DIR/unused_unit.rs:34:18
44+
--> $DIR/unused_unit.rs:33:30
45+
|
46+
LL | fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
47+
| ^^^^^ help: remove the `-> ()`
48+
49+
error: unneeded unit return type
50+
--> $DIR/unused_unit.rs:35:20
51+
|
52+
LL | G: FnMut() -> (),
53+
| ^^^^^ help: remove the `-> ()`
54+
55+
error: unneeded unit return type
56+
--> $DIR/unused_unit.rs:36:17
57+
|
58+
LL | H: Fn() -> ();
59+
| ^^^^^ help: remove the `-> ()`
60+
61+
error: unneeded unit return type
62+
--> $DIR/unused_unit.rs:40:30
63+
|
64+
LL | fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
65+
| ^^^^^ help: remove the `-> ()`
66+
67+
error: unneeded unit return type
68+
--> $DIR/unused_unit.rs:42:20
69+
|
70+
LL | G: FnMut() -> (),
71+
| ^^^^^ help: remove the `-> ()`
72+
73+
error: unneeded unit return type
74+
--> $DIR/unused_unit.rs:43:17
75+
|
76+
LL | H: Fn() -> () {}
77+
| ^^^^^ help: remove the `-> ()`
78+
79+
error: unneeded unit return type
80+
--> $DIR/unused_unit.rs:46:18
2981
|
3082
LL | fn return_unit() -> () { () }
3183
| ^^^^^ help: remove the `-> ()`
3284

3385
error: unneeded unit expression
34-
--> $DIR/unused_unit.rs:34:26
86+
--> $DIR/unused_unit.rs:46:26
3587
|
3688
LL | fn return_unit() -> () { () }
3789
| ^^ help: remove the final `()`
3890

3991
error: unneeded `()`
40-
--> $DIR/unused_unit.rs:44:14
92+
--> $DIR/unused_unit.rs:56:14
4193
|
4294
LL | break();
4395
| ^^ help: remove the `()`
4496

4597
error: unneeded `()`
46-
--> $DIR/unused_unit.rs:46:11
98+
--> $DIR/unused_unit.rs:58:11
4799
|
48100
LL | return();
49101
| ^^ help: remove the `()`
50102

51-
error: aborting due to 7 previous errors
103+
error: aborting due to 16 previous errors
52104

0 commit comments

Comments
 (0)