Skip to content

x/tools/gopls: modernize slices.Clone suggestion wrong when copying between aliased types #73661

Open
@anderseknert

Description

@anderseknert

gopls version

v0.18.1

go env

-

What did you do?

Ran the modernize linter on this example:

package main

import "fmt"

type ssa []string
type ssb []string

var a ssa = ssa{"a", "b", "c"}

func main() {
	var b ssb = append([]string{}, a...)

	fmt.Println(b)
}
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix ./...

What did you see happen?

package main

import "slices"

import "fmt"

type ssa []string
type ssb []string

var a ssa = ssa{"a", "b", "c"}

func main() {
	var b ssb = slices.Clone(a)

	fmt.Println(b)
}
go build
./main.go:13:14: cannot use slices.Clone(a) (value of slice type ssa) as ssb value in variable declaration

What did you expect to see?

slices.Clone not being suggested here, or at least not in a form that breaks compilation.

The example is silly, but I encountered this in real code when trying to run modernize against the OPA project, here specifically.

Editor and settings

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ToolProposalIssues describing a requested change to a Go tool or command-line program.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions