honnef.co/go/tools@v0.5.0-0.dev.0.20240520180541-dcae280a5e87/staticcheck/sa1021/sa1021.go (about) 1 package sa1021 2 3 import ( 4 "go/types" 5 6 "honnef.co/go/tools/analysis/callcheck" 7 "honnef.co/go/tools/analysis/lint" 8 "honnef.co/go/tools/go/ir" 9 "honnef.co/go/tools/internal/passes/buildir" 10 "honnef.co/go/tools/knowledge" 11 12 "golang.org/x/tools/go/analysis" 13 ) 14 15 var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{ 16 Analyzer: &analysis.Analyzer{ 17 Name: "SA1021", 18 Requires: []*analysis.Analyzer{buildir.Analyzer}, 19 Run: callcheck.Analyzer(rules), 20 }, 21 Doc: &lint.Documentation{ 22 Title: `Using \'bytes.Equal\' to compare two \'net.IP\'`, 23 Text: `A \'net.IP\' stores an IPv4 or IPv6 address as a slice of bytes. The 24 length of the slice for an IPv4 address, however, can be either 4 or 25 16 bytes long, using different ways of representing IPv4 addresses. In 26 order to correctly compare two \'net.IP\'s, the \'net.IP.Equal\' method should 27 be used, as it takes both representations into account.`, 28 Since: "2017.1", 29 Severity: lint.SeverityWarning, 30 MergeIf: lint.MergeIfAny, 31 }, 32 }) 33 34 var Analyzer = SCAnalyzer.Analyzer 35 36 var rules = map[string]callcheck.Check{ 37 "bytes.Equal": func(call *callcheck.Call) { 38 if isConvertedFrom(call.Args[knowledge.Arg("bytes.Equal.a")].Value, "net.IP") && 39 isConvertedFrom(call.Args[knowledge.Arg("bytes.Equal.b")].Value, "net.IP") { 40 call.Invalid("use net.IP.Equal to compare net.IPs, not bytes.Equal") 41 } 42 }, 43 } 44 45 // ConvertedFrom reports whether value v was converted from type typ. 46 func isConvertedFrom(v callcheck.Value, typ string) bool { 47 change, ok := v.Value.(*ir.ChangeType) 48 return ok && types.TypeString(types.Unalias(change.X.Type()), nil) == typ 49 }