Skip to content

Commit e2ca594

Browse files
committed
gopls/internal/lsp/source: address problems detected by staticcheck
While working on performance I disabled staticcheck. Reenabling it today turned up several suggestions, and a few real bugs. Change-Id: I032d0cfb12ec8fda2573a2663faec4126b321afc Reviewed-on: https://go-review.googlesource.com/c/tools/+/557496 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 94d99e3 commit e2ca594

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

gopls/internal/lsp/source/change_signature.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
134134
if err != nil {
135135
return nil, err
136136
}
137+
137138
// Finally, rewrite the original declaration. We do this after inlining all
138139
// calls, as there may be calls in the same file as the declaration. But none
139140
// of the inlining should have changed the location of the original
@@ -149,7 +150,10 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
149150
src = pgf.Src
150151
}
151152
fset := tokeninternal.FileSetFor(pgf.Tok)
152-
src, err = rewriteSignature(fset, idx, src, newDecl)
153+
src, err := rewriteSignature(fset, idx, src, newDecl)
154+
if err != nil {
155+
return nil, err
156+
}
153157
newContent[pgf.URI] = src
154158
}
155159

gopls/internal/lsp/source/codeaction.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func CodeActions(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
9898
return nil, err
9999
}
100100
if want[protocol.RefactorRewrite] {
101-
rewrites, err := getRewriteCodeActions(snapshot, pkg, pgf, fh, rng, snapshot.Options())
101+
rewrites, err := getRewriteCodeActions(pkg, pgf, fh, rng, snapshot.Options())
102102
if err != nil {
103103
return nil, err
104104
}
@@ -253,7 +253,7 @@ func newCodeAction(title string, kind protocol.CodeActionKind, cmd *protocol.Com
253253
}
254254

255255
// getRewriteCodeActions returns refactor.rewrite code actions available at the specified range.
256-
func getRewriteCodeActions(snapshot *cache.Snapshot, pkg *cache.Package, pgf *ParsedGoFile, fh file.Handle, rng protocol.Range, options *settings.Options) (_ []protocol.CodeAction, rerr error) {
256+
func getRewriteCodeActions(pkg *cache.Package, pgf *ParsedGoFile, fh file.Handle, rng protocol.Range, options *settings.Options) (_ []protocol.CodeAction, rerr error) {
257257
// golang/go#61693: code actions were refactored to run outside of the
258258
// analysis framework, but as a result they lost their panic recovery.
259259
//

gopls/internal/lsp/source/definition.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ func builtinDecl(ctx context.Context, snapshot *cache.Snapshot, obj types.Object
150150
if err != nil {
151151
return nil, nil, err
152152
}
153-
154153
decl, err = getDecl(pgf.File, obj.Name())
154+
if err != nil {
155+
return nil, nil, err
156+
}
155157
} else {
156158
// pseudo-package "builtin":
157159
// use parsed $GOROOT/src/builtin/builtin.go
@@ -163,7 +165,9 @@ func builtinDecl(ctx context.Context, snapshot *cache.Snapshot, obj types.Object
163165
if obj.Parent() == types.Universe {
164166
// built-in function or type
165167
decl, err = getDecl(pgf.File, obj.Name())
166-
168+
if err != nil {
169+
return nil, nil, err
170+
}
167171
} else if obj.Name() == "Error" {
168172
// error.Error method
169173
decl, err = getDecl(pgf.File, "error")

gopls/internal/lsp/source/invertifcondition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func invertIfCondition(fset *token.FileSet, start, end token.Pos, src []byte, fi
9292
func endsWithReturn(elseBranch ast.Stmt) (bool, error) {
9393
elseBlock, isBlockStatement := elseBranch.(*ast.BlockStmt)
9494
if !isBlockStatement {
95-
return false, fmt.Errorf("Unable to figure out whether this ends with return: %T", elseBranch)
95+
return false, fmt.Errorf("unable to figure out whether this ends with return: %T", elseBranch)
9696
}
9797

9898
if len(elseBlock.List) == 0 {

gopls/internal/lsp/source/rename.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func renameOrdinary(ctx context.Context, snapshot *cache.Snapshot, f file.Handle
402402
// transitive rdeps. (The exportedness of the field's struct
403403
// or method's receiver is irrelevant.)
404404
transitive := false
405-
switch obj.(type) {
405+
switch obj := obj.(type) {
406406
case *types.TypeName:
407407
// Renaming an exported package-level type
408408
// requires us to inspect all transitive rdeps
@@ -418,7 +418,7 @@ func renameOrdinary(ctx context.Context, snapshot *cache.Snapshot, f file.Handle
418418
}
419419

420420
case *types.Var:
421-
if obj.(*types.Var).IsField() {
421+
if obj.IsField() {
422422
transitive = true // field
423423
}
424424

0 commit comments

Comments
 (0)