Skip to content

Attributes on expressions become duplicated under certain conditions #4452

Closed
@coadler

Description

@coadler

Describe the bug

Attributes on expressions become doubled every time rustfmt is ran when preceeded by a comment and the expression contains a cast, i.e. as i64.

To Reproduce

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2499dbd812381b73c863ad79691053fd

trait FooBar {
    fn foo(&self) -> i64 {
        // some comment
        #[allow(clippy::cast_possible_wrap)]
        1u64 as i64
    }
}

Running rustfmt under tools on the above playground link causes the attribute to double on every invocation.

Playing around with it, removing either the comment and as i64 cause this not to reproduce, so it seems like it's the combination of them both that is causing this bug.

Expected behavior

The attribute isn't doubled every time.

Meta

  • rustfmt version: rustfmt 1.4.21-nightly (01f2ead 2020-09-04) and latest stable
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt directly on the file


(edited by calebcartwright to include inline reproduction snippet)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.good first issueIssues up for grabs, also good candidates for new rustfmt contributorshacktoberfesthelp wanted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions