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

     1  package sa6006
     2  
     3  import (
     4  	"go/ast"
     5  
     6  	"github.com/amarpal/go-tools/analysis/code"
     7  	"github.com/amarpal/go-tools/analysis/lint"
     8  	"github.com/amarpal/go-tools/analysis/report"
     9  	"github.com/amarpal/go-tools/go/types/typeutil"
    10  	"github.com/amarpal/go-tools/pattern"
    11  
    12  	"golang.org/x/tools/go/analysis"
    13  	"golang.org/x/tools/go/analysis/passes/inspect"
    14  )
    15  
    16  var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
    17  	Analyzer: &analysis.Analyzer{
    18  		Name:     "SA6006",
    19  		Run:      run,
    20  		Requires: []*analysis.Analyzer{inspect.Analyzer},
    21  	},
    22  	Doc: &lint.Documentation{
    23  		Title: `Using io.WriteString to write \'[]byte\'`,
    24  		Text: `Using io.WriteString to write a slice of bytes, as in
    25  
    26      io.WriteString(w, string(b))
    27  
    28  is both unnecessary and inefficient. Converting from \'[]byte\' to \'string\'
    29  has to allocate and copy the data, and we could simply use \'w.Write(b)\'
    30  instead.`,
    31  
    32  		Since: "Unreleased",
    33  	},
    34  })
    35  
    36  var Analyzer = SCAnalyzer.Analyzer
    37  
    38  var ioWriteStringConversion = pattern.MustParse(`(CallExpr (Symbol "io.WriteString") [_ (CallExpr (Builtin "string") [arg])])`)
    39  
    40  func run(pass *analysis.Pass) (interface{}, error) {
    41  	fn := func(node ast.Node) {
    42  		m, ok := code.Match(pass, ioWriteStringConversion, node)
    43  		if !ok {
    44  			return
    45  		}
    46  		if !typeutil.IsType(pass.TypesInfo.TypeOf(m.State["arg"].(ast.Expr)).Underlying(), "[]byte") {
    47  			return
    48  		}
    49  		report.Report(pass, node, "use io.Writer.Write instead of converting from []byte to string to use io.WriteString")
    50  	}
    51  	code.Preorder(pass, fn, (*ast.CallExpr)(nil))
    52  
    53  	return nil, nil
    54  }