From 9126d556628b04fd4b72443817d8d1aef9a8d372 Mon Sep 17 00:00:00 2001 From: Makai Date: Fri, 14 Aug 2026 23:54:11 +0800 Subject: [PATCH] cleanup: rip out unnecessary `iter().last()` and `iter().next()` --- compiler/rustc_ast_passes/src/ast_validation.rs | 2 +- compiler/rustc_hir_typeck/src/lib.rs | 2 +- compiler/rustc_parse/src/parser/pat.rs | 2 +- compiler/rustc_resolve/src/diagnostics/impls.rs | 6 ++---- compiler/rustc_resolve/src/late.rs | 5 ++--- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 3270fb940bab3..fb33f2823372b 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1180,7 +1180,7 @@ impl<'a> AstValidator<'a> { self.dcx().emit_err(diagnostics::ArgsBeforeConstraint { arg_spans: arg_spans.clone(), constraints: constraint_spans[0], - args: *arg_spans.iter().last().unwrap(), + args: *arg_spans.last().unwrap(), data: data.span, constraint_spans: diagnostics::EmptyLabelManySpans(constraint_spans), arg_spans2: diagnostics::EmptyLabelManySpans(arg_spans), diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 7a2670f3b1b78..7f38aeb5ac3fa 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -324,7 +324,7 @@ fn extend_err_with_const_context( { // `foo()`, point at the const parameter in the definition of `foo`. if let Some(i) = - path.segments.iter().last().and_then(|segment| segment.args).and_then(|args| { + path.segments.last().and_then(|segment| segment.args).and_then(|args| { args.args.iter().position(|arg| { matches!(arg, hir::GenericArg::Const(arg) if arg.hir_id == parent.hir_id) }) diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index d017a27e8f77f..266a2d134199c 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -1688,7 +1688,7 @@ impl<'a> Parser<'a> { /// If the user writes `S { ref field: name }` instead of `S { field: ref name }`, we suggest /// the correct code. fn recover_misplaced_pattern_modifiers(&self, fields: &ThinVec, err: &mut Diag<'a>) { - if let Some(last) = fields.iter().last() + if let Some(last) = fields.last() && last.is_shorthand && let PatKind::Ident(binding, ident, None) = last.pat.kind && binding != BindingMode::NONE diff --git a/compiler/rustc_resolve/src/diagnostics/impls.rs b/compiler/rustc_resolve/src/diagnostics/impls.rs index 9fa762c87ef3e..149f34cb6c35d 100644 --- a/compiler/rustc_resolve/src/diagnostics/impls.rs +++ b/compiler/rustc_resolve/src/diagnostics/impls.rs @@ -256,9 +256,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { for note in notes { diag.note(note); } - } else if let Some((_, UnresolvedImportError { note: Some(note), .. })) = - errors.iter().last() - { + } else if let Some((_, UnresolvedImportError { note: Some(note), .. })) = errors.last() { diag.note(note.clone()); } @@ -2876,7 +2874,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { if struct_expr.fields.is_empty() { return; } - let last_span = struct_expr.fields.iter().last().unwrap().span; + let last_span = struct_expr.fields.last().unwrap().span; let mut iter = struct_expr.fields.iter().peekable(); let mut prev: Option = None; while let Some(field) = iter.next() { diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index c21d3653a13be..543082a6af7fc 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -4092,7 +4092,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { v.could_be_path = false; } self.report_error( - v.origin.iter().next().unwrap().0, + v.origin.first().unwrap().0, ResolutionError::VariableNotBoundInPattern(v, self.parent_scope), ); } @@ -4757,8 +4757,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { self.resolve_path(&std_path, Some(ns), None, source) { // Check if we wrote `str::from_utf8` instead of `std::str::from_utf8` - let item_span = - path.iter().last().map_or(path_span, |segment| segment.ident.span); + let item_span = path.last().map_or(path_span, |segment| segment.ident.span); self.r.confused_type_with_std_module.insert(item_span, path_span); self.r.confused_type_with_std_module.insert(path_span, path_span);