Skip to content

Commit 4bbd5c4

Browse files
authored
Merge pull request #2681 from topecongiro/issue-2680
Error on self_tests when there are lines that exceed max width
2 parents effba71 + 51c07f4 commit 4bbd5c4

File tree

7 files changed

+43
-22
lines changed

7 files changed

+43
-22
lines changed

rustfmt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error_on_line_overflow = true
2+
error_on_unformatted = true

src/config/config_type.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,9 @@ macro_rules! create_config {
322322
///
323323
/// Returns the `Config` to use, and the path of the project file if there was
324324
/// one.
325-
pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), Error> {
326-
325+
pub(super) fn from_resolved_toml_path(
326+
dir: &Path,
327+
) -> Result<(Config, Option<PathBuf>), Error> {
327328
/// Try to find a project file in the given directory and its parents.
328329
/// Returns the path of a the nearest project file if one exists,
329330
/// or `None` if no project file was found.

src/config/mod.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ create_config! {
5050
comment_width: usize, 80, false,
5151
"Maximum length of comments. No effect unless wrap_comments = true";
5252
normalize_comments: bool, false, true, "Convert /* */ comments to // comments where possible";
53-
license_template_path: String, String::default(), false, "Beginning of file must match license template";
53+
license_template_path: String, String::default(), false,
54+
"Beginning of file must match license template";
5455
format_strings: bool, false, false, "Format string literals where necessary";
5556

5657
// Single line expressions and items
@@ -239,16 +240,21 @@ mod test {
239240
create_config! {
240241
// Options that are used by the generated functions
241242
max_width: usize, 100, true, "Maximum width of each line";
242-
use_small_heuristics: bool, true, false, "Whether to use different formatting for items and \
243-
expressions if they satisfy a heuristic notion of 'small'.";
244-
license_template_path: String, String::default(), false, "Beginning of file must match license template";
245-
required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false, "Require a specific version of rustfmt.";
246-
ignore: IgnoreList, IgnoreList::default(), false, "Skip formatting the specified files and directories.";
243+
use_small_heuristics: bool, true, false,
244+
"Whether to use different formatting for items and \
245+
expressions if they satisfy a heuristic notion of 'small'.";
246+
license_template_path: String, String::default(), false,
247+
"Beginning of file must match license template";
248+
required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false,
249+
"Require a specific version of rustfmt.";
250+
ignore: IgnoreList, IgnoreList::default(), false,
251+
"Skip formatting the specified files and directories.";
247252
verbose: bool, false, false, "Use verbose output";
248253
file_lines: FileLines, FileLines::all(), false,
249254
"Lines to format; this is not supported in rustfmt.toml, and can only be specified \
250255
via the --file-lines option";
251-
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false, "'small' heuristic values";
256+
width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false,
257+
"'small' heuristic values";
252258

253259
// Options that are used by the tests
254260
stable_option: bool, false, true, "A stable option";

src/config/options.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ configuration_option_enum! { WriteMode:
191191
// Output the changed lines (for internal value only)
192192
Modified,
193193
// Checks if a diff can be generated. If so, rustfmt outputs a diff and quits with exit code 1.
194-
// This option is designed to be run in CI where a non-zero exit signifies non-standard code formatting.
194+
// This option is designed to be run in CI where a non-zero exit signifies non-standard code
195+
// formatting.
195196
Check,
196197
// Rustfmt shouldn't output anything formatting-like (e.g., emit a help message).
197198
None,

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ pub(crate) type FileRecord = (FileName, String);
116116
pub enum ErrorKind {
117117
// Line has exceeded character limit (found, maximum)
118118
#[fail(
119-
display = "line formatted, but exceeded maximum width (maximum: {} (see `max_width` option), found: {})",
119+
display = "line formatted, but exceeded maximum width \
120+
(maximum: {} (see `max_width` option), found: {})",
120121
_0,
121122
_1
122123
)]

src/macros.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,9 @@ impl MacroBranch {
12971297
fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream) -> Option<String> {
12981298
let mut result = String::with_capacity(1024);
12991299
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
1300-
let nested_shape = shape.block_indent(context.config.tab_spaces());
1300+
let nested_shape = shape
1301+
.block_indent(context.config.tab_spaces())
1302+
.with_max_width(context.config);
13011303

13021304
result.push_str("lazy_static! {");
13031305
result.push_str(&nested_shape.indent.to_string_with_newline(context.config));

src/test/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn write_message(msg: &str) {
111111
fn system_tests() {
112112
// Get all files in the tests/source directory.
113113
let files = get_test_files(Path::new("tests/source"), true);
114-
let (_reports, count, fails) = check_files(files);
114+
let (_reports, count, fails) = check_files(files, None);
115115

116116
// Display results.
117117
println!("Ran {} system tests.", count);
@@ -123,7 +123,7 @@ fn system_tests() {
123123
#[test]
124124
fn coverage_tests() {
125125
let files = get_test_files(Path::new("tests/coverage/source"), true);
126-
let (_reports, count, fails) = check_files(files);
126+
let (_reports, count, fails) = check_files(files, None);
127127

128128
println!("Ran {} tests in coverage mode.", count);
129129
assert_eq!(fails, 0, "{} tests failed", fails);
@@ -192,7 +192,7 @@ fn assert_output(source: &Path, expected_filename: &Path) {
192192
fn idempotence_tests() {
193193
// Get all files in the tests/target directory.
194194
let files = get_test_files(Path::new("tests/target"), true);
195-
let (_reports, count, fails) = check_files(files);
195+
let (_reports, count, fails) = check_files(files, None);
196196

197197
// Display results.
198198
println!("Ran {} idempotent tests.", count);
@@ -213,7 +213,7 @@ fn self_tests() {
213213
}
214214
files.push(PathBuf::from("src/lib.rs"));
215215

216-
let (reports, count, fails) = check_files(files);
216+
let (reports, count, fails) = check_files(files, Some(PathBuf::from("rustfmt.toml")));
217217
let mut warnings = 0;
218218

219219
// Display results.
@@ -298,15 +298,15 @@ fn format_lines_errors_are_reported_with_tabs() {
298298

299299
// For each file, run rustfmt and collect the output.
300300
// Returns the number of files checked and the number of failures.
301-
fn check_files(files: Vec<PathBuf>) -> (Vec<FormatReport>, u32, u32) {
301+
fn check_files(files: Vec<PathBuf>, opt_config: Option<PathBuf>) -> (Vec<FormatReport>, u32, u32) {
302302
let mut count = 0;
303303
let mut fails = 0;
304304
let mut reports = vec![];
305305

306306
for file_name in files {
307307
debug!("Testing '{}'...", file_name.display());
308308

309-
match idempotent_check(&file_name) {
309+
match idempotent_check(&file_name, &opt_config) {
310310
Ok(ref report) if report.has_warnings() => {
311311
print!("{}", report);
312312
fails += 1;
@@ -385,9 +385,16 @@ pub enum IdempotentCheckError {
385385
Parse,
386386
}
387387

388-
pub fn idempotent_check(filename: &PathBuf) -> Result<FormatReport, IdempotentCheckError> {
388+
pub fn idempotent_check(
389+
filename: &PathBuf,
390+
opt_config: &Option<PathBuf>,
391+
) -> Result<FormatReport, IdempotentCheckError> {
389392
let sig_comments = read_significant_comments(filename);
390-
let config = read_config(filename);
393+
let config = if let Some(ref config_file_path) = opt_config {
394+
Config::from_toml_path(config_file_path).expect("rustfmt.toml not found")
395+
} else {
396+
read_config(filename)
397+
};
391398
let (error_summary, file_map, format_report) = format_file(filename, &config);
392399
if error_summary.has_parsing_errors() {
393400
return Err(IdempotentCheckError::Parse);
@@ -611,8 +618,9 @@ impl ConfigurationSection {
611618
lazy_static! {
612619
static ref CONFIG_NAME_REGEX: regex::Regex =
613620
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
614-
static ref CONFIG_VALUE_REGEX: regex::Regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
615-
.expect("Failed creating configuration value pattern");
621+
static ref CONFIG_VALUE_REGEX: regex::Regex =
622+
regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
623+
.expect("Failed creating configuration value pattern");
616624
}
617625

618626
loop {

0 commit comments

Comments
 (0)