Skip to content

Commit

Permalink
Merge branch 'fix-14'
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoh86 committed Nov 29, 2022
2 parents 1a8d24d + 544fae5 commit 02c7f42
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
18 changes: 12 additions & 6 deletions looppointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/ast"
"go/token"
"go/types"

"github.com/kyoh86/nolint"
"golang.org/x/tools/go/analysis"
Expand Down Expand Up @@ -41,15 +42,15 @@ func run(pass *analysis.Pass) (interface{}, error) {
}

inspect.WithStack(nodeFilter, func(n ast.Node, _ bool, stack []ast.Node) bool {
typ, id, insert, digg := search.Check(n, stack)
if typ == RefTypeNone {
refType, id, insert, digg := search.Check(n, stack, pass.TypesInfo.Types)
if refType == RefTypeNone {
return digg
}
if noLinter.IgnoreNode(id, "looppointer") {
return digg
}
var dMsg string
switch typ {
switch refType {
case RefTypePointer:
dMsg = fmt.Sprintf("taking a pointer for the loop variable %s", id.Name)
case RefTypeSlice:
Expand Down Expand Up @@ -95,7 +96,7 @@ const (
RefTypeSlice
)

func (s *Searcher) Check(n ast.Node, stack []ast.Node) (RefType, *ast.Ident, token.Pos, bool) {
func (s *Searcher) Check(n ast.Node, stack []ast.Node, astTypes map[ast.Expr]types.TypeAndValue) (RefType, *ast.Ident, token.Pos, bool) {
switch typed := n.(type) {
case *ast.RangeStmt:
s.parseRangeStmt(typed)
Expand All @@ -104,7 +105,7 @@ func (s *Searcher) Check(n ast.Node, stack []ast.Node) (RefType, *ast.Ident, tok
case *ast.UnaryExpr:
return s.checkUnaryExpr(typed, stack)
case *ast.SliceExpr:
return s.checkSliceExpr(typed, stack)
return s.checkSliceExpr(typed, stack, astTypes)
}
return RefTypeNone, nil, token.NoPos, true
}
Expand Down Expand Up @@ -177,7 +178,7 @@ func (s *Searcher) checkUnaryExpr(n *ast.UnaryExpr, stack []ast.Node) (RefType,
return RefTypePointer, id, insert, false
}

func (s *Searcher) checkSliceExpr(n *ast.SliceExpr, stack []ast.Node) (RefType, *ast.Ident, token.Pos, bool) {
func (s *Searcher) checkSliceExpr(n *ast.SliceExpr, stack []ast.Node, astTypes map[ast.Expr]types.TypeAndValue) (RefType, *ast.Ident, token.Pos, bool) {
loop, insert := s.innermostLoop(stack)
if loop == nil {
return RefTypeNone, nil, token.NoPos, true
Expand All @@ -195,6 +196,11 @@ func (s *Searcher) checkSliceExpr(n *ast.SliceExpr, stack []ast.Node) (RefType,
return RefTypeNone, nil, token.NoPos, true
}

idType, clearType := astTypes[id]
if clearType && idType.Type.Underlying().String() == "string" {
return RefTypeNone, nil, token.NoPos, true
}

return RefTypeSlice, id, insert, false
}

Expand Down
1 change: 1 addition & 0 deletions looppointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ func Test(t *testing.T) {
analysistest.Run(t, testdata, looppointer.Analyzer, "nolint")
analysistest.Run(t, testdata, looppointer.Analyzer, "nested")
analysistest.Run(t, testdata, looppointer.Analyzer, "slice")
analysistest.Run(t, testdata, looppointer.Analyzer, "strslice")
}
29 changes: 29 additions & 0 deletions testdata/src/strslice/strslice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "fmt"

var a = []string{
"123",
"456",
"789",
}

type strlike string

var b = []string{
"abc",
"def",
"ghi",
}

func main() {
var s []string
for _, e := range a {
s = append(s, e[:1]) // no problem
}
for _, e := range b {
s = append(s, e[:1]) // no problem
}

fmt.Printf("%q", s)
}

0 comments on commit 02c7f42

Please sign in to comment.