Skip to content

Commit 5e73127

Browse files
authored
Let empty files be empty files (#6839)
* Empty files don't need trailing newlines * Move reorder_modules 2027 tests into reorder_modules_2027 * Rename test: empty_file_style_edition_2024 to newline_file * invert check. fix newline_file. * Move empty_file test from tests/{source,target,config}/ to next to append_newline * Make tests/source/recorder_modules_2027/*/mod.rs empty files
1 parent 01f2ec7 commit 5e73127

96 files changed

Lines changed: 24 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
230230

231231
// For some reason, the source_map does not include terminating
232232
// newlines so we must add one on for each file. This is sad.
233-
source_file::append_newline(&mut visitor.buffer);
233+
source_file::append_newline(&mut visitor.buffer, self.config.style_edition());
234234

235235
format_lines(
236236
&mut visitor.buffer,

src/source_file.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use std::io::{self, Write};
33
use std::path::Path;
44
use std::sync::Arc;
55

6-
use crate::NewlineStyle;
76
use crate::config::FileName;
87
use crate::emitter::{self, Emitter};
98
use crate::parse::session::ParseSess;
9+
use crate::{NewlineStyle, StyleEdition};
1010

1111
#[cfg(test)]
1212
use crate::config::Config;
@@ -16,10 +16,27 @@ use crate::create_emitter;
1616
use crate::formatting::FileRecord;
1717

1818
// Append a newline to the end of each file.
19-
pub(crate) fn append_newline(s: &mut String) {
19+
pub(crate) fn append_newline(s: &mut String, style_edition: StyleEdition) {
20+
if style_edition >= StyleEdition::Edition2027 && s.is_empty() {
21+
return;
22+
}
2023
s.push('\n');
2124
}
2225

26+
#[test]
27+
fn append_newline_adds_newlines_before_2027() {
28+
let mut text = String::new();
29+
append_newline(&mut text, StyleEdition::Edition2024);
30+
assert_eq!(text, "\n");
31+
}
32+
33+
#[test]
34+
fn append_newline_leaves_empty_files_empty_in_2027() {
35+
let mut text = String::new();
36+
append_newline(&mut text, StyleEdition::Edition2027);
37+
assert!(text.is_empty());
38+
}
39+
2340
#[cfg(test)]
2441
pub(crate) fn write_all_files<T>(
2542
source_file: &[FileRecord],

src/test/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const FILE_SKIP_LIST: &[&str] = &[
4949
"cfg_mod/bar.rs",
5050
"cfg_mod/foo.rs",
5151
"cfg_mod/wasm32.rs",
52+
"reorder_modules_2027",
5253
"skip/foo.rs",
5354
];
5455

tests/config/newline_file.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style_edition = "2024"

tests/source/newline_file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/source/reorder_modules_2027/A2/mod.rs

Whitespace-only changes.

tests/source/reorder_modules_2027/ABCD/mod.rs

Whitespace-only changes.

tests/source/reorder_modules_2027/ZYXW/mod.rs

Whitespace-only changes.

tests/source/reorder_modules_2027/ZYXW_/mod.rs

Whitespace-only changes.

tests/source/reorder_modules_2027/ZY_XW/mod.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)