Skip to content

Add error_on_line_overflow_comments config option #1869

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

Merged
Merged
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
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> = None;
let mut is_comment = false;

for (c, b) in text.chars() {
if c == '\r' {
Expand Down Expand Up @@ -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()),
Expand All @@ -638,16 +642,25 @@ 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;
if c.is_whitespace() {
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);
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/target/configs-error_on_line_overflow_comment-false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-error_on_line_overflow_comments: false
// Error on line overflow comment

// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
fn main() {
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
}