Prefer if chain to let-else
This commit is contained in:
parent
09c5d34f98
commit
27acfd8a5b
1 changed files with 13 additions and 29 deletions
|
|
@ -9,34 +9,18 @@ use rustc_span::{Span, sym};
|
|||
use super::DOUBLE_ENDED_ITERATOR_LAST;
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, self_expr: &'_ Expr<'_>, call_span: Span) {
|
||||
let typeck = cx.typeck_results();
|
||||
|
||||
// Check if the current "last" method is that of the Iterator trait
|
||||
if !is_trait_method(cx, expr, sym::Iterator) {
|
||||
return;
|
||||
if is_trait_method(cx, expr, sym::Iterator)
|
||||
&& let Some(deiter_id) = cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator)
|
||||
&& implements_trait(cx, cx.typeck_results().expr_ty(self_expr).peel_refs(), deiter_id, &[])
|
||||
{
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
DOUBLE_ENDED_ITERATOR_LAST,
|
||||
call_span,
|
||||
"called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator",
|
||||
"try",
|
||||
"next_back()".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
|
||||
// Find id for DoubleEndedIterator trait
|
||||
let Some(deiter_id) = cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator) else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Find the type of self
|
||||
let self_type = typeck.expr_ty(self_expr).peel_refs();
|
||||
|
||||
// Check that the object implements the DoubleEndedIterator trait
|
||||
if !implements_trait(cx, self_type, deiter_id, &[]) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Emit lint
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
DOUBLE_ENDED_ITERATOR_LAST,
|
||||
call_span,
|
||||
"called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator",
|
||||
"try",
|
||||
"next_back()".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue