8000 go-tools/staticcheck/lint.go at 2019.1 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2818 lines (2656 loc) · 70.2 KB

lint.go

File metadata and controls

2818 lines (2656 loc) · 70.2 KB
// Package staticcheck contains a linter for Go source code.
package staticcheck // import "honnef.co/go/tools/staticcheck"
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
htmltemplate "html/template"
"net/http"
"regexp"
"regexp/syntax"
"sort"
"strconv"
"strings"
"sync"
texttemplate "text/template"
. "honnef.co/go/tools/arg"
"honnef.co/go/tools/deprecated"
"honnef.co/go/tools/functions"
"honnef.co/go/tools/internal/sharedcheck"
"honnef.co/go/tools/lint"
. "honnef.co/go/tools/lint/lintdsl"
"honnef.co/go/tools/ssa"
"honnef.co/go/tools/ssautil"
"honnef.co/go/tools/staticcheck/vrp"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)
func validRegexp(call *Call) {
arg := call.Args[0]
err := ValidateRegexp(arg.Value)
if err != nil {
arg.Invalid(err.Error())
}
}
type runeSlice []rune
func (rs runeSlice) Len() int { return len(rs) }
func (rs runeSlice) Less(i int, j int) bool { return rs[i] < rs[j] }
func (rs runeSlice) Swap(i int, j int) { rs[i], rs[j] = rs[j], rs[i] }
func utf8Cutset(call *Call) {
arg := call.Args[1]
if InvalidUTF8(arg.Value) {
arg.Invalid(MsgInvalidUTF8)
}
}
func uniqueCutset(call *Call) {
arg := call.Args[1]
if !UniqueStringCutset(arg.Value) {
arg.Invalid(MsgNonUniqueCutset)
}
}
func unmarshalPointer(name string, arg int) CallCheck {
return func(call *Call) {
if !Pointer(call.Args[arg].Value) {
call.Args[arg].Invalid(fmt.Sprintf("%s expects to unmarshal into a pointer, but the provided value is not a pointer", name))
}
}
}
func pointlessIntMath(call *Call) {
if ConvertedFromInt(call.Args[0].Value) {
call.Invalid(fmt.Sprintf("calling %s on a converted integer is pointless", CallName(call.Instr.Common())))
}
}
func checkValidHostPort(arg int) CallCheck {
return func(call *Call) {
if !ValidHostPort(call.Args[arg].Value) {
call.Args[arg].Invalid(MsgInvalidHostPort)
}
}
}
var (
checkRegexpRules = map[string]CallCheck{
"regexp.MustCompile": validRegexp,
"regexp.Compile": validRegexp,
"regexp.Match": validRegexp,
"regexp.MatchReader": validRegexp,
"regexp.MatchString": validRegexp,
}
checkTimeParseRules = map[string]CallCheck{
"time.Parse": func(call *Call) {
arg := call.Args[Arg("time.Parse.layout")]
err := ValidateTimeLayout(arg.Value)
if err != nil {
arg.Invalid(err.Error())
}
},
}
checkEncodingBinaryRules = map[string]CallCheck{
"encoding/binary.Write": func(call *Call) {
arg := call.Args[Arg("encoding/binary.Write.data")]
if !CanBinaryMarshal(call.Job, arg.Value) {
arg.Invalid(fmt.Sprintf("value of type %s cannot be used with binary.Write", arg.Value.Value.Type()))
}
},
}
checkURLsRules = map[string]CallCheck{
"net/url.Parse": func(call *Call) {
arg := call.Args[Arg("net/url.Parse.rawurl")]
err := ValidateURL(arg.Value)
if err != nil {
arg.Invalid(err.Error())
}
},
}
checkSyncPoolValueRules = map[string]CallCheck{
"(*sync.Pool).Put": func(call *Call) {
arg := call.Args[Arg("(*sync.Pool).Put.x")]
typ := arg.Value.Value.Type()
if !IsPointerLike(typ) {
arg.Invalid("argument should be pointer-like to avoid allocations")
}
},
}
checkRegexpFindAllRules = map[string]CallCheck{
"(*regexp.Regexp).FindAll": RepeatZeroTimes("a FindAll method", 1),
"(*regexp.Regexp).FindAllIndex": RepeatZeroTimes("a FindAll method", 1),
"(*regexp.Regexp).FindAllString": RepeatZeroTimes("a FindAll method", 1),
"(*regexp.Regexp).FindAllStringIndex": RepeatZeroTimes("a FindAll method", 1),
"(*regexp.Regexp).FindAllStringSubmatch": RepeatZeroTimes("a FindAll method", 1),
"(*regexp.Regexp).FindAllStringSubmatchIndex": RepeatZeroTimes("a FindAll method", 1),
"(*regexp.Regexp).FindAllSubmatch": RepeatZeroTimes("a FindAll method", 1),
"(*regexp.Regexp).FindAllSubmatchIndex": RepeatZeroTimes("a FindAll method", 1),
}
checkUTF8CutsetRules = map[string]CallCheck{
"strings.IndexAny": utf8Cutset,
"strings.LastIndexAny": utf8Cutset,
"strings.ContainsAny": utf8Cutset,
"strings.Trim": utf8Cutset,
"strings.TrimLeft": utf8Cutset,
"strings.TrimRight": utf8Cutset,
}
checkUniqueCutsetRules = map[string]CallCheck{
"strings.Trim": uniqueCutset,
"strings.TrimLeft": uniqueCutset,
"strings.TrimRight": uniqueCutset,
}
checkUnmarshalPointerRules = map[string]CallCheck{
"encoding/xml.Unmarshal": unmarshalPointer("xml.Unmarshal", 1),
"(*encoding/xml.Decoder).Decode": unmarshalPointer("Decode", 0),
"(*encoding/xml.Decoder).DecodeElement": unmarshalPointer("DecodeElement", 0),
"encoding/json.Unmarshal": unmarshalPointer("json.Unmarshal", 1),
"(*encoding/json.Decoder).Decode": unmarshalPointer("Decode", 0),
}
checkUnbufferedSignalChanRules = map[string]CallCheck{
"os/signal.Notify": func(call *Call) {
arg := call.Args[Arg("os/signal.Notify.c")]
if UnbufferedChannel(arg.Value) {
arg.Invalid("the channel used with signal.Notify should be buffered")
}
},
}
checkMathIntRules = map[string]CallCheck{
"math.Ceil": pointlessIntMath,
"math.Floor": pointlessIntMath,
"math.IsNaN": pointlessIntMath,
"math.Trunc": pointlessIntMath,
"math.IsInf": pointlessIntMath,
}
checkStringsReplaceZeroRules = map[string]CallCheck{
"strings.Replace": RepeatZeroTimes("strings.Replace", 3),
"bytes.Replace": RepeatZeroTimes("bytes.Replace", 3),
}
checkListenAddressRules = map[string]CallCheck{
"net/http.ListenAndServe": checkValidHostPort(0),
"net/http.ListenAndServeTLS": checkValidHostPort(0),
}
checkBytesEqualIPRules = map[string]CallCheck{
"bytes.Equal": func(call *Call) {
if ConvertedFrom(call.Args[Arg("bytes.Equal.a")].Value, "net.IP") &&
ConvertedFrom(call.Args[Arg("bytes.Equal.b")].Value, "net.IP") {
call.Invalid("use net.IP.Equal to compare net.IPs, not bytes.Equal")
}
},
}
checkRegexpMatchLoopRules = map[string]CallCheck{
"regexp.Match": loopedRegexp("regexp.Match"),
"regexp.MatchReader": loopedRegexp("regexp.MatchReader"),
"regexp.MatchString": loopedRegexp("regexp.MatchString"),
}
)
type Checker struct {
CheckGenerated bool
funcDescs *functions.Descriptions
deprecatedObjs map[types.Object]string
}
func NewChecker() *Checker {
return &Checker{}
}
func (*Checker) Name() string { return "staticcheck" }
func (*Checker) Prefix() string { return "SA" }
func (c *Checker) Checks() []lint.Check {
return []lint.Check{
{ID: "SA1000", FilterGenerated: false, Fn: c.callChecker(checkRegexpRules)},
{ID: "SA1001", FilterGenerated: false, Fn: c.CheckTemplate},
{ID: "SA1002", FilterGenerated: false, Fn: c.callChecker(checkTimeParseRules)},
{ID: "SA1003", FilterGenerated: false, Fn: c.callChecker(checkEncodingBinaryRules)},
{ID: "SA1004", FilterGenerated: false, Fn: c.CheckTimeSleepConstant},
{ID: "SA1005", FilterGenerated: false, Fn: c.CheckExec},
{ID: "SA1006", FilterGenerated: false, Fn: c.CheckUnsafePrintf},
{ID: "SA1007", FilterGenerated: false, Fn: c.callChecker(checkURLsRules)},
{ID: "SA1008", FilterGenerated: false, Fn: c.CheckCanonicalHeaderKey},
{ID: "SA1010", FilterGenerated: false, Fn: c.callChecker(checkRegexpFindAllRules)},
{ID: "SA1011", FilterGenerated: false, Fn: c.callChecker(checkUTF8CutsetRules)},
{ID: "SA1012", FilterGenerated: false, Fn: c.CheckNilContext},
{ID: "SA1013", FilterGenerated: false, Fn: c.CheckSeeker},
{ID: "SA1014", FilterGenerated: false, Fn: c.callChecker(checkUnmarshalPointerRules)},
{ID: "SA1015", FilterGenerated: false, Fn: c.CheckLeakyTimeTick},
{ID: "SA1016", FilterGenerated: false, Fn: c.CheckUntrappableSignal},
{ID: "SA1017", FilterGenerated: false, Fn: c.callChecker(checkUnbufferedSignalChanRules)},
{ID: "SA1018", FilterGenerated: false, Fn: c.callChecker(checkStringsReplaceZeroRules)},
{ID: "SA1019", FilterGenerated: false, Fn: c.CheckDeprecated},
{ID: "SA1020", FilterGenerated: false, Fn: c.callChecker(checkListenAddressRules)},
{ID: "SA1021", FilterGenerated: false, Fn: c.callChecker(checkBytesEqualIPRules)},
{ID: "SA1023", FilterGenerated: false, Fn: c.CheckWriterBufferModified},
{ID: "SA1024", FilterGenerated: false, Fn: c.callChecker(checkUniqueCutsetRules)},
{ID: "SA1025", FilterGenerated: false, Fn: c.CheckTimerResetReturnValue},
{ID: "SA2000", FilterGenerated: false, Fn: c.CheckWaitgroupAdd},
{ID: "SA2001", FilterGenerated: false, Fn: c.CheckEmptyCriticalSection},
{ID: "SA2002", FilterGenerated: false, Fn: c.CheckConcurrentTesting},
{ID: "SA2003", FilterGenerated: false, Fn: c.CheckDeferLock},
{ID: "SA3000", FilterGenerated: false, Fn: c.CheckTestMainExit},
{ID: "SA3001", FilterGenerated: false, Fn: c.CheckBenchmarkN},
{ID: "SA4000", FilterGenerated: false, Fn: c.CheckLhsRhsIdentical},
{ID: "SA4001", FilterGenerated: false, Fn: c.CheckIneffectiveCopy},
{ID: "SA4002", FilterGenerated: false, Fn: c.CheckDiffSizeComparison},
{ID: "SA4003", FilterGenerated: false, Fn: c.CheckUnsignedComparison},
{ID: "SA4004", FilterGenerated: false, Fn: c.CheckIneffectiveLoop},
{ID: "SA4006", FilterGenerated: false, Fn: c.CheckUnreadVariableValues},
{ID: "SA4008", FilterGenerated: false, Fn: c.CheckLoopCondition},
{ID: "SA4009", FilterGenerated: false, Fn: c.CheckArgOverwritten},
{ID: "SA4010", FilterGenerated: false, Fn: c.CheckIneffectiveAppend},
{ID: "SA4011", FilterGenerated: false, Fn: c.CheckScopedBreak},
{ID: "SA4012", FilterGenerated: false, Fn: c.CheckNaNComparison},
{ID: "SA4013", FilterGenerated: false, Fn: c.CheckDoubleNegation},
{ID: "SA4014", FilterGenerated: false, Fn: c.CheckRepeatedIfElse},
{ID: "SA4015", FilterGenerated: false, Fn: c.callChecker(checkMathIntRules)},
{ID: "SA4016", FilterGenerated: false, Fn: c.CheckSillyBitwiseOps},
{ID: "SA4017", FilterGenerated: false, Fn: c.CheckPureFunctions},
{ID: "SA4018", FilterGenerated: true, Fn: c.CheckSelfAssignment},
{ID: "SA4019", FilterGenerated: true, Fn: c.CheckDuplicateBuildConstraints},
{ID: "SA5000", FilterGenerated: false, Fn: c.CheckNilMaps},
{ID: "SA5001", FilterGenerated: false, Fn: c.CheckEarlyDefer},
{ID: "SA5002", FilterGenerated: false, Fn: c.CheckInfiniteEmptyLoop},
{ID: "SA5003", FilterGenerated: false, Fn: c.CheckDeferInInfiniteLoop},
{ID: "SA5004", FilterGenerated: false, Fn: c.CheckLoopEmptyDefault},
{ID: "SA5005", FilterGenerated: false, Fn: c.CheckCyclicFinalizer},
{ID: "SA5007", FilterGenerated: false, Fn: c.CheckInfiniteRecursion},
{ID: "SA6000", FilterGenerated: false, Fn: c.callChecker(checkRegexpMatchLoopRules)},
{ID: "SA6001", FilterGenerated: false, Fn: c.CheckMapBytesKey},
{ID: "SA6002", FilterGenerated: false, Fn: c.callChecker(checkSyncPoolValueRules)},
{ID: "SA6003", FilterGenerated: false, Fn: c.CheckRangeStringRunes},
// {ID: "SA6004", FilterGenerated: false, Fn: c.CheckSillyRegexp},
{ID: "SA9001", FilterGenerated: false, Fn: c.CheckDubiousDeferInChannelRangeLoop},
{ID: "SA9002", FilterGenerated: false, Fn: c.CheckNonOctalFileMode},
{ID: "SA9003", FilterGenerated: false, Fn: c.CheckEmptyBranch},
{ID: "SA9004", FilterGenerated: false, Fn: c.CheckMissingEnumTypesInDeclaration},
}
// "SA5006": c.CheckSliceOutOfBounds,
// "SA4007": c.CheckPredeterminedBooleanExprs,
}
func (c *Checker) findDeprecated(prog *lint.Program) {
var docs []*ast.CommentGroup
var names []*ast.Ident
doDocs := func(pkg *packages.Package, names []*ast.Ident, docs []*ast.CommentGroup) {
var alt string
for _, doc := range docs {
if doc == nil {
continue
}
parts := strings.Split(doc.Text(), "\n\n")
last := parts[len(parts)-1]
if !strings.HasPrefix(last, "Deprecated: ") {
continue
}
alt = last[len("Deprecated: "):]
alt = strings.Replace(alt, "\n", " ", -1)
break
}
if alt == "" {
return
}
for _, name := range names {
obj := pkg.TypesInfo.ObjectOf(name)
c.deprecatedObjs[obj] = alt
}
}
for _, pkg := range prog.AllPackages {
for _, f := range pkg.Syntax {
fn := func(node ast.Node) bool {
if node == nil {
return true
}
var ret bool
switch node := node.(type) {
case *ast.GenDecl:
switch node.Tok {
case token.TYPE, token.CONST, token.VAR:
docs = append(docs, node.Doc)
return true
default:
return false
}
case *ast.FuncDecl:
docs = append(docs, node.Doc)
names = []*ast.Ident{node.Name}
ret = false
case *ast.TypeSpec:
docs = append(docs, node.Doc)
names = []*ast.Ident{node.Name}
ret = true
case *ast.ValueSpec:
docs = append(docs, node.Doc)
names = node.Names
ret = false
case *ast.File:
return true
case *ast.StructType:
for _, field := range node.Fields.List {
doDocs(pkg, field.Names, []*ast.CommentGroup{field.Doc})
}
return false
case *ast.InterfaceType:
for _, field := range node.Methods.List {
doDocs(pkg, field.Names, []*ast.CommentGroup{field.Doc})
}
return false
default:
return false
}
if len(names) == 0 || len(docs) == 0 {
return ret
}
doDocs(pkg, names, docs)
docs = docs[:0]
names = nil
return ret
}
ast.Inspect(f, fn)
}
}
}
func (c *Checker) Init(prog *lint.Program) {
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
c.funcDescs = functions.NewDescriptions(prog.SSA)
for _, fn := range prog.AllFunctions {
if fn.Blocks != nil {
applyStdlibKnowledge(fn)
ssa.OptimizeBlocks(fn)
}
}
wg.Done()
}()
go func() {
c.deprecatedObjs = map[types.Object]string{}
c.findDeprecated(prog)
wg.Done()
}()
wg.Wait()
}
func (c *Checker) isInLoop(b *ssa.BasicBlock) bool {
sets := c.funcDescs.Get(b.Parent()).Loops
for _, set := range sets {
if set[b] {
return true
}
}
return false
}
func applyStdlibKnowledge(fn *ssa.Function) {
if len(fn.Blocks) == 0 {
return
}
// comma-ok receiving from a time.Tick channel will never return
// ok == false, so any branching on the value of ok can be
// replaced with an unconditional jump. This will primarily match
// `for range time.Tick(x)` loops, but it can also match
// user-written code.
for _, block := range fn.Blocks {
if len(block.Instrs) < 3 {
continue
}
if len(block.Succs) != 2 {
continue
}
var instrs []*ssa.Instruction
for i, ins := range block.Instrs {
if _, ok := ins.(*ssa.DebugRef); ok {
continue
}
instrs = append(instrs, &block.Instrs[i])
}
for i, ins := range instrs {
unop, ok := (*ins).(*ssa.UnOp)
if !ok || unop.Op != token.ARROW {
continue
}
call, ok := unop.X.(*ssa.Call)
if !ok {
continue
}
if !IsCallTo(call.Common(), "time.Tick") {
continue
}
ex, ok := (*instrs[i+1]).(*ssa.Extract)
if !ok || ex.Tuple != unop || ex.Index != 1 {
continue
}
ifstmt, ok := (*instrs[i+2]).(*ssa.If)
if !ok || ifstmt.Cond != ex {
continue
}
*instrs[i+2] = ssa.NewJump(block)
succ := block.Succs[1]
block.Succs = block.Succs[0:1]
succ.RemovePred(block)
}
}
}
func hasType(j *lint.Job, expr ast.Expr, name string) bool {
T := TypeOf(j, expr)
return IsType(T, name)
}
func (c *Checker) CheckUntrappableSignal(j *lint.Job) {
fn := func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
if !IsCallToAnyAST(j, call,
"os/signal.Ignore", "os/signal.Notify", "os/signal.Reset") {
return true
}
for _, arg := range call.Args {
if conv, ok := arg.(*ast.CallExpr); ok && isName(j, conv.Fun, "os.Signal") {
arg = conv.Args[0]
}
if isName(j, arg, "os.Kill") || isName(j, arg, "syscall.SIGKILL") {
j.Errorf(arg, "%s cannot be trapped (did you mean syscall.SIGTERM?)", Render(j, arg))
}
if isName(j, arg, "syscall.SIGSTOP") {
j.Errorf(arg, "%s signal cannot be trapped", Render(j, arg))
}
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckTemplate(j *lint.Job) {
fn := func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
var kind string
if IsCallToAST(j, call, "(*text/template.Template).Parse") {
kind = "text"
} else if IsCallToAST(j, call, "(*html/template.Template).Parse") {
kind = "html"
} else {
return true
}
sel := call.Fun.(*ast.SelectorExpr)
if !IsCallToAST(j, sel.X, "text/template.New") &&
!IsCallToAST(j, sel.X, "html/template.New") {
// TODO(dh): this is a cheap workaround for templates with
// different delims. A better solution with less false
// negatives would use data flow analysis to see where the
// template comes from and where it has been
return true
}
s, ok := ExprToString(j, call.Args[Arg("(*text/template.Template).Parse.text")])
if !ok {
return true
}
var err error
switch kind {
case "text":
_, err = texttemplate.New("").Parse(s)
case "html":
_, err = htmltemplate.New("").Parse(s)
}
if err != nil {
// TODO(dominikh): whitelist other parse errors, if any
if strings.Contains(err.Error(), "unexpected") {
j.Errorf(call.Args[Arg("(*text/template.Template).Parse.text")], "%s", err)
}
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckTimeSleepConstant(j *lint.Job) {
fn := func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
if !IsCallToAST(j, call, "time.Sleep") {
return true
}
lit, ok := call.Args[Arg("time.Sleep.d")].(*ast.BasicLit)
if !ok {
return true
}
n, err := strconv.Atoi(lit.Value)
if err != nil {
return true
}
if n == 0 || n > 120 {
// time.Sleep(0) is a seldom used pattern in concurrency
// tests. >120 might be intentiona 10BC8 l. 120 was chosen
// because the user could've meant 2 minutes.
return true
}
recommendation := "time.Sleep(time.Nanosecond)"
if n != 1 {
recommendation = fmt.Sprintf("time.Sleep(%d * time.Nanosecond)", n)
}
j.Errorf(call.Args[Arg("time.Sleep.d")],
"sleeping for %d nanoseconds is probably a bug. Be explicit if it isn't: %s", n, recommendation)
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckWaitgroupAdd(j *lint.Job) {
fn := func(node ast.Node) bool {
g, ok := node.(*ast.GoStmt)
if !ok {
return true
}
fun, ok := g.Call.Fun.(*ast.FuncLit)
if !ok {
return true
}
if len(fun.Body.List) == 0 {
return true
}
stmt, ok := fun.Body.List[0].(*ast.ExprStmt)
if !ok {
return true
}
call, ok := stmt.X.(*ast.CallExpr)
if !ok {
return true
}
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
fn, ok := ObjectOf(j, sel.Sel).(*types.Func)
if !ok {
return true
}
if fn.FullName() == "(*sync.WaitGroup).Add" {
j.Errorf(sel, "should call %s before starting the goroutine to avoid a race",
Render(j, stmt))
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckInfiniteEmptyLoop(j *lint.Job) {
fn := func(node ast.Node) bool {
loop, ok := node.(*ast.ForStmt)
if !ok || len(loop.Body.List) != 0 || loop.Post != nil {
return true
}
if loop.Init != nil {
// TODO(dh): this isn't strictly necessary, it just makes
// the check easier.
return true
}
// An empty loop is bad news in two cases: 1) The loop has no
// condition. In that case, it's just a loop that spins
// forever and as fast as it can, keeping a core busy. 2) The
// loop condition only consists of variable or field reads and
// operators on those. The only way those could change their
// value is with unsynchronised access, which constitutes a
// data race.
//
// If the condition contains any function calls, its behaviour
// is dynamic and the loop might terminate. Similarly for
// channel receives.
if loop.Cond != nil && hasSideEffects(loop.Cond) {
return true
}
j.Errorf(loop, "this loop will spin, using 100%% CPU")
if loop.Cond != nil {
j.Errorf(loop, "loop condition never changes or has a race condition")
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckDeferInInfiniteLoop(j *lint.Job) {
fn := func(node ast.Node) bool {
mightExit := false
var defers []ast.Stmt
loop, ok := node.(*ast.ForStmt)
if !ok || loop.Cond != nil {
return true
}
fn2 := func(node ast.Node) bool {
switch stmt := node.(type) {
case *ast.ReturnStmt:
mightExit = true
case *ast.BranchStmt:
// TODO(dominikh): if this sees a break in a switch or
// select, it doesn't check if it breaks the loop or
// just the select/switch. This causes some false
// negatives.
if stmt.Tok == token.BREAK {
mightExit = true
}
case *ast.DeferStmt:
defers = append(defers, stmt)
case *ast.FuncLit:
// Don't look into function bodies
return false
}
return true
}
ast.Inspect(loop.Body, fn2)
if mightExit {
return true
}
for _, stmt := range defers {
j.Errorf(stmt, "defers in this infinite loop will never run")
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckDubiousDeferInChannelRangeLoop(j *lint.Job) {
fn := func(node ast.Node) bool {
loop, ok := node.(*ast.RangeStmt)
if !ok {
return true
}
typ := TypeOf(j, loop.X)
_, ok = typ.Underlying().(*types.Chan)
if !ok {
return true
}
fn2 := func(node ast.Node) bool {
switch stmt := node.(type) {
case *ast.DeferStmt:
j.Errorf(stmt, "defers in this range loop won't run unless the channel gets closed")
case *ast.FuncLit:
// Don't look into function bodies
return false
}
return true
}
ast.Inspect(loop.Body, fn2)
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckTestMainExit(j *lint.Job) {
fn := func(node ast.Node) bool {
if !isTestMain(j, node) {
return true
}
arg := ObjectOf(j, node.(*ast.FuncDecl).Type.Params.List[0].Names[0])
callsRun := false
fn2 := func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
ident, ok := sel.X.(*ast.Ident)
if !ok {
return true
}
if arg != ObjectOf(j, ident) {
return true
}
if sel.Sel.Name == "Run" {
callsRun = true
return false
}
return true
}
ast.Inspect(node.(*ast.FuncDecl).Body, fn2)
callsExit := false
fn3 := func(node ast.Node) bool {
if IsCallToAST(j, node, "os.Exit") {
callsExit = true
return false
}
return true
}
ast.Inspect(node.(*ast.FuncDecl).Body, fn3)
if !callsExit && callsRun {
j.Errorf(node, "TestMain should call os.Exit to set exit code")
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func isTestMain(j *lint.Job, node ast.Node) bool {
decl, ok := node.(*ast.FuncDecl)
if !ok {
return false
}
if decl.Name.Name != "TestMain" {
return false
}
if len(decl.Type.Params.List) != 1 {
return false
}
arg := decl.Type.Params.List[0]
if len(arg.Names) != 1 {
return false
}
return IsOfType(j, arg.Type, "*testing.M")
}
func (c *Checker) CheckExec(j *lint.Job) {
fn := func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
if !IsCallToAST(j, call, "os/exec.Command") {
return true
}
val, ok := ExprToString(j, call.Args[Arg("os/exec.Command.name")])
if !ok {
return true
}
if !strings.Contains(val, " ") || strings.Contains(val, `\`) || strings.Contains(val, "/") {
return true
}
j.Errorf(call.Args[Arg("os/exec.Command.name")],
"first argument to exec.Command looks like a shell command, but a program name or path are expected")
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckLoopEmptyDefault(j *lint.Job) {
fn := func(node ast.Node) bool {
loop, ok := node.(*ast.ForStmt)
if !ok || len(loop.Body.List) != 1 || loop.Cond != nil || loop.Init != nil {
return true
}
sel, ok := loop.Body.List[0].(*ast.SelectStmt)
if !ok {
return true
}
for _, c := range sel.Body.List {
if comm, ok := c.(*ast.CommClause); ok && comm.Comm == nil && len(comm.Body) == 0 {
j.Errorf(comm, "should not have an empty default case in a for+select loop. The loop will spin.")
}
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckLhsRhsIdentical(j *lint.Job) {
fn := func(node ast.Node) bool {
op, ok := node.(*ast.BinaryExpr)
if !ok {
return true
}
switch op.Op {
case token.EQL, token.NEQ:
if basic, ok := TypeOf(j, op.X).(*types.Basic); ok {
if kind := basic.Kind(); kind == types.Float32 || kind == types.Float64 {
// f == f and f != f might be used to check for NaN
return true
}
}
case token.SUB, token.QUO, token.AND, token.REM, token.OR, token.XOR, token.AND_NOT,
token.LAND, token.LOR, token.LSS, token.GTR, token.LEQ, token.GEQ:
default:
// For some ops, such as + and *, it can make sense to
// have identical operands
return true
}
if Render(j, op.X) != Render(j, op.Y) {
return true
}
j.Errorf(op, "identical expressions on the left and right side of the '%s' operator", op.Op)
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckScopedBreak(j *lint.Job) {
fn := func(node ast.Node) bool {
var body *ast.BlockStmt
switch node := node.(type) {
case *ast.ForStmt:
body = node.Body
case *ast.RangeStmt:
body = node.Body
default:
return true
}
for _, stmt := range body.List {
var blocks [][]ast.Stmt
switch stmt := stmt.(type) {
case *ast.SwitchStmt:
for _, c := range stmt.Body.List {
blocks = append(blocks, c.(*ast.CaseClause).Body)
}
case *ast.SelectStmt:
for _, c := range stmt.Body.List {
blocks = append(blocks, c.(*ast.CommClause).Body)
}
default:
continue
}
for _, body := range blocks {
if len(body) == 0 {
continue
}
lasts := []ast.Stmt{body[len(body)-1]}
// TODO(dh): unfold all levels of nested block
// statements, not just a single level if statement
if ifs, ok := lasts[0].(*ast.IfStmt); ok {
if len(ifs.Body.List) == 0 {
continue
}
lasts[0] = ifs.Body.List[len(ifs.Body.List)-1]
if block, ok := ifs.Else.(*ast.BlockStmt); ok {
if len(block.List) != 0 {
lasts = append(lasts, block.List[len(block.List)-1])
}
}
}
for _, last := range lasts {
branch, ok := last.(*ast.BranchStmt)
if !ok || branch.Tok != token.BREAK || branch.Label != nil {
continue
}
j.Errorf(branch, "ineffective break statement. Did you mean to break out of the outer loop?")
}
}
}
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckUnsafePrintf(j *lint.Job) {
fn := func(node ast.Node) bool {
call, ok := node.(*ast.CallExpr)
if !ok {
return true
}
if !IsCallToAnyAST(j, call, "fmt.Printf", "fmt.Sprintf", "log.Printf") {
return true
}
if len(call.Args) != 1 {
return true
}
switch call.Args[Arg("fmt.Printf.format")].(type) {
case *ast.CallExpr, *ast.Ident:
default:
return true
}
j.Errorf(call.Args[Arg("fmt.Printf.format")],
"printf-style function with dynamic first argument and no further arguments should use print-style function instead")
return true
}
for _, f := range j.Program.Files {
ast.Inspect(f, fn)
}
}
func (c *Checker) CheckEarlyDefer(j *lint.Job) {
fn := func(node ast.Node) bool {
block, ok := node.(*ast.BlockStmt)
if !ok {
return true
}
if len(block.List) < 2 {
return true
}
for i, stmt := range block.List {
if i == len(block.List)-1 {
break
}
assign, ok := stmt.(*ast.AssignStmt)
if !ok {
continue
}
if len(assign.Rhs) != 1 {
continue
}
if len(assign.Lhs) < 2 {
continue
}
0