-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/analysis/modernize: replace interface{} with any
Also, modernize our tests in this manner. Also, remove existing "useany" analyzer, which is subsumed by this one. The fact that useany was off by default suggest that perhaps modernize may need to be as well; but perhaps severity=INFO will suffice. Updates golang/go#70815 Change-Id: Iba1662993fb8a1c50b2d843ad9425bbddafc927f Reviewed-on: https://go-review.googlesource.com/c/tools/+/636276 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
- Loading branch information
Showing
38 changed files
with
143 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package modernize | ||
|
||
import ( | ||
"go/ast" | ||
"go/types" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
// The efaceany pass replaces interface{} with 'any'. | ||
func efaceany(pass *analysis.Pass) { | ||
// TODO(adonovan): opt: combine all these micro-passes into a single traversal. | ||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
for iface := range inspector.All[*ast.InterfaceType](inspect) { | ||
if iface.Methods.NumFields() == 0 { | ||
|
||
// TODO(adonovan): opt: record the enclosing file as we go. | ||
f := enclosingFile(pass, iface.Pos()) | ||
scope := pass.TypesInfo.Scopes[f].Innermost(iface.Pos()) | ||
if _, obj := scope.LookupParent("any", iface.Pos()); obj != types.Universe.Lookup("any") { | ||
continue // 'any' is shadowed | ||
} | ||
|
||
pass.Report(analysis.Diagnostic{ | ||
Pos: iface.Pos(), | ||
End: iface.End(), | ||
Category: "efaceany", | ||
Message: "interface{} can be replaced by any", | ||
SuggestedFixes: []analysis.SuggestedFix{{ | ||
Message: "Replace interface{} by any", | ||
TextEdits: []analysis.TextEdit{ | ||
{ | ||
Pos: iface.Pos(), | ||
End: iface.End(), | ||
NewText: []byte("any"), | ||
}, | ||
}, | ||
}}, | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
gopls/internal/analysis/modernize/testdata/src/efaceany/efaceany.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package efaceany | ||
|
||
func _(x interface{}) {} // want "interface{} can be replaced by any" | ||
|
||
func _() { | ||
var x interface{} // want "interface{} can be replaced by any" | ||
const any = 1 | ||
var y interface{} // nope: any is shadowed here | ||
_, _ = x, y | ||
} |
10 changes: 10 additions & 0 deletions
10
gopls/internal/analysis/modernize/testdata/src/efaceany/efaceany.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package efaceany | ||
|
||
func _(x any) {} // want "interface{} can be replaced by any" | ||
|
||
func _() { | ||
var x any // want "interface{} can be replaced by any" | ||
const any = 1 | ||
var y interface{} // nope: any is shadowed here | ||
_, _ = x, y | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.