mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 14:38:46 +00:00
Return a struct and add applicability
This commit is contained in:
parent
d0dd797709
commit
97783a8cb9
1 changed files with 19 additions and 6 deletions
|
@ -41,6 +41,7 @@ pub(super) fn check<'tcx>(
|
||||||
if search_snippet.lines().count() <= 1 {
|
if search_snippet.lines().count() <= 1 {
|
||||||
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
|
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
|
||||||
// suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
|
// suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
|
||||||
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
let any_search_snippet = if_chain! {
|
let any_search_snippet = if_chain! {
|
||||||
if search_method == "find";
|
if search_method == "find";
|
||||||
if let hir::ExprKind::Closure(_, _, body_id, ..) = search_arg.kind;
|
if let hir::ExprKind::Closure(_, _, body_id, ..) = search_arg.kind;
|
||||||
|
@ -52,8 +53,12 @@ pub(super) fn check<'tcx>(
|
||||||
} else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
|
} else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
|
||||||
// `find()` provides a reference to the item, but `any` does not,
|
// `find()` provides a reference to the item, but `any` does not,
|
||||||
// so we should fix item usages for suggestion
|
// so we should fix item usages for suggestion
|
||||||
get_closure_suggestion(cx, search_arg, closure_body)
|
if let Some(closure_sugg) = get_closure_suggestion(cx, search_arg, closure_body) {
|
||||||
.or_else(|| Some(search_snippet.to_string()))
|
applicability = closure_sugg.applicability;
|
||||||
|
Some(closure_sugg.suggestion)
|
||||||
|
} else {
|
||||||
|
Some(search_snippet.to_string())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -73,7 +78,7 @@ pub(super) fn check<'tcx>(
|
||||||
"any({})",
|
"any({})",
|
||||||
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
|
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
|
||||||
),
|
),
|
||||||
Applicability::MachineApplicable,
|
applicability,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let iter = snippet(cx, search_recv.span, "..");
|
let iter = snippet(cx, search_recv.span, "..");
|
||||||
|
@ -88,7 +93,7 @@ pub(super) fn check<'tcx>(
|
||||||
iter,
|
iter,
|
||||||
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
|
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
|
||||||
),
|
),
|
||||||
Applicability::MachineApplicable,
|
applicability,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -153,6 +158,11 @@ pub(super) fn check<'tcx>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ClosureSugg {
|
||||||
|
applicability: Applicability,
|
||||||
|
suggestion: String,
|
||||||
|
}
|
||||||
|
|
||||||
// Build suggestion gradually by handling closure arg specific usages,
|
// Build suggestion gradually by handling closure arg specific usages,
|
||||||
// such as explicit deref and borrowing cases.
|
// such as explicit deref and borrowing cases.
|
||||||
// Returns `None` if no such use cases have been triggered in closure body
|
// Returns `None` if no such use cases have been triggered in closure body
|
||||||
|
@ -160,7 +170,7 @@ fn get_closure_suggestion<'tcx>(
|
||||||
cx: &LateContext<'_>,
|
cx: &LateContext<'_>,
|
||||||
search_arg: &'tcx hir::Expr<'_>,
|
search_arg: &'tcx hir::Expr<'_>,
|
||||||
closure_body: &hir::Body<'_>,
|
closure_body: &hir::Body<'_>,
|
||||||
) -> Option<String> {
|
) -> Option<ClosureSugg> {
|
||||||
let mut visitor = DerefDelegate {
|
let mut visitor = DerefDelegate {
|
||||||
cx,
|
cx,
|
||||||
closure_span: search_arg.span,
|
closure_span: search_arg.span,
|
||||||
|
@ -178,7 +188,10 @@ fn get_closure_suggestion<'tcx>(
|
||||||
if visitor.suggestion_start.is_empty() {
|
if visitor.suggestion_start.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(visitor.finish())
|
Some(ClosureSugg {
|
||||||
|
applicability: visitor.applicability,
|
||||||
|
suggestion: visitor.finish(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue