Skip to content

Commit

Permalink
Split lint suggestion into two
Browse files Browse the repository at this point in the history
  • Loading branch information
hkmatsumoto committed Sep 26, 2020
1 parent 055cc4f commit 6fb22b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
41 changes: 22 additions & 19 deletions clippy_lints/src/unnecessary_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
}
}

let path = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
&paths::OPTION_SOME
let (return_type, path) = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
("Option", &paths::OPTION_SOME)
} else if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {
&paths::RESULT_OK
("Result", &paths::RESULT_OK)
} else {
return;
};
Expand All @@ -98,23 +98,26 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
UNNECESSARY_WRAP,
span,
"this function returns unnecessarily wrapping data",
move |diag| {
|diag| {
let inner_ty = return_ty(cx, hir_id)
.walk()
.skip(1) // skip `std::option::Option` or `std::result::Result`
.take(1) // take the first outermost inner type
.filter_map(|inner| match inner.unpack() {
GenericArgKind::Type(inner_ty) => Some(inner_ty.to_string()),
_ => None,
});
inner_ty.for_each(|inner_ty| {
diag.span_suggestion(
fn_decl.output.span(),
format!("remove `{}` from the return type...", return_type).as_str(),
inner_ty,
Applicability::MachineApplicable,
);
});
diag.multipart_suggestion(
"factor this out to",
suggs
.into_iter()
.chain({
let inner_ty = return_ty(cx, hir_id)
.walk()
.skip(1) // skip `std::option::Option` or `std::result::Result`
.take(1) // take the first outermost inner type
.filter_map(|inner| match inner.unpack() {
GenericArgKind::Type(inner_ty) => Some(inner_ty.to_string()),
_ => None,
});
inner_ty.map(|inner_ty| (fn_decl.output.span(), inner_ty))
})
.collect(),
"...and change the returning expressions",
suggs,
Applicability::MachineApplicable,
);
},
Expand Down
18 changes: 14 additions & 4 deletions tests/ui/unnecessary_wrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ LL | | }
| |_^
|
= note: `-D clippy::unnecessary-wrap` implied by `-D warnings`
help: factor this out to
help: remove `Option` from the return type...
|
LL | fn func1(a: bool, b: bool) -> i32 {
LL | if a && b {
| ^^^
help: ...and change the returning expressions
|
LL | return 42;
LL | }
LL | if a {
LL | Some(-1);
LL | 2
LL | } else {
...

error: this function returns unnecessarily wrapping data
Expand All @@ -29,9 +33,12 @@ LL | | Some(1)
LL | | }
| |_^
|
help: factor this out to
help: remove `Option` from the return type...
|
LL | fn func4() -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | 1
|

Expand All @@ -43,9 +50,12 @@ LL | | Ok(1)
LL | | }
| |_^
|
help: factor this out to
help: remove `Result` from the return type...
|
LL | fn func6() -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | 1
|

Expand Down

0 comments on commit 6fb22b0

Please sign in to comment.