Skip to content

Commit 4c39e5a

Browse files
committed
Add fields to FormattingError
is_comment is set to true if the error happened inside comment. line_buffer is the line which overflowed.
1 parent 222ae15 commit 4c39e5a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,10 @@ impl fmt::Display for ErrorKind {
468468

469469
// Formatting errors that are identified *after* rustfmt has run.
470470
pub struct FormattingError {
471-
line: u32,
471+
line: usize,
472472
kind: ErrorKind,
473+
is_comment: bool,
474+
line_buffer: String,
473475
}
474476

475477
impl FormattingError {
@@ -600,6 +602,7 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
600602
let mut issue_seeker = BadIssueSeeker::new(config.report_todo(), config.report_fixme());
601603
let mut prev_char: Option<char> = None;
602604
let mut is_comment = false;
605+
let mut line_buffer = String::with_capacity(config.max_width() * 2);
603606

604607
for (c, b) in text.chars() {
605608
if c == '\r' {
@@ -614,6 +617,8 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
614617
errors.push(FormattingError {
615618
line: cur_line,
616619
kind: ErrorKind::BadIssue(issue),
620+
is_comment: false,
621+
line_buffer: String::new(),
617622
});
618623
}
619624
}
@@ -622,7 +627,7 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
622627
if format_line {
623628
// Check for (and record) trailing whitespace.
624629
if let Some(lw) = last_wspace {
625-
trims.push((cur_line, lw, b));
630+
trims.push((cur_line, lw, b, line_buffer.clone()));
626631
line_len -= 1;
627632
}
628633

@@ -633,6 +638,8 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
633638
errors.push(FormattingError {
634639
line: cur_line,
635640
kind: ErrorKind::LineOverflow(line_len, config.max_width()),
641+
is_comment: is_comment,
642+
line_buffer: line_buffer.clone(),
636643
});
637644
}
638645
}
@@ -643,6 +650,7 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
643650
last_wspace = None;
644651
prev_char = None;
645652
is_comment = false;
653+
line_buffer.clear();
646654
} else {
647655
newline_count = 0;
648656
line_len += 1;
@@ -660,6 +668,7 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
660668
last_wspace = None;
661669
}
662670
prev_char = Some(c);
671+
line_buffer.push(c);
663672
}
664673
}
665674

@@ -669,10 +678,12 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
669678
text.truncate(line);
670679
}
671680

672-
for &(l, _, _) in &trims {
681+
for &(l, _, _, ref b) in &trims {
673682
errors.push(FormattingError {
674683
line: l,
675684
kind: ErrorKind::TrailingWhitespace,
685+
is_comment: false,
686+
line_buffer: b.clone(),
676687
});
677688
}
678689

0 commit comments

Comments
 (0)