Skip to content

Commit 89f1e2b

Browse files
committed
rust-lang/style-team#189: rhs-should-use-indent-of-last-line-of-lhs
1 parent 1a70f40 commit 89f1e2b

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

Diff for: src/expr.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ use std::cmp::min;
33

44
use itertools::Itertools;
55
use rustc_ast::token::{Delimiter, Lit, LitKind};
6-
use rustc_ast::{ForLoopKind, MatchKind, ast, ptr, token};
6+
use rustc_ast::{ast, ptr, token, ForLoopKind, MatchKind};
77
use rustc_span::{BytePos, Span};
88

99
use crate::chains::rewrite_chain;
1010
use crate::closures;
1111
use crate::comment::{
12-
CharClasses, FindUncommented, combine_strs_with_missing_comments, contains_comment,
13-
recover_comment_removed, rewrite_comment, rewrite_missing_comment,
12+
combine_strs_with_missing_comments, contains_comment, recover_comment_removed, rewrite_comment,
13+
rewrite_missing_comment, CharClasses, FindUncommented,
1414
};
1515
use crate::config::lists::*;
1616
use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, StyleEdition};
1717
use crate::lists::{
18-
ListFormatting, Separator, definitive_tactic, itemize_list, shape_for_tactic,
19-
struct_lit_formatting, struct_lit_shape, struct_lit_tactic, write_list,
18+
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
19+
struct_lit_tactic, write_list, ListFormatting, Separator,
2020
};
21-
use crate::macros::{MacroPosition, rewrite_macro};
21+
use crate::macros::{rewrite_macro, MacroPosition};
2222
use crate::matches::rewrite_match;
2323
use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
24-
use crate::pairs::{PairParts, rewrite_all_pairs, rewrite_pair};
24+
use crate::pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
2525
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
2626
use crate::shape::{Indent, Shape};
2727
use crate::source_map::{LineRangeUtils, SpanUtils};
2828
use crate::spanned::Spanned;
2929
use crate::stmt;
30-
use crate::string::{StringFormat, rewrite_string};
31-
use crate::types::{PathContext, rewrite_path};
30+
use crate::string::{rewrite_string, StringFormat};
31+
use crate::types::{rewrite_path, PathContext};
3232
use crate::utils::{
3333
colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
3434
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
@@ -2124,6 +2124,25 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
21242124
rhs_kind: &RhsAssignKind<'_>,
21252125
rhs_tactics: RhsTactics,
21262126
) -> RewriteResult {
2127+
let get_lhs_last_line_shape = || {
2128+
let last_line = lhs.rsplitn(2, "\n").next().unwrap_or_default();
2129+
let tab_spaces = context.config.tab_spaces();
2130+
let new_shape = shape
2131+
.block_indent(tab_spaces)
2132+
.saturating_sub_width(tab_spaces);
2133+
let extra_indent_string = new_shape.to_string(&context.config).to_string();
2134+
if last_line.starts_with(&extra_indent_string) {
2135+
new_shape
2136+
} else {
2137+
shape
2138+
}
2139+
};
2140+
let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
2141+
get_lhs_last_line_shape()
2142+
} else {
2143+
shape
2144+
};
2145+
21272146
let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
21282147
shape.indent.width()
21292148
} else {

Diff for: src/shape.rs

+6
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ impl Shape {
278278
offset_indent.to_string_inner(config, 0)
279279
}
280280

281+
pub(crate) fn to_string(&self, config: &Config) -> Cow<'static, str> {
282+
let mut offset_indent = self.indent;
283+
offset_indent.alignment = self.offset;
284+
offset_indent.to_string_inner(config, 1)
285+
}
286+
281287
/// Creates a `Shape` with a virtually infinite width.
282288
pub(crate) fn infinite_width(&self) -> Shape {
283289
Shape {

Diff for: tests/source/rust-lang/style-team#189.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// rustfmt-style_edition: 2024
2+
3+
impl SomeType {
4+
fn method(&mut self) {
5+
self.array[array_index as usize]
6+
.as_mut()
7+
.expect("thing must exist")
8+
.extra_info = Some(ExtraInfo {
9+
parent,
10+
count: count as u16,
11+
children: children.into_boxed_slice(),
12+
});
13+
}
14+
}
15+
16+
impl SomeType {
17+
fn method(&mut self) {
18+
self.array[array_index as usize]
19+
.as_mut()
20+
.expect("thing must exist")
21+
.extra_info =
22+
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
23+
}
24+
}

Diff for: tests/target/rust-lang/style-team#189.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// rustfmt-style_edition: 2024
2+
3+
impl SomeType {
4+
fn method(&mut self) {
5+
self.array[array_index as usize]
6+
.as_mut()
7+
.expect("thing must exist")
8+
.extra_info = Some(ExtraInfo {
9+
parent,
10+
count: count as u16,
11+
children: children.into_boxed_slice(),
12+
});
13+
}
14+
}
15+
16+
impl SomeType {
17+
fn method(&mut self) {
18+
self.array[array_index as usize]
19+
.as_mut()
20+
.expect("thing must exist")
21+
.extra_info =
22+
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
23+
}
24+
}

0 commit comments

Comments
 (0)