diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 28edcb784b4..7df14ca2be9 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -278,11 +278,14 @@ impl<'a> FmtVisitor<'a> { let first_line = &subslice[..offset]; self.push_str(first_line); self.push_str(&comment_indent.to_string_with_newline(self.config)); - let other_lines = &subslice[offset + 1..]; - let comment_str = - rewrite_comment(other_lines, false, comment_shape, self.config) - .unwrap_or_else(|| String::from(other_lines)); + let comment_str = rewrite_comment( + other_lines.trim_start(), + false, + comment_shape, + self.config, + ) + .unwrap_or_else(|| other_lines.to_string()); self.push_str(&comment_str); } } diff --git a/src/visitor.rs b/src/visitor.rs index c0fc37eaaa8..e5276a3a319 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -313,7 +313,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.push_str(&self.block_indent.to_string_with_newline(config)); // put the other lines below it, shaping it as needed - let other_lines = &sub_slice[offset + 1..]; + let other_lines = &sub_slice[offset + 1..].trim_start(); let comment_str = rewrite_comment(other_lines, false, comment_shape, config); match comment_str { diff --git a/tests/source/issue-4824/one.rs b/tests/source/issue-4824/one.rs new file mode 100644 index 00000000000..5529d1885f5 --- /dev/null +++ b/tests/source/issue-4824/one.rs @@ -0,0 +1,71 @@ +// rustfmt-version: One + +// Original issue test case + +#[cfg(windows)] +fn main() { +use common_util::get_version_for_rc; +use std::env; +use winres::{VersionInfo, WindowsResource}; + +println!("cargo:rerun-if-changed=favicon.ico"); +println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed +/*let profile = env::var("PROFILE").unwrap(); +if profile == "release"*/ { +let mut res = WindowsResource::new(); +res.set_icon("favicon.ico"); +let version = get_version_for_rc!(); +res.set_version_info(VersionInfo::PRODUCTVERSION, version); +res.set_version_info(VersionInfo::FILEVERSION, version); +res.compile().unwrap(); +} +} + +#[cfg(not(windows))] +fn main() {} + +// Other test cases + +fn main() { +let a = 1; // One line comment +/* Block comment */ +true +} + +fn main() { +let a = 1; /* Block comment */ +// One line comment +true +} + +fn main() { +let a = 1; // One line comment +/* Block comment */ +} + +fn main() { +let a = 1; /* Block comment */ +// One line comment +} + +fn main() { +// One line comment +/* Block comment */ +true +} + +fn main() { +/* Block comment */ +// One line comment +true +} + +fn main() { +// One line comment +/* Block comment */ +} + +fn main() { +/* Block comment */ +// One line comment +} diff --git a/tests/source/issue-4824/two.rs b/tests/source/issue-4824/two.rs new file mode 100644 index 00000000000..fd38ab7969e --- /dev/null +++ b/tests/source/issue-4824/two.rs @@ -0,0 +1,71 @@ +// rustfmt-version: Two + +// Original issue test case + +#[cfg(windows)] +fn main() { +use common_util::get_version_for_rc; +use std::env; +use winres::{VersionInfo, WindowsResource}; + +println!("cargo:rerun-if-changed=favicon.ico"); +println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed +/*let profile = env::var("PROFILE").unwrap(); +if profile == "release"*/ { +let mut res = WindowsResource::new(); +res.set_icon("favicon.ico"); +let version = get_version_for_rc!(); +res.set_version_info(VersionInfo::PRODUCTVERSION, version); +res.set_version_info(VersionInfo::FILEVERSION, version); +res.compile().unwrap(); +} +} + +#[cfg(not(windows))] +fn main() {} + +// Other test cases + +fn main() { +let a = 1; // One line comment +/* Block comment */ +true +} + +fn main() { +let a = 1; /* Block comment */ +// One line comment +true +} + +fn main() { +let a = 1; // One line comment +/* Block comment */ +} + +fn main() { +let a = 1; /* Block comment */ +// One line comment +} + +fn main() { +// One line comment +/* Block comment */ +true +} + +fn main() { +/* Block comment */ +// One line comment +true +} + +fn main() { +// One line comment +/* Block comment */ +} + +fn main() { +/* Block comment */ +// One line comment +} diff --git a/tests/target/issue-4824/one.rs b/tests/target/issue-4824/one.rs new file mode 100644 index 00000000000..4e0499803a0 --- /dev/null +++ b/tests/target/issue-4824/one.rs @@ -0,0 +1,72 @@ +// rustfmt-version: One + +// Original issue test case + +#[cfg(windows)] +fn main() { + use common_util::get_version_for_rc; + use std::env; + use winres::{VersionInfo, WindowsResource}; + + println!("cargo:rerun-if-changed=favicon.ico"); + println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed + /*let profile = env::var("PROFILE").unwrap(); + if profile == "release"*/ + { + let mut res = WindowsResource::new(); + res.set_icon("favicon.ico"); + let version = get_version_for_rc!(); + res.set_version_info(VersionInfo::PRODUCTVERSION, version); + res.set_version_info(VersionInfo::FILEVERSION, version); + res.compile().unwrap(); + } +} + +#[cfg(not(windows))] +fn main() {} + +// Other test cases + +fn main() { + let a = 1; // One line comment + /* Block comment */ + true +} + +fn main() { + let a = 1; /* Block comment */ + // One line comment + true +} + +fn main() { + let a = 1; // One line comment + /* Block comment */ +} + +fn main() { + let a = 1; /* Block comment */ + // One line comment +} + +fn main() { + // One line comment + /* Block comment */ + true +} + +fn main() { + /* Block comment */ + // One line comment + true +} + +fn main() { + // One line comment + /* Block comment */ +} + +fn main() { + /* Block comment */ + // One line comment +} diff --git a/tests/target/issue-4824/two.rs b/tests/target/issue-4824/two.rs new file mode 100644 index 00000000000..4ac336dcb77 --- /dev/null +++ b/tests/target/issue-4824/two.rs @@ -0,0 +1,72 @@ +// rustfmt-version: Two + +// Original issue test case + +#[cfg(windows)] +fn main() { + use common_util::get_version_for_rc; + use std::env; + use winres::{VersionInfo, WindowsResource}; + + println!("cargo:rerun-if-changed=favicon.ico"); + println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed + /*let profile = env::var("PROFILE").unwrap(); + if profile == "release"*/ + { + let mut res = WindowsResource::new(); + res.set_icon("favicon.ico"); + let version = get_version_for_rc!(); + res.set_version_info(VersionInfo::PRODUCTVERSION, version); + res.set_version_info(VersionInfo::FILEVERSION, version); + res.compile().unwrap(); + } +} + +#[cfg(not(windows))] +fn main() {} + +// Other test cases + +fn main() { + let a = 1; // One line comment + /* Block comment */ + true +} + +fn main() { + let a = 1; /* Block comment */ + // One line comment + true +} + +fn main() { + let a = 1; // One line comment + /* Block comment */ +} + +fn main() { + let a = 1; /* Block comment */ + // One line comment +} + +fn main() { + // One line comment + /* Block comment */ + true +} + +fn main() { + /* Block comment */ + // One line comment + true +} + +fn main() { + // One line comment + /* Block comment */ +} + +fn main() { + /* Block comment */ + // One line comment +}