src

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

sa1002.go (1323B)


      1 package sa1002
      2 
      3 import (
      4 	"go/constant"
      5 	"strings"
      6 	"time"
      7 
      8 	"honnef.co/go/tools/analysis/callcheck"
      9 	"honnef.co/go/tools/analysis/lint"
     10 	"honnef.co/go/tools/internal/passes/buildir"
     11 	"honnef.co/go/tools/knowledge"
     12 
     13 	"golang.org/x/tools/go/analysis"
     14 )
     15 
     16 var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
     17 	Analyzer: &analysis.Analyzer{
     18 		Name:     "SA1002",
     19 		Requires: []*analysis.Analyzer{buildir.Analyzer},
     20 		Run:      callcheck.Analyzer(rules),
     21 	},
     22 	Doc: &lint.RawDocumentation{
     23 		Title: `Invalid format in \'time.Parse\'`,
     24 		Text: `\'time.Parse\' requires a layout string that uses Go's reference time:
     25 \"Mon Jan 2 15:04:05 MST 2006\". The layout must represent this date and time
     26 exactly. See https://pkg.go.dev/time#pkg-constants for layout examples.`,
     27 		Since:    "2017.1",
     28 		Severity: lint.SeverityError,
     29 		MergeIf:  lint.MergeIfAny,
     30 	},
     31 })
     32 
     33 var Analyzer = SCAnalyzer.Analyzer
     34 
     35 var rules = map[string]callcheck.Check{
     36 	"time.Parse": func(call *callcheck.Call) {
     37 		arg := call.Args[knowledge.Arg("time.Parse.layout")]
     38 		if c := callcheck.ExtractConstExpectKind(arg.Value, constant.String); c != nil {
     39 			s := constant.StringVal(c.Value)
     40 			s = strings.Replace(s, "_", " ", -1)
     41 			s = strings.Replace(s, "Z", "-", -1)
     42 			_, err := time.Parse(s, s)
     43 			if err != nil {
     44 				arg.Invalid(err.Error())
     45 			}
     46 		}
     47 	},
     48 }