Split lint suggestion into two

This commit is contained in:
Hirochika Matsumoto 2020-09-26 15:39:39 +09:00
parent a433d4690e
commit cdb72df6f9
2 changed files with 36 additions and 23 deletions

View File

@ -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;
};
@ -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,
);
},

View File

@ -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
@ -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
|
@ -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
|