github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1028/s1028.go (about)

     1  package s1028
     2  
     3  import (
     4  	"go/ast"
     5  
     6  	"github.com/amarpal/go-tools/analysis/code"
     7  	"github.com/amarpal/go-tools/analysis/edit"
     8  	"github.com/amarpal/go-tools/analysis/facts/generated"
     9  	"github.com/amarpal/go-tools/analysis/lint"
    10  	"github.com/amarpal/go-tools/analysis/report"
    11  	"github.com/amarpal/go-tools/pattern"
    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:     "S1028",
    20  		Run:      run,
    21  		Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer},
    22  	},
    23  	Doc: &lint.Documentation{
    24  		Title:   `Simplify error construction with \'fmt.Errorf\'`,
    25  		Before:  `errors.New(fmt.Sprintf(...))`,
    26  		After:   `fmt.Errorf(...)`,
    27  		Since:   "2017.1",
    28  		MergeIf: lint.MergeIfAny,
    29  	},
    30  })
    31  
    32  var Analyzer = SCAnalyzer.Analyzer
    33  
    34  var (
    35  	checkErrorsNewSprintfQ = pattern.MustParse(`(CallExpr (Symbol "errors.New") [(CallExpr (Symbol "fmt.Sprintf") args)])`)
    36  	checkErrorsNewSprintfR = pattern.MustParse(`(CallExpr (SelectorExpr (Ident "fmt") (Ident "Errorf")) args)`)
    37  )
    38  
    39  func run(pass *analysis.Pass) (interface{}, error) {
    40  	fn := func(node ast.Node) {
    41  		if _, edits, ok := code.MatchAndEdit(pass, checkErrorsNewSprintfQ, checkErrorsNewSprintfR, node); ok {
    42  			// TODO(dh): the suggested fix may leave an unused import behind
    43  			report.Report(pass, node, "should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...))",
    44  				report.FilterGenerated(),
    45  				report.Fixes(edit.Fix("use fmt.Errorf", edits...)))
    46  		}
    47  	}
    48  	code.Preorder(pass, fn, (*ast.CallExpr)(nil))
    49  	return nil, nil
    50  }