golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.37k stars 17.71k forks source link

x/tools/gopls: refactor.extract.variable on multi-valued expression produces invalid code #70561

Closed adonovan closed 5 days ago

adonovan commented 6 days ago

Gopls' extract logic does not correctly handle the case when an expression has multiple values:

Before:

func f(m map[string]int) {
    if v, ok := m[""]; ok { // <-- refactor.extract.variable on m[""]
        println(v)
    }
}

After:

func f(m map[string]int) {
    x := m[""]
    if v, ok := x; ok { // error: assignment mismatch
        println(v)
    }
}
gopherbot commented 6 days ago

Change https://go.dev/cl/631777 mentions this issue: gopls/internal/golang: refactor.extract.variable: allow all exprs

gabyhelp commented 6 days ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)