8000 SA1019: detect deprecated dot-imported objects by dajiaohuang · Pull Request #1825 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions staticcheck/sa1019/sa1019.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func run(pass *analysis.Pass) (any, error) {

var tfn types.Object
stack := 0
var nodeStack []ast.Node

checkIdentObj := func(sel *ast.SelectorExpr) bool {
obj := pass.TypesInfo.ObjectOf(sel.Sel)
checkObject := func(obj types.Object, node ast.Node, name string) bool {

if obj_, ok := obj.(*types.Func); ok {
obj = obj_.Origin()
Expand Down Expand Up @@ -148,21 +148,34 @@ func run(pass *analysis.Pass) (any, error) {
return true
}

if depr, ok := deprs.Objects[obj]; ok {
handleDeprecation(depr, node, name, obj.Pkg().Path(), tfn)
}
return true
}
checkIdentObj := func(sel *ast.SelectorExpr) bool {
obj := pass.TypesInfo.ObjectOf(sel.Sel)
if obj_, ok := obj.(*types.Func); ok {
obj = obj_.Origin()
}
node := ast.Node(sel)
if pass.TypesInfo.Types[sel.X].IsType() {
node = sel.Sel
}
if depr, ok := deprs.Objects[obj]; ok {
handleDeprecation(depr, node, code.SelectorName(pass, sel), obj.Pkg().Path(), tfn)
}
return true
return checkObject(obj, node, code.SelectorName(pass, sel))
}

fn := func(node ast.Node, push bool) bool {
if !push {
stack--
nodeStack = nodeStack[:len(nodeStack)-1]
return false
}
var parent ast.Node
if len(nodeStack) > 0 {
parent = nodeStack[len(nodeStack)-1]
}
nodeStack = append(nodeStack, node)
stack++
if stack == 1 {
tfn = nil
Expand All @@ -172,9 +185,33 @@ func run(pass *analysis.Pass) (any, error) {
}

switch v := node.(type) {
// FIXME(dh): this misses dot-imported objects
case *ast.SelectorExpr:
return checkIdentObj(v)
case *ast.Ident:
// Selector names are handled together with their selector so that
// diagnostics can use the qualified name and the correct source node.
if sel, ok := parent.(*ast.SelectorExpr); ok && sel.Sel == v {
break
}
if kv, ok := parent.(*ast.KeyValueExpr); ok && kv.Key == v && len(nodeStack) >= 3 {
if lit, ok := nodeStack[len(nodeStack)-3].(*ast.CompositeLit); ok {
litType := pass.TypesInfo.Types[lit.Type]
if litType.IsType() {
if _, ok := litType.Type.Underlying().(*types.Struct); ok {
// Struct literal keys are checked below with a synthetic selector.
break
}
}
}
}
obj := pass.TypesInfo.ObjectOf(v)
if obj == nil || obj.Pkg() == nil {
break
}
if obj_, ok := obj.(*types.Func); ok {
obj = obj_.Origin()
}
checkObject(obj, v, obj.Pkg().Name()+"."+obj.Name())

case *ast.CompositeLit:
litType := pass.TypesInfo.Types[v.Type]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package pkg

// Deprecated: use another function.
func DeprecatedFn() {}

type SD struct {
// Deprecated: external don't use me
D string
Expand Down
8 changes: 8 additions & 0 deletions staticcheck/sa1019/testdata/go1.0/CheckDeprecated/dot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pkg

import . "example.com/CheckDeprecated.assist_external"

func dotImported() {
DeprecatedFn() //@ diag(`pkg.DeprecatedFn is deprecated: use another function`)
_ = SD{D: "used"} //@ diag(`external don't use me`)
}
0