Skip to content

Commit 877c1d1

Browse files
committed
gopls: address various staticcheck findings
Fix several real findings uncovered by running staticcheck ./... from the gopls module. Change-Id: Ieffd38bfd98cac24052c3b408907eb197c9e1cda Reviewed-on: https://go-review.googlesource.com/c/tools/+/650643 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 44abb0a commit 877c1d1

File tree

14 files changed

+24
-58
lines changed

14 files changed

+24
-58
lines changed

gopls/internal/analysis/modernize/sortslice.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package modernize
66

77
import (
8-
"fmt"
98
"go/ast"
109
"go/token"
1110
"go/types"
@@ -78,9 +77,9 @@ func sortslice(pass *analysis.Pass) {
7877
Pos: call.Fun.Pos(),
7978
End: call.Fun.End(),
8079
Category: "sortslice",
81-
Message: fmt.Sprintf("sort.Slice can be modernized using slices.Sort"),
80+
Message: "sort.Slice can be modernized using slices.Sort",
8281
SuggestedFixes: []analysis.SuggestedFix{{
83-
Message: fmt.Sprintf("Replace sort.Slice call by slices.Sort"),
82+
Message: "Replace sort.Slice call by slices.Sort",
8483
TextEdits: append(importEdits, []analysis.TextEdit{
8584
{
8685
// Replace sort.Slice with slices.Sort.

gopls/internal/analysis/unusedvariable/unusedvariable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func run(pass *analysis.Pass) (any, error) {
4747
if len(match) > 0 {
4848
varName := match[1]
4949
// Beginning in Go 1.23, go/types began quoting vars as `v'.
50-
varName = strings.Trim(varName, "'`'")
50+
varName = strings.Trim(varName, "`'")
5151

5252
err := runForError(pass, typeErr, varName)
5353
if err != nil {

gopls/internal/cache/analysis.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,7 @@ func typesLookup(pkg *types.Package) func(string) *types.Package {
822822
)
823823

824824
// search scans children the next package in pending, looking for pkgPath.
825-
var search func(pkgPath string) (*types.Package, int)
826-
search = func(pkgPath string) (sought *types.Package, numPending int) {
825+
search := func(pkgPath string) (sought *types.Package, numPending int) {
827826
mu.Lock()
828827
defer mu.Unlock()
829828

gopls/internal/cache/check.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ import (
4444
"golang.org/x/tools/internal/versions"
4545
)
4646

47-
// Various optimizations that should not affect correctness.
48-
const (
49-
preserveImportGraph = true // hold on to the import graph for open packages
50-
)
51-
5247
type unit = struct{}
5348

5449
// A typeCheckBatch holds data for a logical type-checking operation, which may
@@ -97,21 +92,6 @@ func (b *typeCheckBatch) getHandle(id PackageID) *packageHandle {
9792
return b._handles[id]
9893
}
9994

100-
// A futurePackage is a future result of type checking or importing a package,
101-
// to be cached in a map.
102-
//
103-
// The goroutine that creates the futurePackage is responsible for evaluating
104-
// its value, and closing the done channel.
105-
type futurePackage struct {
106-
done chan unit
107-
v pkgOrErr
108-
}
109-
110-
type pkgOrErr struct {
111-
pkg *types.Package
112-
err error
113-
}
114-
11595
// TypeCheck parses and type-checks the specified packages,
11696
// and returns them in the same order as the ids.
11797
// The resulting packages' types may belong to different importers,
@@ -701,8 +681,7 @@ func importLookup(mp *metadata.Package, source metadata.Source) func(PackagePath
701681

702682
// search scans children the next package in pending, looking for pkgPath.
703683
// Invariant: whenever search is called, pkgPath is not yet mapped.
704-
var search func(pkgPath PackagePath) (PackageID, bool)
705-
search = func(pkgPath PackagePath) (id PackageID, found bool) {
684+
search := func(pkgPath PackagePath) (id PackageID, found bool) {
706685
pkg := pending[0]
707686
pending = pending[1:]
708687
for depPath, depID := range pkg.DepsByPkgPath {

gopls/internal/cache/methodsets/fingerprint_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ func Test_fingerprint(t *testing.T) {
3939
// (Non-tricky types only.)
4040
var fingerprints typeutil.Map
4141

42-
type eqclass struct {
43-
class map[types.Type]bool
44-
fp string
45-
}
46-
4742
for _, pkg := range pkgs {
4843
switch pkg.Types.Path() {
4944
case "unsafe", "builtin":

gopls/internal/cache/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ searchOverlays:
13011301
// where the file is inside a workspace module, but perhaps no packages
13021302
// were loaded for that module.
13031303
_, loadedMod := loadedModFiles[goMod]
1304-
_, workspaceMod := s.view.viewDefinition.workspaceModFiles[goMod]
1304+
_, workspaceMod := s.view.workspaceModFiles[goMod]
13051305
// If we have a relevant go.mod file, check whether the file is orphaned
13061306
// due to its go.mod file being inactive. We could also offer a
13071307
// prescriptive diagnostic in the case that there is no go.mod file, but

gopls/internal/cache/source.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ func (s *goplsSource) ResolveReferences(ctx context.Context, filename string, mi
6161
// collect the ones that are still
6262
needed := maps.Clone(missing)
6363
for _, a := range fromWS {
64-
if _, ok := needed[a.Package.Name]; ok {
65-
delete(needed, a.Package.Name)
66-
}
64+
delete(needed, a.Package.Name)
6765
}
6866
// when debug (below) is gone, change this to: if len(needed) == 0 {return fromWS, nil}
6967
var fromCache []*result
@@ -184,10 +182,13 @@ type found struct {
184182
func (s *goplsSource) resolveWorkspaceReferences(filename string, missing imports.References) ([]*imports.Result, error) {
185183
uri := protocol.URIFromPath(filename)
186184
mypkgs, err := s.S.MetadataForFile(s.ctx, uri)
187-
if len(mypkgs) != 1 {
188-
// what does this mean? can it happen?
185+
if err != nil {
186+
return nil, err
187+
}
188+
if len(mypkgs) == 0 {
189+
return nil, nil
189190
}
190-
mypkg := mypkgs[0]
191+
mypkg := mypkgs[0] // narrowest package
191192
// search the metadata graph for package ids correstponding to missing
192193
g := s.S.MetadataGraph()
193194
var ids []metadata.PackageID

gopls/internal/cache/view.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"os/exec"
2121
"path"
2222
"path/filepath"
23-
"regexp"
2423
"slices"
2524
"sort"
2625
"strings"
@@ -1253,8 +1252,6 @@ func globsMatchPath(globs, target string) bool {
12531252
return false
12541253
}
12551254

1256-
var modFlagRegexp = regexp.MustCompile(`-mod[ =](\w+)`)
1257-
12581255
// TODO(rfindley): clean up the redundancy of allFilesExcluded,
12591256
// pathExcludedByFilterFunc, pathExcludedByFilter, view.filterFunc...
12601257
func allFilesExcluded(files []string, filterFunc func(protocol.DocumentURI) bool) bool {

gopls/internal/cmd/cmd.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,6 @@ func (app *Application) featureCommands() []tool.Application {
310310
}
311311
}
312312

313-
var (
314-
internalMu sync.Mutex
315-
internalConnections = make(map[string]*connection)
316-
)
317-
318313
// connect creates and initializes a new in-process gopls session.
319314
func (app *Application) connect(ctx context.Context) (*connection, error) {
320315
client := newClient(app)
@@ -377,10 +372,10 @@ func (c *connection) initialize(ctx context.Context, options func(*settings.Opti
377372
params.InitializationOptions = map[string]interface{}{
378373
"symbolMatcher": string(opts.SymbolMatcher),
379374
}
380-
if c.initializeResult, err = c.Server.Initialize(ctx, params); err != nil {
375+
if c.initializeResult, err = c.Initialize(ctx, params); err != nil {
381376
return err
382377
}
383-
if err := c.Server.Initialized(ctx, &protocol.InitializedParams{}); err != nil {
378+
if err := c.Initialized(ctx, &protocol.InitializedParams{}); err != nil {
384379
return err
385380
}
386381
return nil

gopls/internal/golang/extract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func stmtToInsertVarBefore(path []ast.Node, variables []*variable) (ast.Stmt, er
375375
}
376376
return parent, nil
377377
}
378-
return enclosingStmt.(ast.Stmt), nil
378+
return enclosingStmt, nil
379379
}
380380

381381
// canExtractVariable reports whether the code in the given range can be

0 commit comments

Comments
 (0)