src

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

sa1013.go (1468B)


      1 package sa1013
      2 
      3 import (
      4 	"go/ast"
      5 
      6 	"honnef.co/go/tools/analysis/code"
      7 	"honnef.co/go/tools/analysis/edit"
      8 	"honnef.co/go/tools/analysis/lint"
      9 	"honnef.co/go/tools/analysis/report"
     10 	"honnef.co/go/tools/knowledge"
     11 	"honnef.co/go/tools/pattern"
     12 
     13 	"golang.org/x/tools/go/analysis"
     14 )
     15 
     16 var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
     17 	Analyzer: &analysis.Analyzer{
     18 		Name:     "SA1013",
     19 		Run:      run,
     20 		Requires: code.RequiredAnalyzers,
     21 	},
     22 	Doc: &lint.RawDocumentation{
     23 		Title:    `\'io.Seeker.Seek\' is being called with the whence constant as the first argument, but it should be the second`,
     24 		Since:    "2017.1",
     25 		Severity: lint.SeverityWarning,
     26 		MergeIf:  lint.MergeIfAny,
     27 	},
     28 })
     29 
     30 var Analyzer = SCAnalyzer.Analyzer
     31 
     32 var (
     33 	checkSeekerQ = pattern.MustParse(`(CallExpr fun@(SelectorExpr _ (Ident "Seek")) [arg1@(SelectorExpr _ (Symbol (Or "io.SeekStart" "io.SeekCurrent" "io.SeekEnd"))) arg2])`)
     34 	checkSeekerR = pattern.MustParse(`(CallExpr fun [arg2 arg1])`)
     35 )
     36 
     37 func run(pass *analysis.Pass) (any, error) {
     38 	for node, m := range code.Matches(pass, checkSeekerQ) {
     39 		if !code.IsMethod(pass, m.State["fun"].(*ast.SelectorExpr), "Seek", knowledge.Signatures["(io.Seeker).Seek"]) {
     40 			continue
     41 		}
     42 		edits := code.EditMatch(pass, node, m, checkSeekerR)
     43 		report.Report(pass, node, "the first argument of io.Seeker is the offset, but an io.Seek* constant is being used instead",
     44 			report.Fixes(edit.Fix("Swap arguments", edits...)))
     45 	}
     46 	return nil, nil
     47 }