diff --git a/cmd/makedocs/main.go b/cmd/makedocs/main.go index 318d370b2..ec8466857 100644 --- a/cmd/makedocs/main.go +++ b/cmd/makedocs/main.go @@ -18,6 +18,7 @@ const ( func main() { tmpl := parseTemplate( "overview.md.tmpl", + "checker.partial.tmpl", "checker_tr.partial.tmpl") buf := bytes.Buffer{} diff --git a/docs/overview.md b/docs/overview.md index fb8302f99..9740311d9 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -60,11 +60,6 @@ They also detect code that may be correct, but looks suspicious. dupArg Detects suspicious duplicated arguments - - :white_check_mark: - dupAssign - - Detects suspicious re-assignments :heavy_check_mark: dupBranchBody @@ -357,7 +352,7 @@ can make your code run slower than it could be. - + ## appendAssign [ @@ -368,6 +363,7 @@ Detects suspicious append result assignments. + **Before:** ```go p.positives = append(p.negatives, x) @@ -381,7 +377,8 @@ p.negatives = append(p.negatives, y) ``` - + + ## appendCombine [ @@ -392,6 +389,7 @@ Detects `append` chains to the same slice that can be done in a single `append` + **Before:** ```go xs = append(xs, 1) @@ -404,7 +402,8 @@ xs = append(xs, 1, 2) ``` - + + ## argOrder [ @@ -416,6 +415,7 @@ Detects suspicious arguments order. + **Before:** ```go strings.HasPrefix("#", userpass) @@ -427,7 +427,8 @@ strings.HasPrefix(userpass, "#") ``` - + + ## assignOp [ @@ -438,6 +439,7 @@ Detects assignments that can be simplified by using assignment operators. + **Before:** ```go x = x * 2 @@ -449,7 +451,8 @@ x *= 2 ``` - + + ## badCond [ @@ -461,6 +464,7 @@ Detects suspicious condition expressions. + **Before:** ```go for i := 0; i > n; i++ { @@ -476,7 +480,8 @@ for i := 0; i < n; i++ { ``` - + + ## boolExprSimplify [ @@ -488,6 +493,7 @@ Detects bool expressions that can be simplified. + **Before:** ```go a := !(elapsed >= expectElapsedMin) @@ -501,7 +507,8 @@ b := (x) == (y) ``` - + + ## builtinShadow [ @@ -513,6 +520,7 @@ Detects when predeclared identifiers shadowed in assignments. + **Before:** ```go len := 10 @@ -524,7 +532,8 @@ length := 10 ``` - + + ## captLocal [ @@ -535,6 +544,7 @@ Detects capitalized names for local variables. + **Before:** ```go func f(IN int, OUT *int) (ERR error) {} @@ -546,7 +556,10 @@ func f(in int, out *int) (err error) {} ``` - +Checker parameters: +* `@captLocal.paramsOnly` whether to restrict checker to params only (default true) + + ## caseOrder [ @@ -557,6 +570,7 @@ Detects erroneous case order inside switch statements. + **Before:** ```go switch x.(type) { @@ -578,7 +592,8 @@ case ast.Expr: ``` - + + ## codegenComment [ @@ -590,6 +605,7 @@ Detects malformed 'code generated' file comments. + **Before:** ```go // This file was automatically generated by foogen @@ -601,7 +617,8 @@ Detects malformed 'code generated' file comments. ``` - + + ## commentFormatting [ @@ -613,6 +630,7 @@ Detects comments with non-idiomatic formatting. + **Before:** ```go //This is a comment @@ -624,7 +642,8 @@ Detects comments with non-idiomatic formatting. ``` - + + ## commentedOutCode [ @@ -636,6 +655,7 @@ Detects commented-out code inside function bodies. + **Before:** ```go // fmt.Println("Debugging hard") @@ -648,7 +668,8 @@ foo(1, 2) ``` - + + ## commentedOutImport [ @@ -660,6 +681,7 @@ Detects commented-out imports. + **Before:** ```go import ( @@ -676,7 +698,8 @@ import ( ``` - + + ## defaultCaseOrder [ @@ -687,6 +710,7 @@ Detects when default case in switch isn't on 1st or last position. + **Before:** ```go switch { @@ -712,7 +736,8 @@ default: // <- last case (could also be the first one) ``` - + + ## deprecatedComment [ @@ -724,6 +749,7 @@ Detects malformed 'deprecated' doc-comments. + **Before:** ```go // deprecated, use FuncNew instead @@ -737,7 +763,8 @@ func FuncOld() int ``` - + + ## docStub [ @@ -749,6 +776,7 @@ Detects comments that silence go lint complaints about doc-comment. + **Before:** ```go // Foo ... @@ -766,7 +794,8 @@ func Foo() {} ``` - + + ## dupArg [ @@ -777,6 +806,7 @@ Detects suspicious duplicated arguments. + **Before:** ```go copy(dst, dst) @@ -788,34 +818,8 @@ copy(dst, src) ``` - -## dupAssign -[ - **diagnostic** - **experimental** ] - -Detects suspicious re-assignments. - - - - -**Before:** -```go -xs[0] = i+0 -xs[1] = i+1 -xs[0] = i+2 -``` - -**After:** -```go -xs[0] = i+0 -xs[1] = i+1 -xs[2] = i+2 -``` - - - + ## dupBranchBody [ @@ -826,6 +830,7 @@ Detects duplicated branch bodies inside conditional statements. + **Before:** ```go if cond { @@ -845,7 +850,8 @@ if cond { ``` - + + ## dupCase [ @@ -856,6 +862,7 @@ Detects duplicated case clauses inside switch statements. + **Before:** ```go switch x { @@ -871,7 +878,8 @@ case ys[0], ys[1], ys[2], ys[3], ys[4]: ``` - + + ## dupSubExpr [ @@ -882,6 +890,7 @@ Detects suspicious duplicated sub-expressions. + **Before:** ```go sort.Slice(xs, func(i, j int) bool { @@ -897,7 +906,8 @@ sort.Slice(xs, func(i, j int) bool { ``` - + + ## elseif [ @@ -908,6 +918,7 @@ Detects else with nested if statement that can be replaced with else-if. + **Before:** ```go if cond1 { @@ -925,7 +936,10 @@ if cond1 { ``` - +Checker parameters: +* `@elseif.skipBalanced` whether to skip balanced if-else pairs (default true) + + ## emptyFallthrough [ @@ -937,6 +951,7 @@ Detects fallthrough that can be avoided by using multi case values. + **Before:** ```go switch kind { @@ -956,7 +971,8 @@ case reflect.Int, reflect.Int32: ``` - + + ## emptyStringTest [ @@ -968,6 +984,8 @@ Detects empty string checks that can be written more idiomatically. +> See https://dmitri.shuralyov.com/idiomatic-go#empty-string-check. + **Before:** ```go len(s) == 0 @@ -979,9 +997,8 @@ s == "" ``` -**Note:** -See https://dmitri.shuralyov.com/idiomatic-go#empty-string-check. - + + ## equalFold [ @@ -993,6 +1010,7 @@ Detects unoptimal strings/bytes case-insensitive comparison. + **Before:** ```go strings.ToLower(x) == strings.ToLower(y) @@ -1004,7 +1022,8 @@ strings.EqualFold(x, y) ``` - + + ## exitAfterDefer [ @@ -1016,6 +1035,7 @@ Detects calls to exit/fatal inside functions that use defer. + **Before:** ```go defer os.Remove(filename) @@ -1034,7 +1054,8 @@ if bad { ``` - + + ## flagDeref [ @@ -1045,6 +1066,9 @@ Detects immediate dereferencing of `flag` package pointers. Suggests to use pointer to array to avoid the copy using `&` on range expression. +> Dereferencing returned pointers will lead to hard to find errors +where flag values are not updated after flag.Parse(). + **Before:** ```go b := *flag.Bool("b", false, "b docs") @@ -1057,10 +1081,8 @@ flag.BoolVar(&b, "b", false, "b docs") ``` -**Note:** -Dereferencing returned pointers will lead to hard to find errors -where flag values are not updated after flag.Parse(). - + + ## flagName [ @@ -1072,6 +1094,7 @@ Detects flag names with whitespace. + **Before:** ```go b := flag.Bool(" foo ", false, "description") @@ -1083,7 +1106,8 @@ b := flag.Bool("foo", false, "description") ``` - + + ## hexLiteral [ @@ -1095,6 +1119,7 @@ Detects hex literals that have mixed case letter digits. + **Before:** ```go x := 0X12 @@ -1111,7 +1136,8 @@ y := 0xFF ``` - + + ## hugeParam [ @@ -1122,6 +1148,7 @@ Detects params that incur excessive amount of copying. + **Before:** ```go func f(x [1024]int) {} @@ -1133,7 +1160,10 @@ func f(x *[1024]int) {} ``` - +Checker parameters: +* `@hugeParam.sizeThreshold` size in bytes that makes the warning trigger (default 80) + + ## ifElseChain [ @@ -1144,6 +1174,10 @@ Detects repeated if-else statements and suggests to replace them with switch sta +> Permits single else or else-if; repeated else-if or else + else-if +will trigger suggestion to use switch statement. +See [EffectiveGo#switch](https://golang.org/doc/effective_go.html#switch). + **Before:** ```go if cond1 { @@ -1168,11 +1202,8 @@ default: ``` -**Note:** -Permits single else or else-if; repeated else-if or else + else-if -will trigger suggestion to use switch statement. -See [EffectiveGo#switch](https://golang.org/doc/effective_go.html#switch). - + + ## importShadow [ @@ -1184,6 +1215,7 @@ Detects when imported package names shadowed in the assignments. + **Before:** ```go // "path/filepath" is imported. @@ -1196,7 +1228,8 @@ filename := "foo.txt" ``` - + + ## indexAlloc [ @@ -1207,6 +1240,8 @@ Detects strings.Index calls that may cause unwanted allocs. +> See Go issue for details: https://github.com/golang/go/issues/25864 + **Before:** ```go strings.Index(string(x), y) @@ -1218,9 +1253,8 @@ bytes.Index(x, []byte(y)) ``` -**Note:** -See Go issue for details: https://github.com/golang/go/issues/25864 - + + ## initClause [ @@ -1233,6 +1267,7 @@ Detects non-assignment statements inside if/switch init clause. + **Before:** ```go if sideEffect(); cond { @@ -1247,7 +1282,8 @@ if cond { ``` - + + ## methodExprCall [ @@ -1259,6 +1295,7 @@ Detects method expression call that can be replaced with a method call. + **Before:** ```go f := foo{} @@ -1272,7 +1309,8 @@ f.bar() ``` - + + ## nestingReduce [ @@ -1285,6 +1323,7 @@ Finds where nesting level could be reduced. + **Before:** ```go for _, v := range a { @@ -1305,7 +1344,10 @@ for _, v := range a { ``` - +Checker parameters: +* `@nestingReduce.bodyWidth` min number of statements inside a branch to trigger a warning (default 5) + + ## nilValReturn [ @@ -1317,6 +1359,7 @@ Detects return statements those results evaluate to nil. + **Before:** ```go if err == nil { @@ -1337,7 +1380,8 @@ if err != nil { ``` - + + ## offBy1 [ @@ -1349,6 +1393,7 @@ Detects various off-by-one kind of errors. + **Before:** ```go xs[len(xs)] @@ -1360,7 +1405,8 @@ xs[len(xs)-1] ``` - + + ## paramTypeCombine [ @@ -1372,6 +1418,7 @@ Detects if function parameters could be combined by type and suggest the way to + **Before:** ```go func foo(a, b int, c, d int, e, f int, g int) {} @@ -1383,7 +1430,8 @@ func foo(a, b, c, d, e, f, g int) {} ``` - + + ## ptrToRefParam [ @@ -1396,6 +1444,7 @@ Detects input and output parameters that have a type of pointer to referential t + **Before:** ```go func f(m *map[string]int) (*chan *int) @@ -1407,7 +1456,8 @@ func f(m map[string]int) (chan *int) ``` - + + ## rangeExprCopy [ @@ -1418,6 +1468,8 @@ Detects expensive copies of `for` loop range expressions. Suggests to use pointer to array to avoid the copy using `&` on range expression. +> See Go issue for details: https://github.com/golang/go/issues/15812. + **Before:** ```go var xs [2048]byte @@ -1435,9 +1487,11 @@ for _, x := range &xs { // No copy ``` -**Note:** -See Go issue for details: https://github.com/golang/go/issues/15812. - +Checker parameters: +* `@rangeExprCopy.sizeThreshold` size in bytes that makes the warning trigger (default 512) +* `@rangeExprCopy.skipTestFuncs` whether to check test functions (default true) + + ## rangeValCopy [ @@ -1448,6 +1502,7 @@ Detects loops that copy big objects during each iteration. Suggests to use index access or take address and make use pointer instead. + **Before:** ```go xs := make([][1024]byte, length) @@ -1466,7 +1521,11 @@ for i := range xs { ``` - +Checker parameters: +* `@rangeValCopy.sizeThreshold` size in bytes that makes the warning trigger (default 128) +* `@rangeValCopy.skipTestFuncs` whether to check test functions (default true) + + ## regexpMust [ @@ -1477,6 +1536,7 @@ Detects `regexp.Compile*` that can be replaced with `regexp.MustCompile*`. + **Before:** ```go re, _ := regexp.Compile("const pattern") @@ -1488,7 +1548,8 @@ re := regexp.MustCompile("const pattern") ``` - + + ## singleCaseSwitch [ @@ -1499,6 +1560,7 @@ Detects switch statements that could be better written as if statement. + **Before:** ```go switch x := x.(type) { @@ -1515,7 +1577,8 @@ if x, ok := x.(int); ok { ``` - + + ## sloppyLen [ @@ -1526,6 +1589,7 @@ Detects usage of `len` when result is obvious or doesn't make sense. + **Before:** ```go len(arr) >= 0 // Sloppy @@ -1540,7 +1604,8 @@ len(arr) == 0 ``` - + + ## sloppyReassign [ @@ -1552,6 +1617,7 @@ Detects suspicious/confusing re-assignments. + **Before:** ```go if err = f(); err != nil { return err } @@ -1563,7 +1629,8 @@ if err := f(); err != nil { return err } ``` - + + ## stringXbytes [ @@ -1575,6 +1642,7 @@ Detects redundant conversions between string and []byte. + **Before:** ```go copy(b, []byte(s)) @@ -1586,7 +1654,8 @@ copy(b, s) ``` - + + ## switchTrue [ @@ -1597,6 +1666,7 @@ Detects switch-over-bool statements that use explicit `true` tag value. + **Before:** ```go switch true { @@ -1612,7 +1682,8 @@ case x > y: ``` - + + ## typeAssertChain [ @@ -1624,6 +1695,7 @@ Detects repeated type assertions and suggests to replace them with type switch s + **Before:** ```go if x, ok := v.(T1); ok { @@ -1648,7 +1720,8 @@ default: ``` - + + ## typeSwitchVar [ @@ -1659,6 +1732,7 @@ Detects type switches that can benefit from type guard clause with variable. + **Before:** ```go switch v.(type) { @@ -1684,7 +1758,8 @@ default: ``` - + + ## typeUnparen [ @@ -1696,6 +1771,7 @@ Detects unneded parenthesis inside type expressions and suggests to remove them. + **Before:** ```go type foo [](func([](func()))) @@ -1707,7 +1783,8 @@ type foo []func([]func()) ``` - + + ## underef [ @@ -1718,6 +1795,7 @@ Detects dereference expressions that can be omitted. + **Before:** ```go (*k).field = 5 @@ -1731,7 +1809,10 @@ v := a[5] ``` - +Checker parameters: +* `@underef.skipRecvDeref` whether to skip (*x).method() calls where x is a pointer receiver (default true) + + ## unlabelStmt [ @@ -1743,6 +1824,7 @@ Detects redundant statement labels. + **Before:** ```go derp: @@ -1763,7 +1845,8 @@ for x := range xs { ``` - + + ## unlambda [ @@ -1774,6 +1857,7 @@ Detects function literals that can be simplified. + **Before:** ```go func(x int) int { return fn(x) } @@ -1785,7 +1869,8 @@ fn ``` - + + ## unnamedResult [ @@ -1798,6 +1883,7 @@ Detects unnamed results that may benefit from names. + **Before:** ```go func f() (float64, float64) @@ -1809,7 +1895,10 @@ func f() (x, y float64) ``` - +Checker parameters: +* `@unnamedResult.checkExported` whether to check exported functions (default false) + + ## unnecessaryBlock [ @@ -1822,6 +1911,7 @@ Detects unnecessary braced statement blocks. + **Before:** ```go x := 1 @@ -1837,7 +1927,8 @@ print(x) ``` - + + ## unslice [ @@ -1848,6 +1939,7 @@ Detects slice expressions that can be simplified to sliced expression itself. + **Before:** ```go f(s[:]) // s is string @@ -1861,7 +1953,8 @@ copy(b, values...) ``` - + + ## valSwap [ @@ -1873,6 +1966,7 @@ Detects value swapping code that are not using parallel assignment. + **Before:** ```go tmp := *x @@ -1886,7 +1980,8 @@ tmp := *x ``` - + + ## weakCond [ @@ -1898,6 +1993,7 @@ Detects conditions that are unsafe due to not being exhaustive. + **Before:** ```go xs != nil && xs[0] != nil @@ -1909,7 +2005,8 @@ len(xs) != 0 && xs[0] != nil ``` - + + ## wrapperFunc [ @@ -1921,6 +2018,7 @@ Detects function calls that can be replaced with convenience wrappers. + **Before:** ```go wg.Add(-1) @@ -1932,7 +2030,8 @@ wg.Done() ``` - + + ## yodaStyleExpr [ @@ -1944,6 +2043,7 @@ Detects Yoda style expressions and suggests to replace them. + **Before:** ```go return nil != ptr @@ -1954,3 +2054,4 @@ return nil != ptr return ptr != nil ``` + diff --git a/docs/templates/checker.partial.tmpl b/docs/templates/checker.partial.tmpl new file mode 100644 index 000000000..93372073d --- /dev/null +++ b/docs/templates/checker.partial.tmpl @@ -0,0 +1,35 @@ +{{ define "checker" -}} + + +## {{ .Name }} + +[{{ range $tag := .Tags }} + **{{ $tag }}** +{{- end }} ] + +{{ .Summary -}}. + +{{ .Details }} + +{{ if .Note }} +> {{ .Note }} +{{- end }} + +**Before:** +```go +{{ .Before }} +``` + +**After:** +```go +{{ .After }} +``` + +{{ if .Params }} +Checker parameters: +{{- range $key, $_ := .Params }} +* `@{{$.Name}}.{{$key}}` {{.Usage}} (default {{.Value}}) +{{- end }} +{{- end }} + +{{- end }} \ No newline at end of file diff --git a/docs/templates/overview.md.tmpl b/docs/templates/overview.md.tmpl index 1f85159d4..3330ac193 100644 --- a/docs/templates/overview.md.tmpl +++ b/docs/templates/overview.md.tmpl @@ -69,32 +69,7 @@ can make your code run slower than it could be. {{ range .Checkers }} - -## {{ .Name }} - -[{{ range $tag := .Tags }} - **{{ $tag }}** -{{- end }} ] - -{{ .Summary -}}. - -{{ .Details }} - - -**Before:** -```go -{{ .Before }} -``` - -**After:** -```go -{{ .After }} -``` - -{{ if .Note }} -**Note:** -{{ .Note }} -{{- end }} -{{- end }} + {{ template "checker" . }} +{{ end }} {{- end }} \ No newline at end of file