github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1035/s1035.go (about) 1 package s1035 2 3 import ( 4 "fmt" 5 "go/ast" 6 7 "github.com/amarpal/go-tools/analysis/code" 8 "github.com/amarpal/go-tools/analysis/edit" 9 "github.com/amarpal/go-tools/analysis/facts/generated" 10 "github.com/amarpal/go-tools/analysis/lint" 11 "github.com/amarpal/go-tools/analysis/report" 12 13 "golang.org/x/tools/go/analysis" 14 "golang.org/x/tools/go/analysis/passes/inspect" 15 ) 16 17 var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{ 18 Analyzer: &analysis.Analyzer{ 19 Name: "S1035", 20 Run: run, 21 Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer}, 22 }, 23 Doc: &lint.Documentation{ 24 Title: `Redundant call to \'net/http.CanonicalHeaderKey\' in method call on \'net/http.Header\'`, 25 Text: ` 26 The methods on \'net/http.Header\', namely \'Add\', \'Del\', \'Get\' 27 and \'Set\', already canonicalize the given header name.`, 28 Since: "2020.1", 29 MergeIf: lint.MergeIfAny, 30 }, 31 }) 32 33 var Analyzer = SCAnalyzer.Analyzer 34 35 func run(pass *analysis.Pass) (interface{}, error) { 36 fn := func(node ast.Node) { 37 call := node.(*ast.CallExpr) 38 callName := code.CallName(pass, call) 39 switch callName { 40 case "(net/http.Header).Add", "(net/http.Header).Del", "(net/http.Header).Get", "(net/http.Header).Set": 41 default: 42 return 43 } 44 45 if !code.IsCallTo(pass, call.Args[0], "net/http.CanonicalHeaderKey") { 46 return 47 } 48 49 report.Report(pass, call, 50 fmt.Sprintf("calling net/http.CanonicalHeaderKey on the 'key' argument of %s is redundant", callName), 51 report.FilterGenerated(), 52 report.Fixes(edit.Fix("remove call to CanonicalHeaderKey", edit.ReplaceWithNode(pass.Fset, call.Args[0], call.Args[0].(*ast.CallExpr).Args[0])))) 53 } 54 code.Preorder(pass, fn, (*ast.CallExpr)(nil)) 55 return nil, nil 56 }