-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
makezero ensures that objects recognized as slices are initialized with length 0. By default, this is only required when we find a subsequent append to the object, but can also be enabled at all times as a way of discouraging the use of integer variables (i.e. "i") to index slices.
- Loading branch information
1 parent
f7a0c3c
commit cf32a7b
Showing
8 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package golinters | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ashanbrown/makezero/makezero" | ||
"github.com/pkg/errors" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const makezeroName = "makezero" | ||
|
||
func NewMakezero() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: makezeroName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
return goanalysis.NewLinter( | ||
makezeroName, | ||
"Finds slice declarations with non-zero initial length", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
s := &lintCtx.Settings().Makezero | ||
|
||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
var res []goanalysis.Issue | ||
linter := makezero.NewLinter(s.Always) | ||
for _, file := range pass.Files { | ||
hints, err := linter.Run(pass.Fset, pass.TypesInfo, file) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "makezero linter failed on file %q", file.Name.String()) | ||
} | ||
for _, hint := range hints { | ||
res = append(res, goanalysis.NewIssue(&result.Issue{ | ||
Pos: hint.Position(), | ||
Text: hint.Details(), | ||
FromLinter: makezeroName, | ||
}, pass)) | ||
} | ||
} | ||
if len(res) == 0 { | ||
return nil, nil | ||
} | ||
mu.Lock() | ||
resIssues = append(resIssues, res...) | ||
mu.Unlock() | ||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax | goanalysis.LoadModeTypesInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//args: -Emakezero | ||
package testdata | ||
|
||
func Makezero() []int { | ||
x := make([]int, 5) | ||
return append(x, 1) // ERROR "append to slice `x` with non-zero initialized length" | ||
} | ||
|
||
func MakezeroNolint() []int { | ||
x := make([]int, 5) | ||
return append(x, 1) //nolint:makezero // ok that we're appending to an uninitialized slice | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//args: -Emakezero | ||
//config: linters-settings.makezero.always=true | ||
package testdata | ||
|
||
func MakezeroAlways() []int { | ||
x := make([]int, 5) // ERROR "slice `x` does not have non-zero initial length" | ||
return x | ||
} | ||
|
||
func MakezeroAlwaysNolint() []int { | ||
x := make([]int, 5) //nolint:makezero // ok that this is not initialized | ||
return x | ||
} |