From 37a33461b1abec84f481e75de82e6d7230baee97 Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Sat, 18 Jan 2025 07:43:31 +0000 Subject: [PATCH] use `Reverse` instead of manually doing reverse comparison --- compiler/rustc_resolve/src/diagnostics.rs | 33 ++++++++++++----------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index dc26d4de57a7c..59de7e369bdfe 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1,3 +1,5 @@ +use std::cmp::Reverse; + use rustc_ast::expand::StrippedCfgItem; use rustc_ast::ptr::P; use rustc_ast::visit::{self, Visitor}; @@ -2357,22 +2359,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { // 2) `std` suggestions before `core` suggestions. let mut extern_crate_names = self.extern_prelude.keys().map(|ident| ident.name).collect::>(); - extern_crate_names.sort_by(|a, b| b.as_str().partial_cmp(a.as_str()).unwrap()); - - for name in extern_crate_names.into_iter() { - // Replace first ident with a crate name and check if that is valid. - path[0].ident.name = name; - let result = self.maybe_resolve_path(&path, None, parent_scope, None); - debug!( - "make_external_crate_suggestion: name={:?} path={:?} result={:?}", - name, path, result - ); - if let PathResult::Module(..) = result { - return Some((path, None)); - } - } - - None + extern_crate_names.sort_by_key(|&name| Reverse(name)); + + extern_crate_names + .into_iter() + .any(|name| { + // Replace first ident with a crate name and check if that is valid. + path[0].ident.name = name; + let result = self.maybe_resolve_path(&path, None, parent_scope, None); + debug!( + "make_external_crate_suggestion: name={:?} path={:?} result={:?}", + name, path, result + ); + matches!(result, PathResult::Module(..)) + }) + .then_some((path, None)) } /// Suggests importing a macro from the root of the crate rather than a module within