From 6d78bd5fdc5c5ade539e221d7e91603538199452 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sat, 9 Dec 2017 20:21:34 +0900 Subject: [PATCH 01/13] Add a test for error_on_line_overflow_strings --- .../configs-error_on_line_overflow_strings-false.rs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/target/configs-error_on_line_overflow_strings-false.rs diff --git a/tests/target/configs-error_on_line_overflow_strings-false.rs b/tests/target/configs-error_on_line_overflow_strings-false.rs new file mode 100644 index 00000000000..b5922745cf2 --- /dev/null +++ b/tests/target/configs-error_on_line_overflow_strings-false.rs @@ -0,0 +1,9 @@ +// rustfmt-error_on_line_overflow_strings: false +// Suppress an error on line overflow strings. + +fn main() { + let x = " "; + let a = " + +"; +} From d1e5d7866b3dc39481ba5e83287ba5078963137e Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sat, 9 Dec 2017 20:23:48 +0900 Subject: [PATCH 02/13] Make CharClasses and FullCodeCharKind public --- src/comment.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index 8cd491b6ee3..bf462e38b7a 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -581,7 +581,7 @@ pub fn remove_trailing_white_spaces(text: &str) -> String { buffer } -struct CharClasses +pub struct CharClasses where T: Iterator, T::Item: RichChar, @@ -606,6 +606,12 @@ impl RichChar for (usize, char) { } } +impl RichChar for (char, usize) { + fn get_char(&self) -> char { + self.0 + } +} + #[derive(PartialEq, Eq, Debug, Clone, Copy)] enum CharClassesStatus { Normal, @@ -635,7 +641,7 @@ pub enum CodeCharKind { /// describing opening and closing of comments for ease when chunking /// code from tagged characters #[derive(PartialEq, Eq, Debug, Clone, Copy)] -enum FullCodeCharKind { +pub enum FullCodeCharKind { Normal, /// The first character of a comment, there is only one for a comment (always '/') StartComment, @@ -649,7 +655,7 @@ enum FullCodeCharKind { } impl FullCodeCharKind { - fn is_comment(&self) -> bool { + pub fn is_comment(&self) -> bool { match *self { FullCodeCharKind::StartComment | FullCodeCharKind::InComment @@ -658,6 +664,10 @@ impl FullCodeCharKind { } } + pub fn is_string(&self) -> bool { + *self == FullCodeCharKind::InString + } + fn to_codecharkind(&self) -> CodeCharKind { if self.is_comment() { CodeCharKind::Comment @@ -672,7 +682,7 @@ where T: Iterator, T::Item: RichChar, { - fn new(base: T) -> CharClasses { + pub fn new(base: T) -> CharClasses { CharClasses { base: base.peekable(), status: CharClassesStatus::Normal, From ef6ebaa2155d1c0da71d06f793707d419f56009f Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sat, 9 Dec 2017 20:24:14 +0900 Subject: [PATCH 03/13] Add a config option to suppress error message on string literal --- src/config.rs | 2 ++ src/lib.rs | 83 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6dc1056b5af..d15507f4962 100644 --- a/src/config.rs +++ b/src/config.rs @@ -684,6 +684,8 @@ create_config! { error_on_line_overflow: bool, true, false, "Error if unable to get all lines within max_width"; error_on_line_overflow_comments: bool, true, false, "Error if unable to get comments within max_width"; + error_on_line_overflow_strings: bool, true, false, + "Error if unable to get string letrais within max_width"; report_todo: ReportTactic, ReportTactic::Never, false, "Report all, none or unnumbered occurrences of TODO in source file comments"; report_fixme: ReportTactic, ReportTactic::Never, false, diff --git a/src/lib.rs b/src/lib.rs index a300e961421..0db8925d01d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ use syntax::codemap::{CodeMap, FilePathMapping}; use syntax::parse::{self, ParseSess}; use checkstyle::{output_footer, output_header}; +use comment::{CharClasses, FullCodeCharKind}; use config::Config; use filemap::FileMap; use issues::{BadIssueSeeker, Issue}; @@ -76,6 +77,7 @@ mod patterns; mod summary; mod vertical; +#[derive(Clone, Copy)] pub enum ErrorKind { // Line has exceeded character limit (found, maximum) LineOverflow(usize, usize), @@ -104,6 +106,7 @@ pub struct FormattingError { line: usize, kind: ErrorKind, is_comment: bool, + is_string: bool, line_buffer: String, } @@ -116,12 +119,14 @@ impl FormattingError { } fn msg_suffix(&self) -> &str { - match self.kind { - ErrorKind::LineOverflow(..) if self.is_comment => { - "use `error_on_line_overflow_comments = false` to suppress \ - the warning against line comments\n" - } - _ => "", + if self.is_comment { + "use `error_on_line_overflow_comments = false` to suppress \ + the warning against comments\n" + } else if self.is_string { + "use `error_on_line_overflow_strings = false` to suppress \ + the warning against string literals\n" + } else { + "" } } @@ -363,6 +368,27 @@ fn is_skipped_line(line_number: usize, skipped_range: &[(usize, usize)]) -> bool .any(|&(lo, hi)| lo <= line_number && line_number <= hi) } +fn should_report_error( + config: &Config, + char_kind: FullCodeCharKind, + is_string: bool, + error_kind: ErrorKind, +) -> bool { + let allow_error_report = if char_kind.is_comment() { + config.error_on_line_overflow_comments() + } else if is_string { + config.error_on_line_overflow_strings() + } else { + true + }; + + match error_kind { + ErrorKind::LineOverflow(..) => config.error_on_line_overflow() && allow_error_report, + ErrorKind::TrailingWhitespace => allow_error_report, + _ => true, + } +} + // Formatting done on a char by char or line by line basis. // FIXME(#209) warn on bad license // FIXME(#20) other stuff for parity with make tidy @@ -381,19 +407,17 @@ fn format_lines( 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; let mut line_buffer = String::with_capacity(config.max_width() * 2); + let mut is_string = false; // true if the current line contains a string literal. + let mut format_line = config.file_lines().contains_line(name, cur_line); let mut b = 0; - for c in text.chars() { + for (kind, c) in CharClasses::new(text.chars()) { b += 1; if c == '\r' { continue; } - let format_line = config.file_lines().contains_line(name, cur_line as usize); - if format_line { // Add warnings for bad todos/ fixmes if let Some(issue) = issue_seeker.inspect(c) { @@ -401,6 +425,7 @@ fn format_lines( line: cur_line, kind: ErrorKind::BadIssue(issue), is_comment: false, + is_string: false, line_buffer: String::new(), }); } @@ -409,20 +434,24 @@ fn format_lines( if c == '\n' { if format_line { // Check for (and record) trailing whitespace. - if let Some(lw) = last_wspace { - trims.push((cur_line, lw, b, line_buffer.clone())); + if let Some(..) = last_wspace { + if should_report_error(config, kind, is_string, ErrorKind::TrailingWhitespace) { + trims.push((cur_line, kind, line_buffer.clone())); + } line_len -= 1; } // Check for any line width errors we couldn't correct. - let report_error_on_line_overflow = config.error_on_line_overflow() + let error_kind = ErrorKind::LineOverflow(line_len, config.max_width()); + if line_len > config.max_width() && !is_skipped_line(cur_line, skipped_range) - && (config.error_on_line_overflow_comments() || !is_comment); - if report_error_on_line_overflow && line_len > config.max_width() { + && should_report_error(config, kind, is_string, error_kind) + { errors.push(FormattingError { line: cur_line, - kind: ErrorKind::LineOverflow(line_len, config.max_width()), - is_comment: is_comment, + kind: error_kind, + is_comment: kind.is_comment(), + is_string: is_string, line_buffer: line_buffer.clone(), }); } @@ -430,11 +459,11 @@ fn format_lines( line_len = 0; cur_line += 1; + format_line = config.file_lines().contains_line(name, cur_line); newline_count += 1; last_wspace = None; - prev_char = None; - is_comment = false; line_buffer.clear(); + is_string = false; } else { newline_count = 0; line_len += 1; @@ -442,16 +471,13 @@ fn format_lines( if last_wspace.is_none() { last_wspace = Some(b); } - } else if c == '/' { - if let Some('/') = prev_char { - is_comment = true; - } - last_wspace = None; } else { last_wspace = None; } - prev_char = Some(c); line_buffer.push(c); + if kind.is_string() { + is_string = true; + } } } @@ -461,12 +487,13 @@ fn format_lines( text.truncate(line); } - for &(l, _, _, ref b) in &trims { + for &(l, kind, ref b) in &trims { if !is_skipped_line(l, skipped_range) { errors.push(FormattingError { line: l, kind: ErrorKind::TrailingWhitespace, - is_comment: false, + is_comment: kind.is_comment(), + is_string: kind.is_string(), line_buffer: b.clone(), }); } From 6c3de706aeb17c0d0d35b52e21a3f378b9d06c37 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sat, 9 Dec 2017 23:37:42 +0900 Subject: [PATCH 04/13] Make RichChar public --- src/comment.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/comment.rs b/src/comment.rs index bf462e38b7a..0442a23876a 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -590,7 +590,7 @@ where status: CharClassesStatus, } -trait RichChar { +pub trait RichChar { fn get_char(&self) -> char; } From 4d9226ffeefcd2863c2f300381e6d9067e165dc0 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 10 Dec 2017 18:14:17 +0900 Subject: [PATCH 05/13] Fix a typo --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index d15507f4962..960f0f687d3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -685,7 +685,7 @@ create_config! { error_on_line_overflow_comments: bool, true, false, "Error if unable to get comments within max_width"; error_on_line_overflow_strings: bool, true, false, - "Error if unable to get string letrais within max_width"; + "Error if unable to get string literals within max_width"; report_todo: ReportTactic, ReportTactic::Never, false, "Report all, none or unnumbered occurrences of TODO in source file comments"; report_fixme: ReportTactic, ReportTactic::Never, false, From aea19d5e3347d6ba31306e4ecbf84f883a2442f4 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 09:39:06 +0900 Subject: [PATCH 06/13] Combine error_on_line_overflow_comments/strings 1. Rename to error_on_unformatted_comments_or_strings 2. Set the option to false by default. --- src/config.rs | 7 +++---- src/lib.rs | 18 ++++++------------ ...igs-error_on_line_overflow_comment-false.rs | 7 ------- ...igs-error_on_line_overflow_strings-false.rs | 9 --------- ...on_unformatted_comments_or_strings-false.rs | 12 ++++++++++++ 5 files changed, 21 insertions(+), 32 deletions(-) delete mode 100644 tests/target/configs-error_on_line_overflow_comment-false.rs delete mode 100644 tests/target/configs-error_on_line_overflow_strings-false.rs create mode 100644 tests/target/configs-error_on_unformatted_comments_or_strings-false.rs diff --git a/src/config.rs b/src/config.rs index 960f0f687d3..2c7d32ab648 100644 --- a/src/config.rs +++ b/src/config.rs @@ -682,10 +682,9 @@ create_config! { disable_all_formatting: bool, false, false, "Don't reformat anything"; skip_children: bool, false, false, "Don't reformat out of line modules"; error_on_line_overflow: bool, true, false, "Error if unable to get all lines within max_width"; - error_on_line_overflow_comments: bool, true, false, - "Error if unable to get comments within max_width"; - error_on_line_overflow_strings: bool, true, false, - "Error if unable to get string literals within max_width"; + error_on_unformatted_comments_or_strings: bool, false, false, + "Error if unable to get comments or string literals within max_width, \ + or they are left with trailing whitespaces"; report_todo: ReportTactic, ReportTactic::Never, false, "Report all, none or unnumbered occurrences of TODO in source file comments"; report_fixme: ReportTactic, ReportTactic::Never, false, diff --git a/src/lib.rs b/src/lib.rs index 0db8925d01d..6a67343303f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,12 +119,9 @@ impl FormattingError { } fn msg_suffix(&self) -> &str { - if self.is_comment { - "use `error_on_line_overflow_comments = false` to suppress \ - the warning against comments\n" - } else if self.is_string { - "use `error_on_line_overflow_strings = false` to suppress \ - the warning against string literals\n" + if self.is_comment || self.is_string { + "set `error_on_unformatted_comments_or_strings = false` to suppress \ + the warning against comments or string literals\n" } else { "" } @@ -374,10 +371,8 @@ fn should_report_error( is_string: bool, error_kind: ErrorKind, ) -> bool { - let allow_error_report = if char_kind.is_comment() { - config.error_on_line_overflow_comments() - } else if is_string { - config.error_on_line_overflow_strings() + let allow_error_report = if char_kind.is_comment() || is_string { + config.error_on_unformatted_comments_or_strings() } else { true }; @@ -443,8 +438,7 @@ fn format_lines( // Check for any line width errors we couldn't correct. let error_kind = ErrorKind::LineOverflow(line_len, config.max_width()); - if line_len > config.max_width() - && !is_skipped_line(cur_line, skipped_range) + if line_len > config.max_width() && !is_skipped_line(cur_line, skipped_range) && should_report_error(config, kind, is_string, error_kind) { errors.push(FormattingError { diff --git a/tests/target/configs-error_on_line_overflow_comment-false.rs b/tests/target/configs-error_on_line_overflow_comment-false.rs deleted file mode 100644 index 9fd9e01e274..00000000000 --- a/tests/target/configs-error_on_line_overflow_comment-false.rs +++ /dev/null @@ -1,7 +0,0 @@ -// rustfmt-error_on_line_overflow_comments: false -// Error on line overflow comment - -// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -fn main() { - // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -} diff --git a/tests/target/configs-error_on_line_overflow_strings-false.rs b/tests/target/configs-error_on_line_overflow_strings-false.rs deleted file mode 100644 index b5922745cf2..00000000000 --- a/tests/target/configs-error_on_line_overflow_strings-false.rs +++ /dev/null @@ -1,9 +0,0 @@ -// rustfmt-error_on_line_overflow_strings: false -// Suppress an error on line overflow strings. - -fn main() { - let x = " "; - let a = " - -"; -} diff --git a/tests/target/configs-error_on_unformatted_comments_or_strings-false.rs b/tests/target/configs-error_on_unformatted_comments_or_strings-false.rs new file mode 100644 index 00000000000..ff9a35eb6b8 --- /dev/null +++ b/tests/target/configs-error_on_unformatted_comments_or_strings-false.rs @@ -0,0 +1,12 @@ +// rustfmt-error_on_unformatted_comments_or_strings: false +// Error on line overflow comment or string literals. + +// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +fn main() { + // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + + let x = " "; + let a = " + +"; +} From d17168f4ba004000a560aacd1bb7b9b215c64084 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 09:54:44 +0900 Subject: [PATCH 07/13] Add error-on-unformatted command line option --- src/bin/rustfmt.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 9bf26887881..aea8174f139 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -163,6 +163,12 @@ fn make_opts() -> Options { "config-help", "show details of rustfmt configuration options", ); + opts.optflag( + "", + "error-on-unformatted", + "Error if unable to get comments or string literals within max_width, \ + or they are left with trailing whitespaces", + ); opts.opt( "", "dump-default-config", From cbd3608c30cfdee4954820d8ee1f940a4f1abf08 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 09:55:15 +0900 Subject: [PATCH 08/13] Organize command line options and start with upper case --- src/bin/rustfmt.rs | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index aea8174f139..713d320aa78 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -135,33 +135,20 @@ fn match_cli_path_or_file( fn make_opts() -> Options { let mut opts = Options::new(); - opts.optflag("h", "help", "show this message"); - opts.optflag("V", "version", "show version information"); - opts.optflag("v", "verbose", "print verbose output"); - opts.optopt( - "", - "write-mode", - "how to write output (not usable when piping from stdin)", - "[replace|overwrite|display|plain|diff|coverage|checkstyle]", - ); - opts.optopt( - "", - "color", - "use colored output (if supported)", - "[always|never|auto]", - ); - opts.optflag("", "skip-children", "don't reformat child modules"); + opts.optflag("h", "help", "Show this message"); + opts.optflag("V", "version", "Show version information"); + opts.optflag("v", "verbose", "Print verbose output"); + opts.optflag("", "skip-children", "Don't reformat child modules"); opts.optflag( "", "unstable-features", "Enables unstable features. Only available on nightly channel", ); - opts.optflag( "", "config-help", - "show details of rustfmt configuration options", + "Show details of rustfmt configuration options", ); opts.optflag( "", @@ -169,13 +156,24 @@ fn make_opts() -> Options { "Error if unable to get comments or string literals within max_width, \ or they are left with trailing whitespaces", ); - opts.opt( + + opts.optopt( + "", + "write-mode", + "How to write output (not usable when piping from stdin)", + "[replace|overwrite|display|plain|diff|coverage|checkstyle]", + ); + opts.optopt( + "", + "color", + "Use colored output (if supported)", + "[always|never|auto]", + ); + opts.optopt( "", "dump-default-config", "Dumps default configuration to PATH. PATH defaults to stdout, if omitted.", "PATH", - HasArg::Maybe, - Occur::Optional, ); opts.optopt( "", From 1e982c66a0ba9f04af745bbf1b70bf43d8596403 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 10:00:49 +0900 Subject: [PATCH 09/13] Fix a typo --- src/bin/rustfmt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 713d320aa78..75b45156ace 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -20,7 +20,7 @@ use std::io::{self, Read, Write}; use std::path::{Path, PathBuf}; use std::str::FromStr; -use getopts::{HasArg, Matches, Occur, Options}; +use getopts::{Matches, Options}; use rustfmt::{run, Input, Summary}; use rustfmt::file_lines::FileLines; From 93a75de18ecb8c52b61432f355458ad14e4bba69 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 10:01:00 +0900 Subject: [PATCH 10/13] Print command line options in alphabetical order --- src/bin/rustfmt.rs | 56 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 75b45156ace..4eb1deb2e74 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -136,38 +136,24 @@ fn match_cli_path_or_file( fn make_opts() -> Options { let mut opts = Options::new(); - opts.optflag("h", "help", "Show this message"); - opts.optflag("V", "version", "Show version information"); - opts.optflag("v", "verbose", "Print verbose output"); - opts.optflag("", "skip-children", "Don't reformat child modules"); - opts.optflag( + // Sorted in alphabetical order. + opts.optopt( "", - "unstable-features", - "Enables unstable features. Only available on nightly channel", + "color", + "Use colored output (if supported)", + "[always|never|auto]", ); opts.optflag( "", "config-help", "Show details of rustfmt configuration options", ); - opts.optflag( - "", - "error-on-unformatted", - "Error if unable to get comments or string literals within max_width, \ - or they are left with trailing whitespaces", - ); - opts.optopt( "", - "write-mode", - "How to write output (not usable when piping from stdin)", - "[replace|overwrite|display|plain|diff|coverage|checkstyle]", - ); - opts.optopt( - "", - "color", - "Use colored output (if supported)", - "[always|never|auto]", + "config-path", + "Recursively searches the given path for the rustfmt.toml config file. If not \ + found reverts to the input file path", + "[Path for the configuration file]", ); opts.optopt( "", @@ -181,12 +167,11 @@ fn make_opts() -> Options { "Dumps configuration options that were checked during formatting to a file.", "PATH", ); - opts.optopt( + opts.optflag( "", - "config-path", - "Recursively searches the given path for the rustfmt.toml config file. If not \ - found reverts to the input file path", - "[Path for the configuration file]", + "error-on-unformatted", + "Error if unable to get comments or string literals within max_width, \ + or they are left with trailing whitespaces", ); opts.optopt( "", @@ -194,6 +179,21 @@ fn make_opts() -> Options { "Format specified line ranges. See README for more detail on the JSON format.", "JSON", ); + opts.optflag("h", "help", "Show this message"); + opts.optflag("", "skip-children", "Don't reformat child modules"); + opts.optflag( + "", + "unstable-features", + "Enables unstable features. Only available on nightly channel", + ); + opts.optflag("v", "verbose", "Print verbose output"); + opts.optflag("V", "version", "Show version information"); + opts.optopt( + "", + "write-mode", + "How to write output (not usable when piping from stdin)", + "[replace|overwrite|display|plain|diff|coverage|checkstyle]", + ); opts } From d3ee7f3f06b8636ce3a26bf3c53242975bccc2de Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 10:15:31 +0900 Subject: [PATCH 11/13] Set error_on_unformatted_comments_or_strings to true when --error-on-unformatted option is passed --- src/bin/rustfmt.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 4eb1deb2e74..81be0761aae 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -63,6 +63,7 @@ struct CliOptions { color: Option, file_lines: FileLines, // Default is all lines in all files. unstable_features: bool, + error_on_unformatted: bool, } impl CliOptions { @@ -104,6 +105,10 @@ impl CliOptions { options.file_lines = file_lines.parse()?; } + if matches.opt_present("error-on-unformatted") { + options.error_on_unformatted = true; + } + Ok(options) } @@ -112,6 +117,7 @@ impl CliOptions { config.set().verbose(self.verbose); config.set().file_lines(self.file_lines); config.set().unstable_features(self.unstable_features); + config.set().error_on_unformatted_comments_or_strings(self.error_on_unformatted); if let Some(write_mode) = self.write_mode { config.set().write_mode(write_mode); } From e45c0c48155f742fcb7f13206ed116af4f7d0eb2 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 11:46:04 +0900 Subject: [PATCH 12/13] Rename error_on_unformatted_comments_or_strings to error_on_unformatted --- src/bin/rustfmt.rs | 2 +- src/config.rs | 2 +- src/lib.rs | 4 ++-- ...strings-false.rs => configs-error_on_unformatted-false.rs} | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename tests/target/{configs-error_on_unformatted_comments_or_strings-false.rs => configs-error_on_unformatted-false.rs} (87%) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 81be0761aae..aebd19d8e4b 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -117,7 +117,7 @@ impl CliOptions { config.set().verbose(self.verbose); config.set().file_lines(self.file_lines); config.set().unstable_features(self.unstable_features); - config.set().error_on_unformatted_comments_or_strings(self.error_on_unformatted); + config.set().error_on_unformatted(self.error_on_unformatted); if let Some(write_mode) = self.write_mode { config.set().write_mode(write_mode); } diff --git a/src/config.rs b/src/config.rs index 2c7d32ab648..ed44df022a9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -682,7 +682,7 @@ create_config! { disable_all_formatting: bool, false, false, "Don't reformat anything"; skip_children: bool, false, false, "Don't reformat out of line modules"; error_on_line_overflow: bool, true, false, "Error if unable to get all lines within max_width"; - error_on_unformatted_comments_or_strings: bool, false, false, + error_on_unformatted: bool, false, false, "Error if unable to get comments or string literals within max_width, \ or they are left with trailing whitespaces"; report_todo: ReportTactic, ReportTactic::Never, false, diff --git a/src/lib.rs b/src/lib.rs index 6a67343303f..2c867539881 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,7 +120,7 @@ impl FormattingError { fn msg_suffix(&self) -> &str { if self.is_comment || self.is_string { - "set `error_on_unformatted_comments_or_strings = false` to suppress \ + "set `error_on_unformatted = false` to suppress \ the warning against comments or string literals\n" } else { "" @@ -372,7 +372,7 @@ fn should_report_error( error_kind: ErrorKind, ) -> bool { let allow_error_report = if char_kind.is_comment() || is_string { - config.error_on_unformatted_comments_or_strings() + config.error_on_unformatted() } else { true }; diff --git a/tests/target/configs-error_on_unformatted_comments_or_strings-false.rs b/tests/target/configs-error_on_unformatted-false.rs similarity index 87% rename from tests/target/configs-error_on_unformatted_comments_or_strings-false.rs rename to tests/target/configs-error_on_unformatted-false.rs index ff9a35eb6b8..6a78374e2a2 100644 --- a/tests/target/configs-error_on_unformatted_comments_or_strings-false.rs +++ b/tests/target/configs-error_on_unformatted-false.rs @@ -1,4 +1,4 @@ -// rustfmt-error_on_unformatted_comments_or_strings: false +// rustfmt-error_on_unformatted: false // Error on line overflow comment or string literals. // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa From 3dd31e25bf3285a52dc02cd5ea0c3b344910f52b Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 11 Dec 2017 11:50:11 +0900 Subject: [PATCH 13/13] Use enumerate() --- src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2c867539881..b00caf40cd5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -405,10 +405,8 @@ fn format_lines( let mut line_buffer = String::with_capacity(config.max_width() * 2); let mut is_string = false; // true if the current line contains a string literal. let mut format_line = config.file_lines().contains_line(name, cur_line); - let mut b = 0; - for (kind, c) in CharClasses::new(text.chars()) { - b += 1; + for (kind, (b, c)) in CharClasses::new(text.chars().enumerate()) { if c == '\r' { continue; }