package main

import (
    "sort"
)

func main() {
    foo := []string{}
    sort.Slice(foo, func(i, j int) bool { return foo[i] < foo[j] })
}

Running go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix ./... on this yields:

package main

import (
    "slices"
    "sort"
)

func main() {
    foo := []string{}
    slices.Sort(foo)
}

Which is invalid go because of compile error "sort" imported and not used. Instead, gopls should remove the unused sort import.

Comment From: gabyhelp

Related Issues

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

Comment From: adonovan

This isn't particular to modernizers; applying any set of fixes has the potential to leave unused imports even if each individual fix removes any imports that it alone makes redundant. So it has to be a cleanup step performed after applying a set of fixes. That means this is a duplicate of https://github.com/golang/go/issues/72035.