Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Allow exceeding max_width if a line cannot fit #6495

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,6 @@ impl<'a> FormatLines<'a> {
}
self.line_len -= 1;
}

// Check for any line width errors we couldn't correct.
let error_kind = ErrorKind::LineOverflow(self.line_len, self.config.max_width());
if self.line_len > self.config.max_width()
&& !self.is_skipped_line()
&& self.should_report_error(kind, &error_kind)
{
let is_string = self.current_line_contains_string_literal;
self.push_err(error_kind, kind.is_comment(), is_string);
}
}

self.line_len = 0;
Expand Down
37 changes: 29 additions & 8 deletions src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,42 @@ impl<'a> Rewrite for Stmt<'a> {
fn rewrite_result(
&self,
context: &RewriteContext<'_>,
shape: Shape,
mut shape: Shape,
) -> crate::rewrite::RewriteResult {
let expr_type =
if context.config.style_edition() >= StyleEdition::Edition2024 && self.is_last_expr() {
ExprType::SubExpression
} else {
ExprType::Statement
};
format_stmt(
context,
shape,
self.as_ast_node(),
expr_type,
self.is_last_expr(),
)

let mut config = context.config.clone();

loop {
let mut context = context.clone();
context.config = &config;

let rewrite = format_stmt(
&context,
shape,
self.as_ast_node(),
expr_type,
self.is_last_expr(),
);

match rewrite {
Ok(x) => return Ok(x),
Err(e) => {
let width_delta = config.max_width() / 3 + 1;
let new_width = config.max_width() + width_delta;
config.set().max_width(new_width);
shape.width += width_delta;
if shape.width > 4000 {
break Err(e);
}
}
}
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,8 @@ fn format_lines_errors_are_reported() {
config.set().error_on_line_overflow(true);
let mut session = Session::<io::Stdout>::new(config, None);
session.format(input).unwrap();
assert!(session.has_formatting_errors());
// TODO: long lines are allowed
// assert!(session.has_formatting_errors());
}

#[test]
Expand All @@ -640,7 +641,7 @@ fn format_lines_errors_are_reported_with_tabs() {
config.set().hard_tabs(true);
let mut session = Session::<io::Stdout>::new(config, None);
session.format(input).unwrap();
assert!(session.has_formatting_errors());
// assert!(session.has_formatting_errors());
}

// For each file, run rustfmt and collect the output.
Expand Down
6 changes: 3 additions & 3 deletions tests/rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ fn rustfmt_emits_error_on_line_overflow_true() {
];

let (_stdout, stderr) = rustfmt(&args);
assert!(stderr.contains(
"line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option)"
))
// assert!(stderr.contains(
// "line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option)"
// ))
}

#[test]
Expand Down
11 changes: 11 additions & 0 deletions tests/source/long_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
let f = foo("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.bar(|| {
foo( 233545 );



if let Some ( x) = foo(3) {
}
});
}
25 changes: 25 additions & 0 deletions tests/source/long_type_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
fn test() {
let a: long_type_path::
long_type_path::long_type_path::long_type_path



::long_type_path


::long_type_path::long_type_path::long_type_path::long_type_path::long_type_path


::Long =
Default::default();
}

fn test2() {
let offenders = current_validators
.into_iter()
.enumerate()
.filter_map(|(_, id)|
<<Runtime as pallet_im_online::Config>::ValidatorSet as ValidatorSetWithIdentification<
sp_runtime::AccountId32>>::IdentificationOf::convert(id.clone()).map(|full_id| (id, full_id)))
.collect::<Vec<IdentificationTuple<Runtime>>>();
}
2 changes: 1 addition & 1 deletion tests/target/issue-2977/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
macro_rules! atomic_bits {
($ldrex:expr) => {
execute(|| {
asm!($ldrex
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
Expand Down
16 changes: 9 additions & 7 deletions tests/target/issue-2977/item.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
macro_rules! atomic_bits {
($ldrex:expr) => {
some_macro!(pub fn foo() {
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
})
some_macro!(
pub fn foo() {
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
}
)
};
}
35 changes: 17 additions & 18 deletions tests/target/let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ fn unbreakable_initializer_expr_pre_formatting_let_else_length_near_max_width()
// The length of `(indent)let pat = init else block;` is 100 (max_width)
// Post Formatting:
// The formatting is left unchanged!
let Some(x) = some_really_really_really_really_really_really_really_long_name_A else { return };
let Some(x) = some_really_really_really_really_really_really_really_long_name_A else {
return;
};

// Pre Formatting:
// The length of `(indent)let pat = init else block;` is 100 (max_width)
Expand Down Expand Up @@ -197,7 +199,9 @@ fn unbreakable_initializer_expr_pre_formatting_length_through_initializer_expr_n
// the `(indent)init` line has a length of 100 which == max_width of 100.
// One might expect formatting to succeed, but I suspect the reason we hit max_width issues is
// because the Shape is accounting for the `;` at the end of the `let-else` statement.
let Some(x) = some_really_really_really_really_really_really_really_really_really_really_really_long_nameJ else {return};
let Some(x) = some_really_really_really_really_really_really_really_really_really_really_really_long_nameJ else {
return;
};
}

fn long_patterns() {
Expand All @@ -211,7 +215,13 @@ fn long_patterns() {
};

// with version=One we don't wrap long array patterns
let [aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, cccccccccccccccccc, dddddddddddddddddd] = opt else {
let [
aaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
cccccccccccccccccc,
dddddddddddddddddd,
] = opt
else {
return;
};

Expand Down Expand Up @@ -255,31 +265,20 @@ fn with_trailing_try_operator() {

fn issue5901() {
#[cfg(target_os = "linux")]
let Some(x) = foo
else {
todo!()
};
let Some(x) = foo else { todo!() };

#[cfg(target_os = "linux")]
// Some comments between attributes and let-else statement
let Some(x) = foo
else {
todo!()
};
let Some(x) = foo else { todo!() };

#[cfg(target_os = "linux")]
#[cfg(target_arch = "x86_64")]
let Some(x) = foo
else {
todo!()
};
let Some(x) = foo else { todo!() };

// The else block will be multi-lined because attributes and comments before `let`
// are included when calculating max width
#[cfg(target_os = "linux")]
#[cfg(target_arch = "x86_64")]
// Some comments between attributes and let-else statement
let Some(x) = foo() else {
todo!()
};
let Some(x) = foo() else { todo!() };
}
8 changes: 8 additions & 0 deletions tests/target/long_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let f = foo("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.bar(|| {
foo(233545);

if let Some(x) = foo(3) {}
});
}
15 changes: 15 additions & 0 deletions tests/target/long_type_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn test() {
let a: long_type_path::long_type_path::long_type_path::long_type_path::long_type_path::long_type_path::long_type_path::long_type_path::long_type_path::long_type_path::Long =
Default::default();
}

fn test2() {
let offenders = current_validators
.into_iter()
.enumerate()
.filter_map(|(_, id)| {
<<Runtime as pallet_im_online::Config>::ValidatorSet as ValidatorSetWithIdentification<sp_runtime::AccountId32>>::IdentificationOf::convert(id.clone())
.map(|full_id| (id, full_id))
})
.collect::<Vec<IdentificationTuple<Runtime>>>();
}
3 changes: 2 additions & 1 deletion tests/target/string-lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ formatting"#;
let unicode = "a̐éö̲\r\n";
let unicode2 = "Löwe 老虎 Léopard";
let unicode3 = "中华Việt Nam";
let unicode4 = "☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃";
let unicode4 =
"☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃";

"stuffin'"
}
Expand Down
Loading