rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Comment indentation on opening line of a block is incorrect

Open mqudsi opened this issue 6 years ago • 7 comments

Similar to the just-filed #3254 but a different case and with a possibly different resolution, the following comment is re-indented incorrectly:

if foo { // this comment describes the entire block
    let x = y();
}

Which incorrectly interprets the comment as belonging within the if statement, which is technically correct only in the most pedantic sense, as to human programmers (an admittedly slowly dying species) it is fairly clear that the comment (regardless of its contents) describes the preceding contents of the line and does not belong inside the block:

if foo {
    // this comment describes the entire block
    let x = y();
}

As now the comment appears to describe the first line of the block (let x = y();) and not the block itself. This should either not be reformatted or it should be reformatted to the following:

// this comment describes the entire block
if foo {
    let x = y();
}

mqudsi avatar Dec 16 '18 22:12 mqudsi

I am working on this issue.

whizsid avatar Feb 21 '21 21:02 whizsid

This issue still exists, and it is quite a problem IMO. Putting the comment on the line after the if changes the semantic meaning of what the comment explains! Consider the following code:

                if flags != 2 { // BCRYPT_USE_SYSTEM_PREFERRED_RNG
                    throw_unsup_format!(
                        "BCryptGenRandom is supported only with the BCRYPT_USE_SYSTEM_PREFERRED_RNG flag"
                    );
                }

The comment is crucial to understand what the conditional check itself is doing. However, rustfmt changes this to

                if flags != 2 {
                    // BCRYPT_USE_SYSTEM_PREFERRED_RNG
                    throw_unsup_format!(
                        "BCryptGenRandom is supported only with the BCRYPT_USE_SYSTEM_PREFERRED_RNG flag"
                    );
                }

This is bad because it changes the semantic comment of the comment! Now it looks like the comment is explaining the throw_unsup_format invocation.

I think this kind of re-ordering of comments is not something a tool should do automatically. I also found no way, including using /*...*/ comments, to actually have a satisfying formatting for this code that rustfmt accept.

Is there any way to disable this comment-changing behavior?

Cc https://github.com/rust-lang/rustfmt/issues/4573

RalfJung avatar May 17 '21 08:05 RalfJung

in year 2023. still not find any solution . I got the same problem too, #5856. the pr may fix the problem has not accept yet: pull/4745

q2333gh avatar Jul 23 '23 02:07 q2333gh

2024, let's go.

If I can't document my code correctly without the formatter changing the meaning of my comments, then that means the formatter is unusable for me.

mbartelsm avatar Jan 03 '24 14:01 mbartelsm

Seems like the current state of this is that a PR exists at https://github.com/rust-lang/rustfmt/pull/4745, but it targets the wrong branch and has to be rebased to master. I don't know how much rustfmt-2.0.0-rc.2 differs from master -- how hard would such a rebase be?

RalfJung avatar Jul 06 '24 08:07 RalfJung

Tough to say how much things have diverged for this change in particular, but we might get lucky and it could be a simple cherry-pick of the commits to a new branch.

ytmimi avatar Jul 06 '24 15:07 ytmimi

2025, what's going on ... I think it's better to keep the comment at the same line, as most language would. It's more readable.

kuchaguangjie avatar Feb 16 '25 08:02 kuchaguangjie