src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

sa1010.go (2056B)


      1 package sa1010
      2 
      3 import (
      4 	"fmt"
      5 	"go/constant"
      6 
      7 	"honnef.co/go/tools/analysis/callcheck"
      8 	"honnef.co/go/tools/analysis/lint"
      9 	"honnef.co/go/tools/go/ir"
     10 	"honnef.co/go/tools/internal/passes/buildir"
     11 
     12 	"golang.org/x/tools/go/analysis"
     13 )
     14 
     15 var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
     16 	Analyzer: &analysis.Analyzer{
     17 		Name:     "SA1010",
     18 		Requires: []*analysis.Analyzer{buildir.Analyzer},
     19 		Run:      callcheck.Analyzer(checkRegexpFindAllRules),
     20 	},
     21 	Doc: &lint.RawDocumentation{
     22 		Title: `\'(*regexp.Regexp).FindAll\' called with \'n == 0\', which will always return zero results`,
     23 		Text: `If \'n >= 0\', the function returns at most \'n\' matches/submatches. To
     24 return all results, specify a negative number.`,
     25 		Since:    "2017.1",
     26 		Severity: lint.SeverityWarning,
     27 		MergeIf:  lint.MergeIfAny, // MergeIfAny if we only flag literals, not named constants
     28 	},
     29 })
     30 
     31 var Analyzer = SCAnalyzer.Analyzer
     32 
     33 var checkRegexpFindAllRules = map[string]callcheck.Check{
     34 	"(*regexp.Regexp).FindAll":                    RepeatZeroTimes("a FindAll method", 1),
     35 	"(*regexp.Regexp).FindAllIndex":               RepeatZeroTimes("a FindAll method", 1),
     36 	"(*regexp.Regexp).FindAllString":              RepeatZeroTimes("a FindAll method", 1),
     37 	"(*regexp.Regexp).FindAllStringIndex":         RepeatZeroTimes("a FindAll method", 1),
     38 	"(*regexp.Regexp).FindAllStringSubmatch":      RepeatZeroTimes("a FindAll method", 1),
     39 	"(*regexp.Regexp).FindAllStringSubmatchIndex": RepeatZeroTimes("a FindAll method", 1),
     40 	"(*regexp.Regexp).FindAllSubmatch":            RepeatZeroTimes("a FindAll method", 1),
     41 	"(*regexp.Regexp).FindAllSubmatchIndex":       RepeatZeroTimes("a FindAll method", 1),
     42 }
     43 
     44 func RepeatZeroTimes(name string, arg int) callcheck.Check {
     45 	return func(call *callcheck.Call) {
     46 		arg := call.Args[arg]
     47 		if k, ok := arg.Value.Value.(*ir.Const); ok && k.Value.Kind() == constant.Int {
     48 			if v, ok := constant.Int64Val(k.Value); ok && v == 0 {
     49 				arg.Invalid(fmt.Sprintf("calling %s with n == 0 will return no results, did you mean -1?", name))
     50 			}
     51 		}
     52 	}
     53 }