Skip to content

Commit 6fb22b0

Browse files
committed
Split lint suggestion into two
1 parent 055cc4f commit 6fb22b0

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

clippy_lints/src/unnecessary_wrap.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
6868
}
6969
}
7070

71-
let path = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
72-
&paths::OPTION_SOME
71+
let (return_type, path) = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
72+
("Option", &paths::OPTION_SOME)
7373
} else if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {
74-
&paths::RESULT_OK
74+
("Result", &paths::RESULT_OK)
7575
} else {
7676
return;
7777
};
@@ -98,23 +98,26 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
9898
UNNECESSARY_WRAP,
9999
span,
100100
"this function returns unnecessarily wrapping data",
101-
move |diag| {
101+
|diag| {
102+
let inner_ty = return_ty(cx, hir_id)
103+
.walk()
104+
.skip(1) // skip `std::option::Option` or `std::result::Result`
105+
.take(1) // take the first outermost inner type
106+
.filter_map(|inner| match inner.unpack() {
107+
GenericArgKind::Type(inner_ty) => Some(inner_ty.to_string()),
108+
_ => None,
109+
});
110+
inner_ty.for_each(|inner_ty| {
111+
diag.span_suggestion(
112+
fn_decl.output.span(),
113+
format!("remove `{}` from the return type...", return_type).as_str(),
114+
inner_ty,
115+
Applicability::MachineApplicable,
116+
);
117+
});
102118
diag.multipart_suggestion(
103-
"factor this out to",
104-
suggs
105-
.into_iter()
106-
.chain({
107-
let inner_ty = return_ty(cx, hir_id)
108-
.walk()
109-
.skip(1) // skip `std::option::Option` or `std::result::Result`
110-
.take(1) // take the first outermost inner type
111-
.filter_map(|inner| match inner.unpack() {
112-
GenericArgKind::Type(inner_ty) => Some(inner_ty.to_string()),
113-
_ => None,
114-
});
115-
inner_ty.map(|inner_ty| (fn_decl.output.span(), inner_ty))
116-
})
117-
.collect(),
119+
"...and change the returning expressions",
120+
suggs,
118121
Applicability::MachineApplicable,
119122
);
120123
},

tests/ui/unnecessary_wrap.stderr

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ LL | | }
1111
| |_^
1212
|
1313
= note: `-D clippy::unnecessary-wrap` implied by `-D warnings`
14-
help: factor this out to
14+
help: remove `Option` from the return type...
1515
|
1616
LL | fn func1(a: bool, b: bool) -> i32 {
17-
LL | if a && b {
17+
| ^^^
18+
help: ...and change the returning expressions
19+
|
1820
LL | return 42;
1921
LL | }
2022
LL | if a {
2123
LL | Some(-1);
24+
LL | 2
25+
LL | } else {
2226
...
2327

2428
error: this function returns unnecessarily wrapping data
@@ -29,9 +33,12 @@ LL | | Some(1)
2933
LL | | }
3034
| |_^
3135
|
32-
help: factor this out to
36+
help: remove `Option` from the return type...
3337
|
3438
LL | fn func4() -> i32 {
39+
| ^^^
40+
help: ...and change the returning expressions
41+
|
3542
LL | 1
3643
|
3744

@@ -43,9 +50,12 @@ LL | | Ok(1)
4350
LL | | }
4451
| |_^
4552
|
46-
help: factor this out to
53+
help: remove `Result` from the return type...
4754
|
4855
LL | fn func6() -> i32 {
56+
| ^^^
57+
help: ...and change the returning expressions
58+
|
4959
LL | 1
5060
|
5161

0 commit comments

Comments
 (0)