@@ -9,34 +9,18 @@ use rustc_span::{Span, sym};
99use super :: DOUBLE_ENDED_ITERATOR_LAST ;
1010
1111pub ( super ) fn check ( cx : & LateContext < ' _ > , expr : & ' _ Expr < ' _ > , self_expr : & ' _ Expr < ' _ > , call_span : Span ) {
12- let typeck = cx. typeck_results ( ) ;
13-
14- // Check if the current "last" method is that of the Iterator trait
15- if !is_trait_method ( cx, expr, sym:: Iterator ) {
16- return ;
17- }
18-
19- // Find id for DoubleEndedIterator trait
20- let Some ( deiter_id) = cx. tcx . get_diagnostic_item ( sym:: DoubleEndedIterator ) else {
21- return ;
22- } ;
23-
24- // Find the type of self
25- let self_type = typeck. expr_ty ( self_expr) . peel_refs ( ) ;
26-
27- // Check that the object implements the DoubleEndedIterator trait
28- if !implements_trait ( cx, self_type, deiter_id, & [ ] ) {
29- return ;
12+ if is_trait_method ( cx, expr, sym:: Iterator )
13+ && let Some ( deiter_id) = cx. tcx . get_diagnostic_item ( sym:: DoubleEndedIterator )
14+ && implements_trait ( cx, cx. typeck_results ( ) . expr_ty ( self_expr) . peel_refs ( ) , deiter_id, & [ ] )
15+ {
16+ span_lint_and_sugg (
17+ cx,
18+ DOUBLE_ENDED_ITERATOR_LAST ,
19+ call_span,
20+ "called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator" ,
21+ "try" ,
22+ "next_back()" . to_string ( ) ,
23+ Applicability :: MachineApplicable ,
24+ ) ;
3025 }
31-
32- // Emit lint
33- span_lint_and_sugg (
34- cx,
35- DOUBLE_ENDED_ITERATOR_LAST ,
36- call_span,
37- "called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator" ,
38- "try" ,
39- "next_back()" . to_string ( ) ,
40- Applicability :: MachineApplicable ,
41- ) ;
4226}
0 commit comments