mvdan / gofumpt

A stricter gofmt
https://pkg.go.dev/mvdan.cc/gofumpt
BSD 3-Clause "New" or "Revised" License
3.15k stars 110 forks source link

No empty lines before a simple error check not working #271

Closed ftamhar closed 10 months ago

ftamhar commented 1 year ago

If my code like this

package main

import "fmt"

func main() {
    var i int
    var err error
    i, err = fmt.Println("hello world")

    if err != nil {
        panic(err)
    }

    fmt.Println(i, err)
}

and I run gofumpt -l -w ., my expected result is like this

package main

import "fmt"

func main() {
    var i int
    var err error
    i, err = fmt.Println("hello world"
    if err != nil {
        panic(err)
    }

    fmt.Println(i, err)
}

but the result is like the first code snippet.

How do I achieve the results I expect?

flan6 commented 10 months ago

I noticed that sometimes it does not work too.

mvdan commented 10 months ago

This check only triggers if the previous line follows the , err := pattern - note how it requires the token :=, it doesn't accept =. I agree that that's an unnecessary restriction, and we should make the formatter slightly more aggressive.