Skip to content

Remove each comment's leading whitespace in comment::rewrite_comment #5392

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn is_custom_comment(comment: &str) -> bool {
}
}

#[derive(Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum CommentStyle<'a> {
DoubleSlash,
TripleSlash,
Expand Down Expand Up @@ -263,7 +263,7 @@ pub(crate) fn rewrite_comment(
shape: Shape,
config: &Config,
) -> Option<String> {
identify_comment(orig, block_style, shape, config, false)
identify_comment(orig.trim_start(), block_style, shape, config, false)
}

fn identify_comment(
Expand Down
123 changes: 123 additions & 0 deletions tests/target/issue_5391.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// rustfmt-version: Two

fn custom_comments() {
// Custom comment is the last element of the block
{
let x = 0; // X
//.Y
}

// Custom comment is not the last element of the block
{
let x = 0; // X
//.Y
println!("hello world!");
}

// Variations on custom comment tests with and without extra text
{
let x = 0; // X
//~Y
}
{
let x = 0; // X
//~Y
println!("hello world!");
}
{
let x = 0; // X
//.Y Some more text
}
{
let x = 0; // X
//.Y Some more text
println!("hello world!");
}
{
let x = 0; // X
//~Y Some more text
}
{
let x = 0; // X
//~Y Some more text
println!("hello world!");
}
}

fn double_slash_comments() {
// DoubleSlash comment is the last element of the block
{
let x = 0; // X
// .Y
}

// DoubleSlash comment is not the the last element of the block
{
let x = 0; // X
// .Y
println!("hello world!");
}
{
let x = 0; // X
// ~Y
}
{
let x = 0; // X
// ~Y
println!("hello world!");
}
{
let x = 0; // X
//Y
}
{
let x = 0; // X
//Y
println!("hello world!");
}
{
let x = 0; // X
// Y
}
{
let x = 0; // X
// Y
println!("hello world!");
}
}

// Compiler UI Test Error Annotations that use custom comments
// https://rustc-dev-guide.rust-lang.org/tests/ui.html#error-annotation-examples
mod compiler_ui_test_error_annotations {
/// Use the //~ ERROR idiom:
fn positioned_on_error_line() {
let x = (1, 2, 3);
match x {
(_a, _x @ ..) => {} //~ ERROR `_x @` is not allowed in a tuple
_ => {}
}
}

/// Use the //~^ idiom with number of carets in the string to indicate the number of lines above
fn positioned_below_error_line() {
let x = (1, 2, 3);
match x {
(_a, _x @ ..) => {} // <- the error is on this line
_ => {}
}
//~^^^ ERROR `_x @` is not allowed in a tuple
}

struct Binder(i32, i32, i32);

/// Use the //~| idiom to define the same error line as the error annotation line above
fn use_same_error_line_as_defined_on_error_annotation_line_above() {
let x = Binder(1, 2, 3);
match x {
Binder(_a, _x @ ..) => {} // <- the error is on this line
_ => {}
}
//~^^^ ERROR `_x @` is not allowed in a tuple struct
//~| ERROR this pattern has 1 field, but the corresponding tuple struct has 3 fields [E0023]
}
}