toolshonnef.co/go/tools/simple Index | Files

package simple

import "honnef.co/go/tools/simple"

Package simple contains analyzes that simplify code. All suggestions made by these analyzes are intended to result in objectively simpler code, and following their advice is recommended.

Index

Variables

var Analyzers = lint.InitializeAnalyzers(Docs, map[string]*analysis.Analyzer{
	"S1000": {
		Run:      CheckSingleCaseSelect,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1001": {
		Run:      CheckLoopCopy,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1002": {
		Run:      CheckIfBoolCmp,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1003": {
		Run:      CheckStringsContains,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1004": {
		Run:      CheckBytesCompare,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1005": {
		Run:      CheckUnnecessaryBlank,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1006": {
		Run:      CheckForTrue,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1007": {
		Run:      CheckRegexpRaw,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1008": {
		Run:      CheckIfReturn,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1009": {
		Run:      CheckRedundantNilCheckWithLen,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1010": {
		Run:      CheckSlicing,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1011": {
		Run:      CheckLoopAppend,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1012": {
		Run:      CheckTimeSince,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1016": {
		Run:      CheckSimplerStructConversion,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1017": {
		Run:      CheckTrim,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1018": {
		Run:      CheckLoopSlide,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1019": {
		Run:      CheckMakeLenCap,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1020": {
		Run:      CheckAssertNotNil,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1021": {
		Run:      CheckDeclareAssign,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1023": {
		Run:      CheckRedundantBreak,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1024": {
		Run:      CheckTimeUntil,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1025": {
		Run:      CheckRedundantSprintf,
		Requires: []*analysis.Analyzer{buildir.Analyzer, inspect.Analyzer, facts.Generated},
	},
	"S1028": {
		Run:      CheckErrorsNewSprintf,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1029": {
		Run:      CheckRangeStringRunes,
		Requires: []*analysis.Analyzer{buildir.Analyzer},
	},
	"S1030": {
		Run:      CheckBytesBufferConversions,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1031": {
		Run:      CheckNilCheckAroundRange,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1032": {
		Run:      CheckSortHelpers,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1033": {
		Run:      CheckGuardedDelete,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1034": {
		Run:      CheckSimplifyTypeSwitch,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1035": {
		Run:      CheckRedundantCanonicalHeaderKey,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1036": {
		Run:      CheckUnnecessaryGuard,
		Requires: []*analysis.Analyzer{inspect.Analyzer},
	},
	"S1037": {
		Run:      CheckElaborateSleep,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1038": {
		Run:      CheckPrintSprintf,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1039": {
		Run:      CheckSprintLiteral,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
	"S1040": {
		Run:      CheckSameTypeTypeAssertion,
		Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated},
	},
})
var Docs = lint.Markdownify(map[string]*lint.RawDocumentation{
	"S1000": {
		Title: `Use plain channel send or receive instead of single-case select`,
		Text: `Select statements with a single case can be replaced with a simple
send or receive.`,
		Before: `
select {
case x := <-ch:
    fmt.Println(x)
}`,
		After: `
x := <-ch
fmt.Println(x)
`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1001": {
		Title: `Replace for loop with call to copy`,
		Text: `
Use \'copy()\' for copying elements from one slice to another. For
arrays of identical size, you can use simple assignment.`,
		Before: `
for i, x := range src {
    dst[i] = x
}`,
		After: `copy(dst, src)`,
		Since: "2017.1",

		MergeIf: lint.MergeIfAll,
	},

	"S1002": {
		Title:  `Omit comparison with boolean constant`,
		Before: `if x == true {}`,
		After:  `if x {}`,
		Since:  "2017.1",

		MergeIf: lint.MergeIfAll,
	},

	"S1003": {
		Title:   `Replace call to \'strings.Index\' with \'strings.Contains\'`,
		Before:  `if strings.Index(x, y) != -1 {}`,
		After:   `if strings.Contains(x, y) {}`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1004": {
		Title:   `Replace call to \'bytes.Compare\' with \'bytes.Equal\'`,
		Before:  `if bytes.Compare(x, y) == 0 {}`,
		After:   `if bytes.Equal(x, y) {}`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1005": {
		Title: `Drop unnecessary use of the blank identifier`,
		Text:  `In many cases, assigning to the blank identifier is unnecessary.`,
		Before: `
for _ = range s {}
x, _ = someMap[key]
_ = <-ch`,
		After: `
for range s{}
x = someMap[key]
<-ch`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1006": {
		Title:   `Use \"for { ... }\" for infinite loops`,
		Text:    `For infinite loops, using \'for { ... }\' is the most idiomatic choice.`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1007": {
		Title: `Simplify regular expression by using raw string literal`,
		Text:  "" /* 286 byte string literal not displayed */,

		Before:  `regexp.Compile("\\A(\\w+) profile: total \\d+\\n\\z")`,
		After:   "regexp.Compile(`\\A(\\w+) profile: total \\d+\\n\\z`)",
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1008": {
		Title: `Simplify returning boolean expression`,
		Before: `
if <expr> {
    return true
}
return false`,
		After:   `return <expr>`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1009": {
		Title: `Omit redundant nil check on slices`,
		Text:  "" /* 189 byte string literal not displayed */,

		Before:  `if x != nil && len(x) != 0 {}`,
		After:   `if len(x) != 0 {}`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1010": {
		Title: `Omit default slice index`,
		Text: `When slicing, the second index defaults to the length of the value,
making \'s[n:len(s)]\' and \'s[n:]\' equivalent.`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1011": {
		Title: `Use a single \'append\' to concatenate two slices`,
		Before: `
for _, e := range y {
    x = append(x, e)
}`,
		After: `x = append(x, y...)`,
		Since: "2017.1",

		MergeIf: lint.MergeIfAll,
	},

	"S1012": {
		Title: `Replace \'time.Now().Sub(x)\' with \'time.Since(x)\'`,
		Text: `The \'time.Since\' helper has the same effect as using \'time.Now().Sub(x)\'
but is easier to read.`,
		Before:  `time.Now().Sub(x)`,
		After:   `time.Since(x)`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1016": {
		Title: `Use a type conversion instead of manually copying struct fields`,
		Text:  "" /* 283 byte string literal not displayed */,

		Before: `
var x T1
y := T2{
    Field1: x.Field1,
    Field2: x.Field2,
}`,
		After: `
var x T1
y := T2(x)`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAll,
	},

	"S1017": {
		Title: `Replace manual trimming with \'strings.TrimPrefix\'`,
		Text:  "" /* 286 byte string literal not displayed */,

		Before: `
if strings.HasPrefix(str, prefix) {
    str = str[len(prefix):]
}`,
		After:   `str = strings.TrimPrefix(str, prefix)`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1018": {
		Title: `Use \"copy\" for sliding elements`,
		Text:  "" /* 148 byte string literal not displayed */,

		Before: `
for i := 0; i < n; i++ {
    bs[i] = bs[offset+i]
}`,
		After:   `copy(bs[:n], bs[offset:])`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1019": {
		Title: `Simplify \"make\" call by omitting redundant arguments`,
		Text:  "" /* 177 byte string literal not displayed */,

		Since: "2017.1",

		MergeIf: lint.MergeIfAll,
	},

	"S1020": {
		Title:   `Omit redundant nil check in type assertion`,
		Before:  `if _, ok := i.(T); ok && i != nil {}`,
		After:   `if _, ok := i.(T); ok {}`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1021": {
		Title: `Merge variable declaration and assignment`,
		Before: `
var x uint
x = 1`,
		After:   `var x uint = 1`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1023": {
		Title: `Omit redundant control flow`,
		Text:  "" /* 270 byte string literal not displayed */,

		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1024": {
		Title: `Replace \'x.Sub(time.Now())\' with \'time.Until(x)\'`,
		Text: `The \'time.Until\' helper has the same effect as using \'x.Sub(time.Now())\'
but is easier to read.`,
		Before:  `x.Sub(time.Now())`,
		After:   `time.Until(x)`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1025": {
		Title: `Don't use \'fmt.Sprintf("%s", x)\' unnecessarily`,
		Text:  "" /* 543 byte string literal not displayed */,

		Since:   "2017.1",
		MergeIf: lint.MergeIfAll,
	},

	"S1028": {
		Title:   `Simplify error construction with \'fmt.Errorf\'`,
		Before:  `errors.New(fmt.Sprintf(...))`,
		After:   `fmt.Errorf(...)`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1029": {
		Title: `Range over the string directly`,
		Text:  "" /* 343 byte string literal not displayed */,

		Before:  `for _, r := range []rune(s) {}`,
		After:   `for _, r := range s {}`,
		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1030": {
		Title: `Use \'bytes.Buffer.String\' or \'bytes.Buffer.Bytes\'`,
		Text:  "" /* 334 byte string literal not displayed */,

		Since:   "2017.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1031": {
		Title: `Omit redundant nil check around loop`,
		Text:  "" /* 143 byte string literal not displayed */,

		Before: `
if s != nil {
    for _, x := range s {
        ...
    }
}`,
		After: `
for _, x := range s {
    ...
}`,
		Since: "2017.1",

		MergeIf: lint.MergeIfAll,
	},

	"S1032": {
		Title: `Use \'sort.Ints(x)\', \'sort.Float64s(x)\', and \'sort.Strings(x)\'`,
		Text:  "" /* 202 byte string literal not displayed */,

		Before:  `sort.Sort(sort.StringSlice(x))`,
		After:   `sort.Strings(x)`,
		Since:   "2019.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1033": {
		Title:   `Unnecessary guard around call to \"delete\"`,
		Text:    `Calling \'delete\' on a nil map is a no-op.`,
		Since:   "2019.2",
		MergeIf: lint.MergeIfAny,
	},

	"S1034": {
		Title:   `Use result of type assertion to simplify cases`,
		Since:   "2019.2",
		MergeIf: lint.MergeIfAny,
	},

	"S1035": {
		Title: `Redundant call to \'net/http.CanonicalHeaderKey\' in method call on \'net/http.Header\'`,
		Text: `
The methods on \'net/http.Header\', namely \'Add\', \'Del\', \'Get\'
and \'Set\', already canonicalize the given header name.`,
		Since:   "2020.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1036": {
		Title: `Unnecessary guard around map access`,

		Text: "" /* 516 byte string literal not displayed */,

		Since:   "2020.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1037": {
		Title: `Elaborate way of sleeping`,
		Text:  "" /* 194 byte string literal not displayed */,

		Since:   "2020.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1038": {
		Title:   "Unnecessarily complex way of printing formatted string",
		Text:    `Instead of using \'fmt.Print(fmt.Sprintf(...))\', one can use \'fmt.Printf(...)\'.`,
		Since:   "2020.1",
		MergeIf: lint.MergeIfAny,
	},

	"S1039": {
		Title: `Unnecessary use of \'fmt.Sprint\'`,
		Text: `
Calling \'fmt.Sprint\' with a single string argument is unnecessary
and identical to using the string directly.`,
		Since: "2020.1",

		MergeIf: lint.MergeIfAll,
	},
	"S1040": {
		Title: "Type assertion to current type",
		Text:  "" /* 403 byte string literal not displayed */,

		Since: "2021.1",

		MergeIf: lint.MergeIfAll,
	},
})

Functions

func CheckAssertNotNil

func CheckAssertNotNil(pass *analysis.Pass) (interface{}, error)

func CheckBytesBufferConversions

func CheckBytesBufferConversions(pass *analysis.Pass) (interface{}, error)

func CheckBytesCompare

func CheckBytesCompare(pass *analysis.Pass) (interface{}, error)

func CheckDeclareAssign

func CheckDeclareAssign(pass *analysis.Pass) (interface{}, error)

func CheckElaborateSleep

func CheckElaborateSleep(pass *analysis.Pass) (interface{}, error)

func CheckErrorsNewSprintf

func CheckErrorsNewSprintf(pass *analysis.Pass) (interface{}, error)

func CheckForTrue

func CheckForTrue(pass *analysis.Pass) (interface{}, error)

func CheckGuardedDelete

func CheckGuardedDelete(pass *analysis.Pass) (interface{}, error)

func CheckIfBoolCmp

func CheckIfBoolCmp(pass *analysis.Pass) (interface{}, error)

func CheckIfReturn

func CheckIfReturn(pass *analysis.Pass) (interface{}, error)

func CheckLoopAppend

func CheckLoopAppend(pass *analysis.Pass) (interface{}, error)

func CheckLoopCopy

func CheckLoopCopy(pass *analysis.Pass) (interface{}, error)

func CheckLoopSlide

func CheckLoopSlide(pass *analysis.Pass) (interface{}, error)

func CheckMakeLenCap

func CheckMakeLenCap(pass *analysis.Pass) (interface{}, error)

func CheckNilCheckAroundRange

func CheckNilCheckAroundRange(pass *analysis.Pass) (interface{}, error)

func CheckPrintSprintf

func CheckPrintSprintf(pass *analysis.Pass) (interface{}, error)

func CheckRangeStringRunes

func CheckRangeStringRunes(pass *analysis.Pass) (interface{}, error)

func CheckRedundantBreak

func CheckRedundantBreak(pass *analysis.Pass) (interface{}, error)

func CheckRedundantCanonicalHeaderKey

func CheckRedundantCanonicalHeaderKey(pass *analysis.Pass) (interface{}, error)

func CheckRedundantNilCheckWithLen

func CheckRedundantNilCheckWithLen(pass *analysis.Pass) (interface{}, error)

CheckRedundantNilCheckWithLen checks for the following redundant nil-checks:

if x == nil || len(x) == 0 {}
if x != nil && len(x) != 0 {}
if x != nil && len(x) == N {} (where N != 0)
if x != nil && len(x) > N {}
if x != nil && len(x) >= N {} (where N != 0)

func CheckRedundantSprintf

func CheckRedundantSprintf(pass *analysis.Pass) (interface{}, error)

func CheckRegexpRaw

func CheckRegexpRaw(pass *analysis.Pass) (interface{}, error)

func CheckSameTypeTypeAssertion

func CheckSameTypeTypeAssertion(pass *analysis.Pass) (interface{}, error)

func CheckSimplerStructConversion

func CheckSimplerStructConversion(pass *analysis.Pass) (interface{}, error)

func CheckSimplifyTypeSwitch

func CheckSimplifyTypeSwitch(pass *analysis.Pass) (interface{}, error)

func CheckSingleCaseSelect

func CheckSingleCaseSelect(pass *analysis.Pass) (interface{}, error)

func CheckSlicing

func CheckSlicing(pass *analysis.Pass) (interface{}, error)

func CheckSortHelpers

func CheckSortHelpers(pass *analysis.Pass) (interface{}, error)

func CheckSprintLiteral

func CheckSprintLiteral(pass *analysis.Pass) (interface{}, error)

func CheckStringsContains

func CheckStringsContains(pass *analysis.Pass) (interface{}, error)

func CheckTimeSince

func CheckTimeSince(pass *analysis.Pass) (interface{}, error)

func CheckTimeUntil

func CheckTimeUntil(pass *analysis.Pass) (interface{}, error)

func CheckTrim

func CheckTrim(pass *analysis.Pass) (interface{}, error)

func CheckUnnecessaryBlank

func CheckUnnecessaryBlank(pass *analysis.Pass) (interface{}, error)

func CheckUnnecessaryGuard

func CheckUnnecessaryGuard(pass *analysis.Pass) (interface{}, error)

Source Files

analysis.go doc.go lint.go

Version
v0.4.0-0.dev
Published
Mar 30, 2022
Platform
js/wasm
Imports
23 packages
Last checked
2 minutes ago

Tools for package owners.