Skip to content

Commit e1a0d82

Browse files
authored
Merge pull request #1869 from topecongiro/configs-error_on_line_overflow_comments
Add error_on_line_overflow_comments config option
2 parents 62689ef + e69a2ab commit e1a0d82

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ create_config! {
501501
via the --file-lines option";
502502
max_width: usize, 100, "Maximum width of each line";
503503
error_on_line_overflow: bool, true, "Error if unable to get all lines within max_width";
504+
error_on_line_overflow_comments: bool, true, "Error if unable to get comments within max_width";
504505
tab_spaces: usize, 4, "Number of spaces per tab";
505506
fn_call_width: usize, 60,
506507
"Maximum width of the args of a function call before falling back to vertical formatting";

src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
599599
let mut newline_count = 0;
600600
let mut errors = vec![];
601601
let mut issue_seeker = BadIssueSeeker::new(config.report_todo(), config.report_fixme());
602+
let mut prev_char: Option<char> = None;
603+
let mut is_comment = false;
602604

603605
for (c, b) in text.chars() {
604606
if c == '\r' {
@@ -626,7 +628,9 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
626628
}
627629

628630
// Check for any line width errors we couldn't correct.
629-
if config.error_on_line_overflow() && line_len > config.max_width() {
631+
let report_error_on_line_overflow = config.error_on_line_overflow() &&
632+
(config.error_on_line_overflow_comments() || !is_comment);
633+
if report_error_on_line_overflow && line_len > config.max_width() {
630634
errors.push(FormattingError {
631635
line: cur_line,
632636
kind: ErrorKind::LineOverflow(line_len, config.max_width()),
@@ -638,16 +642,25 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
638642
cur_line += 1;
639643
newline_count += 1;
640644
last_wspace = None;
645+
prev_char = None;
646+
is_comment = false;
641647
} else {
642648
newline_count = 0;
643649
line_len += 1;
644650
if c.is_whitespace() {
645651
if last_wspace.is_none() {
646652
last_wspace = Some(b);
647653
}
654+
} else if c == '/' {
655+
match prev_char {
656+
Some('/') => is_comment = true,
657+
_ => (),
658+
}
659+
last_wspace = None;
648660
} else {
649661
last_wspace = None;
650662
}
663+
prev_char = Some(c);
651664
}
652665
}
653666

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-error_on_line_overflow_comments: false
2+
// Error on line overflow comment
3+
4+
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
5+
fn main() {
6+
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
7+
}

0 commit comments

Comments
 (0)