diff --git a/src/config.rs b/src/config.rs index 7b2fb9f629db..fbba5e19490f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -501,6 +501,7 @@ create_config! { via the --file-lines option"; max_width: usize, 100, "Maximum width of each line"; error_on_line_overflow: bool, true, "Error if unable to get all lines within max_width"; + error_on_line_overflow_comments: bool, true, "Error if unable to get comments within max_width"; tab_spaces: usize, 4, "Number of spaces per tab"; fn_call_width: usize, 60, "Maximum width of the args of a function call before falling back to vertical formatting"; diff --git a/src/lib.rs b/src/lib.rs index eaac371aa5b6..555aa66671a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -599,6 +599,8 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m let mut newline_count = 0; let mut errors = vec![]; let mut issue_seeker = BadIssueSeeker::new(config.report_todo(), config.report_fixme()); + let mut prev_char: Option = None; + let mut is_comment = false; for (c, b) in text.chars() { if c == '\r' { @@ -626,7 +628,9 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m } // Check for any line width errors we couldn't correct. - if config.error_on_line_overflow() && line_len > config.max_width() { + let report_error_on_line_overflow = config.error_on_line_overflow() && + (config.error_on_line_overflow_comments() || !is_comment); + if report_error_on_line_overflow && line_len > config.max_width() { errors.push(FormattingError { line: cur_line, kind: ErrorKind::LineOverflow(line_len, config.max_width()), @@ -638,6 +642,8 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m cur_line += 1; newline_count += 1; last_wspace = None; + prev_char = None; + is_comment = false; } else { newline_count = 0; line_len += 1; @@ -645,9 +651,16 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m if last_wspace.is_none() { last_wspace = Some(b); } + } else if c == '/' { + match prev_char { + Some('/') => is_comment = true, + _ => (), + } + last_wspace = None; } else { last_wspace = None; } + prev_char = Some(c); } } diff --git a/tests/target/configs-error_on_line_overflow_comment-false.rs b/tests/target/configs-error_on_line_overflow_comment-false.rs new file mode 100644 index 000000000000..9fd9e01e274f --- /dev/null +++ b/tests/target/configs-error_on_line_overflow_comment-false.rs @@ -0,0 +1,7 @@ +// rustfmt-error_on_line_overflow_comments: false +// Error on line overflow comment + +// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +fn main() { + // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +}