golang / go

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

x/tools/internal/refactor/inline: adds unnecessary when inlining pointer receiver function calls on non-pointer values #69442

Open lfolger opened 5 days ago

lfolger commented 5 days ago
func TestPointerReceiver(t *testing.T) {
    files := map[string]string{
                    package foo
                type T int

                func (*T) F() {}

                //inlineme
                func (t *T) G() { t.F() }

                func main() {
                    var t T
                    t.G() // want "inline call of (*fooT).G"
                }
        `,
        "foo/foo.go.golden": `
            package foo
                type T int

                func (*T) F() {}

                //inlineme
                func (t *T) G() { t.F() }

                func main() {
                    var t T
                    t.F() // want "inline call of (*fooT).G"
                }
        `,
    }
    dir, cleanup, err := analysistest.WriteFiles(files)
    if err != nil {
        t.Fatal(err)
    }
    analysistest.RunWithSuggestedFixes(t, dir, analyzer.Analyzer, "foo")
    cleanup()
}

The test fails with:

        --- .../foo/foo.go.golden
        +++ actual
        @@ -9,5 +9,5 @@

         func main() {
                var t T
        -       t.F() // want `inline call of \(\*foo.T\).G`
        +       (&t).F() // want `inline call of \(\*foo.T\).G`
         }

I would be great if the inliner could just not add the unnecessary syntax although it is possible that I'm missing and edge case and this is actually needed.

gabyhelp commented 5 days ago

Related Issues and Documentation

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

lfolger commented 5 days ago

cc: @findleyr @adonovan @stapelberg