Skip to content

Commit 3e7602f

Browse files
committed
Simplify rewrite_explicit_self
1 parent bc7aa64 commit 3e7602f

File tree

1 file changed

+31
-67
lines changed

1 file changed

+31
-67
lines changed

src/tools/rustfmt/src/items.rs

+31-67
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,21 @@ impl Rewrite for ast::Param {
23592359
}
23602360
}
23612361

2362+
fn rewrite_opt_lifetime(
2363+
context: &RewriteContext<'_>,
2364+
lifetime: Option<ast::Lifetime>,
2365+
) -> RewriteResult {
2366+
let Some(l) = lifetime else {
2367+
return Ok(String::new());
2368+
};
2369+
let mut result = l.rewrite_result(
2370+
context,
2371+
Shape::legacy(context.config.max_width(), Indent::empty()),
2372+
)?;
2373+
result.push(' ');
2374+
Ok(result)
2375+
}
2376+
23622377
fn rewrite_explicit_self(
23632378
context: &RewriteContext<'_>,
23642379
explicit_self: &ast::ExplicitSelf,
@@ -2367,85 +2382,34 @@ fn rewrite_explicit_self(
23672382
shape: Shape,
23682383
has_multiple_attr_lines: bool,
23692384
) -> RewriteResult {
2370-
match explicit_self.node {
2385+
let self_str = match explicit_self.node {
23712386
ast::SelfKind::Region(lt, m) => {
23722387
let mut_str = format_mutability(m);
2373-
match lt {
2374-
Some(ref l) => {
2375-
let lifetime_str = l.rewrite_result(
2376-
context,
2377-
Shape::legacy(context.config.max_width(), Indent::empty()),
2378-
)?;
2379-
Ok(combine_strs_with_missing_comments(
2380-
context,
2381-
param_attrs,
2382-
&format!("&{lifetime_str} {mut_str}self"),
2383-
span,
2384-
shape,
2385-
!has_multiple_attr_lines,
2386-
)?)
2387-
}
2388-
None => Ok(combine_strs_with_missing_comments(
2389-
context,
2390-
param_attrs,
2391-
&format!("&{mut_str}self"),
2392-
span,
2393-
shape,
2394-
!has_multiple_attr_lines,
2395-
)?),
2396-
}
2388+
let lifetime_str = rewrite_opt_lifetime(context, lt)?;
2389+
format!("&{lifetime_str}{mut_str}self")
23972390
}
23982391
ast::SelfKind::Pinned(lt, m) => {
23992392
let mut_str = m.ptr_str();
2400-
match lt {
2401-
Some(ref l) => {
2402-
let lifetime_str = l.rewrite_result(
2403-
context,
2404-
Shape::legacy(context.config.max_width(), Indent::empty()),
2405-
)?;
2406-
Ok(combine_strs_with_missing_comments(
2407-
context,
2408-
param_attrs,
2409-
&format!("&{lifetime_str} pin {mut_str} self"),
2410-
span,
2411-
shape,
2412-
!has_multiple_attr_lines,
2413-
)?)
2414-
}
2415-
None => Ok(combine_strs_with_missing_comments(
2416-
context,
2417-
param_attrs,
2418-
&format!("&pin {mut_str} self"),
2419-
span,
2420-
shape,
2421-
!has_multiple_attr_lines,
2422-
)?),
2423-
}
2393+
let lifetime_str = rewrite_opt_lifetime(context, lt)?;
2394+
format!("&{lifetime_str}pin {mut_str} self")
24242395
}
24252396
ast::SelfKind::Explicit(ref ty, mutability) => {
24262397
let type_str = ty.rewrite_result(
24272398
context,
24282399
Shape::legacy(context.config.max_width(), Indent::empty()),
24292400
)?;
2430-
2431-
Ok(combine_strs_with_missing_comments(
2432-
context,
2433-
param_attrs,
2434-
&format!("{}self: {}", format_mutability(mutability), type_str),
2435-
span,
2436-
shape,
2437-
!has_multiple_attr_lines,
2438-
)?)
2401+
format!("{}self: {}", format_mutability(mutability), type_str)
24392402
}
2440-
ast::SelfKind::Value(mutability) => Ok(combine_strs_with_missing_comments(
2441-
context,
2442-
param_attrs,
2443-
&format!("{}self", format_mutability(mutability)),
2444-
span,
2445-
shape,
2446-
!has_multiple_attr_lines,
2447-
)?),
2448-
}
2403+
ast::SelfKind::Value(mutability) => format!("{}self", format_mutability(mutability)),
2404+
};
2405+
Ok(combine_strs_with_missing_comments(
2406+
context,
2407+
param_attrs,
2408+
&self_str,
2409+
span,
2410+
shape,
2411+
!has_multiple_attr_lines,
2412+
)?)
24492413
}
24502414

24512415
pub(crate) fn span_lo_for_param(param: &ast::Param) -> BytePos {

0 commit comments

Comments
 (0)