Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 11 additions & 2 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl ListItem {
}

fn empty_result(s: &RewriteResult) -> bool {
!matches!(*s, Ok(ref s) if !s.is_empty())
!matches!(*s, Ok(ref s) | Err(RewriteError::InlineComment(ref s)) if !s.is_empty())
}

!(empty(&self.pre_comment) && empty_result(&self.item) && empty(&self.post_comment))
Expand Down Expand Up @@ -285,7 +285,16 @@ where
let indent_str = &formatting.shape.indent.to_string(formatting.config);
while let Some((i, item)) = iter.next() {
let item = item.as_ref();
let inner_item = item.item.as_ref().or_else(|err| Err(err.clone()))?;
let inner_item = match item.item.as_ref() {
Ok(item) => item,
Err(RewriteError::InlineComment(item)) => {
// Keep the original contents and recover, allowing the subsequent items to be
// formatted while keeping the original formatting of this item.
// This is currently only relevant to or-patterns with comments in between patterns.
item
}
Err(err) => return Err(err.clone()),
};
let first = i == 0;
let last = iter.peek().is_none();
let mut separate = match sep_place {
Expand Down
20 changes: 16 additions & 4 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use rustc_ast::{MatchKind, ast};
use rustc_span::{BytePos, Span};
use tracing::debug;

use crate::comment::{FindUncommented, combine_strs_with_missing_comments, rewrite_comment};
use crate::comment::{
FindUncommented, combine_strs_with_missing_comments, recover_comment_removed, rewrite_comment,
};
use crate::config::lists::*;
use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, StyleEdition};
use crate::expr::{
Expand Down Expand Up @@ -293,7 +295,13 @@ fn rewrite_match_arm(
.offset_left(pipe_offset, arm.span)?
}
};
let pats_str = arm.pat.rewrite_result(context, pat_shape)?;
let pats_str_rewritten = arm.pat.rewrite_result(context, pat_shape)?;
// FIXME: if the original pattern span is multiline, then we should at least try to reindent
// it, but for now we leave it as it was before.
let pats_str = recover_comment_removed(pats_str_rewritten.clone(), arm.pat.span, context);
// We will continue formatting all other elements of of the `match`, but will still signal the
// error.
let inline_comment = pats_str != pats_str_rewritten;

// Guard
let block_like_pat = trimmed_last_line_width(&pats_str) <= context.config.tab_spaces();
Expand All @@ -319,15 +327,19 @@ fn rewrite_match_arm(
arm.pat.span.hi(),
arm.body.as_ref().unknown_error()?.span().lo(),
);
rewrite_match_body(
let body = rewrite_match_body(
context,
arm.body.as_ref().unknown_error()?,
&lhs_str,
shape,
guard_str.contains('\n'),
arrow_span,
is_last,
)
);
match (body, inline_comment) {
(Ok(body), true) => Err(RewriteError::InlineComment(body)),
(body, _) => body,
}
}

fn stmt_is_expr_mac(stmt: &ast::Stmt) -> bool {
Expand Down
7 changes: 7 additions & 0 deletions src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ pub(crate) enum RewriteError {
#[error("Failed to format given macro{} at: {span:?}", kind)]
MacroFailure { kind: MacroErrorKind, span: Span },

/// This failure will normally happen when an or-pattern has comments in between its elements.
///
/// We will keep the original code, report an error, but recover and format code that comes
/// later properly.
#[error("Formatting was skipped due to a comment that would have been removed.")]
InlineComment(String),

/// Format failure that does not fit to above categories.
#[error("An unknown error occurred during formatting.")]
Unknown,
Expand Down
13 changes: 13 additions & 0 deletions tests/source/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,16 @@ unsafe {}
}
}
}

fn issue_4119() {
match () {
()=> {}
() // Comment
| () => {
{
println!("Foo");
}
}
()=> {}
}
}
11 changes: 11 additions & 0 deletions tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,14 @@ fn issue_4109() {
}
}
}

fn issue_4119() {
match () {
() => {}
() // Comment
| () => {
println!("Foo");
Copy link
Copy Markdown
Contributor Author

@estebank estebank May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This highlights the outstanding problem with this approach: the pattern is kept with bad formatting, where this should instead have been indented to the proper level.

View changes since the review

}
() => {}
}
}
Loading