sway icon indicating copy to clipboard operation
sway copied to clipboard

Formatter mis-formats the associated-type semicolon

Open Lukasz2891 opened this issue 1 year ago • 1 comments

Related Component

forc-fmt

Problem

library;

use std::iterator::Iterator;

struct ListIter<T> {
    this: List<T>,
}

enum List<T> {
    Next: T,
    End: (),
}

impl List<T> {
    pub fn iter(self) -> ListIter<T> {
        Iterator { this: self }
    }
}

impl<T> Iterator for ListIter<T> {
    type Item = T;

    fn next(ref mut self) -> Option<Self::Item> {
        match self.this {
            Next(elt) => {
                this = elt;
                elt
            },
            End => None,
        }
    }
}

is formatted to

// ...
impl<T> Iterator for ListIter<T> {
    type Item = T
;    // <<< here is the formatted semicolon

    fn next(ref mut self) -> Option<Self::Item> {
        match self.this {
            Next(elt) => {
                this = elt;
                elt
            },
            End => None,
        }
    }
}

Then its formatted to:

// ...
impl<T> Iterator for ListIter<T> {
    type Item = T; // Missing the empty line
    fn next(ref mut self) -> Option<Self::Item> {
        match self.this {
            Next(elt) => {
                this = elt;
                elt
            },
            End => None,
        }
    }
}

Steps

Use the file and format it :)

Possible Solution(s)

Not to reformat the given file

Notes

No response

Installed components

active toolchain
----------------
testnet-aarch64-apple-darwin (override) (default), path: ...
  forc : 0.61.2
    - forc-client
      - forc-deploy : 0.61.2
      - forc-run : 0.61.2
    - forc-crypto : 0.61.2
    - forc-debug : 0.61.2
    - forc-doc : 0.61.2
    - forc-fmt : 0.61.2
    - forc-lsp : 0.61.2
    - forc-tx : 0.61.2
    - forc-wallet : 0.8.1
  fuel-core : 0.28.0
  fuel-core-keygen : 0.28.0

fuels versions
--------------
forc : 0.64.0
forc-wallet : 0.64.0

Lukasz2891 avatar Jul 11 '24 09:07 Lukasz2891

Thanks for the bug report, I was able to reproduce it. The formatter should never produce different results on the second run, and the semicolon is definitely mis-formatted.

sdankel avatar Aug 19 '24 17:08 sdankel