Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce false positives of sliceInitialLength #38

Merged
merged 2 commits into from
Apr 21, 2024
Merged
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
30 changes: 23 additions & 7 deletions sliceinitiallength/slicemutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sliceinitiallength

import (
"go/ast"
"go/token"
"go/types"

"github.com/seiyab/gost/utils"
Expand All @@ -13,6 +14,7 @@ type mutation int
const (
appended mutation = iota + 1
assigned
replaced
copied
mixed
)
Expand Down Expand Up @@ -62,17 +64,21 @@ func collectSliceMutations(pass *analysis.Pass) map[types.Object]mutation {
}

func visitAssign(n *ast.AssignStmt, pass *analysis.Pass) map[types.Object]mutation {
if n.Tok != token.ASSIGN {
return nil
}
var muts = make(map[types.Object]mutation)
for i, lhs := range n.Lhs {
if i >= len(n.Rhs) {
break
}
if ident, ok := lhs.(*ast.Ident); ok {
t := pass.TypesInfo.TypeOf(ident)
switch lhs := lhs.(type) {
case *ast.Ident:
t := pass.TypesInfo.TypeOf(lhs)
if t == nil {
continue
}
o := pass.TypesInfo.ObjectOf(ident)
o := pass.TypesInfo.ObjectOf(lhs)
if o == nil {
continue
}
Expand All @@ -81,17 +87,26 @@ func visitAssign(n *ast.AssignStmt, pass *analysis.Pass) map[types.Object]mutati
}
call, ok := n.Rhs[i].(*ast.CallExpr)
if !ok {
muts[o] = replaced
continue
}
if !appendMatcher.Matches(pass, call) {
muts[o] = replaced
continue
}
orig, ok := call.Args[0].(*ast.Ident)
if !ok {
muts[o] = replaced
continue
}
if pass.TypesInfo.ObjectOf(orig) != o {
muts[o] = replaced
continue
}
muts[o] = appended
continue
}

if idx, ok := lhs.(*ast.IndexExpr); ok {
if ident, ok := idx.X.(*ast.Ident); ok {
case *ast.IndexExpr:
if ident, ok := lhs.X.(*ast.Ident); ok {
t := pass.TypesInfo.TypeOf(ident)
if t == nil {
continue
Expand All @@ -107,6 +122,7 @@ func visitAssign(n *ast.AssignStmt, pass *analysis.Pass) map[types.Object]mutati
continue
}
}

}
return muts
}
Expand Down
22 changes: 22 additions & 0 deletions sliceinitiallength/testdata/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,25 @@ func _() {
}
fmt.Println(ys)
}

func _() {
xs := []string{"a", "b", "c"}
ys := make([]int, len(xs))

ys = ys[:0]
for _, x := range xs {
ys = append(ys, len(x))
}
fmt.Println(ys)
}

func _() {
xs := []string{"a", "b", "c"}
ys := make([]int, len(xs))

ys = append(ys[:0], 0)
for _, x := range xs {
ys = append(ys, len(x))
}
fmt.Println(ys)
}
Loading