Skip to content

Commit 0e66490

Browse files
authored
Rollup merge of rust-lang#143922 - nnethercote:join_path, r=petrochenkov
Improve path segment joining Currently paths are joined with `::` in many places, in a variety of ways. This PR unifies things. r? ``@petrochenkov``
2 parents 7823aac + b7caf75 commit 0e66490

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

clippy_lints/src/methods/from_iter_instead_of_collect.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::ty::implements_trait;
66
use clippy_utils::{is_path_diagnostic_item, sugg};
7+
use rustc_ast::join_path_idents;
78
use rustc_errors::Applicability;
89
use rustc_hir::def::Res;
910
use rustc_hir::{self as hir, Expr, ExprKind, GenericArg, QPath, TyKind};
@@ -47,7 +48,7 @@ fn build_full_type(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, app: &mut Applica
4748
&& let QPath::Resolved(None, ty_path) = &ty_qpath
4849
&& let Res::Def(_, ty_did) = ty_path.res
4950
{
50-
let mut ty_str = itertools::join(ty_path.segments.iter().map(|s| s.ident), "::");
51+
let mut ty_str = join_path_idents(ty_path.segments.iter().map(|seg| seg.ident));
5152
let mut first = true;
5253
let mut append = |arg: &str| {
5354
write!(&mut ty_str, "{}{arg}", [", ", "<"][usize::from(first)]).unwrap();

clippy_utils/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ use std::sync::{Mutex, MutexGuard, OnceLock};
8989

9090
use itertools::Itertools;
9191
use rustc_abi::Integer;
92+
use rustc_ast::join_path_syms;
9293
use rustc_ast::ast::{self, LitKind, RangeLimits};
9394
use rustc_attr_data_structures::{AttributeKind, find_attr};
9495
use rustc_data_structures::fx::FxHashMap;
@@ -3245,8 +3246,8 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
32453246
// a::b::c ::d::sym refers to
32463247
// e::f::sym:: ::
32473248
// result should be super::super::super::super::e::f
3248-
if let DefPathData::TypeNs(s) = l {
3249-
path.push(s.to_string());
3249+
if let DefPathData::TypeNs(sym) = l {
3250+
path.push(sym);
32503251
}
32513252
if let DefPathData::TypeNs(_) = r {
32523253
go_up_by += 1;
@@ -3256,7 +3257,7 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
32563257
// a::b::sym:: :: refers to
32573258
// c::d::e ::f::sym
32583259
// when looking at `f`
3259-
Left(DefPathData::TypeNs(sym)) => path.push(sym.to_string()),
3260+
Left(DefPathData::TypeNs(sym)) => path.push(sym),
32603261
// consider:
32613262
// a::b::c ::d::sym refers to
32623263
// e::f::sym:: ::
@@ -3268,17 +3269,17 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
32683269

32693270
if go_up_by > max_super {
32703271
// `super` chain would be too long, just use the absolute path instead
3271-
once(String::from("crate"))
3272-
.chain(to.data.iter().filter_map(|el| {
3272+
join_path_syms(
3273+
once(kw::Crate).chain(to.data.iter().filter_map(|el| {
32733274
if let DefPathData::TypeNs(sym) = el.data {
3274-
Some(sym.to_string())
3275+
Some(sym)
32753276
} else {
32763277
None
32773278
}
32783279
}))
3279-
.join("::")
3280+
)
32803281
} else {
3281-
repeat_n(String::from("super"), go_up_by).chain(path).join("::")
3282+
join_path_syms(repeat_n(kw::Super, go_up_by).chain(path))
32823283
}
32833284
}
32843285

0 commit comments

Comments
 (0)