aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/alexkohler
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2024-11-11 11:41:38 +0100
committerTaras Madan <tarasmadan@google.com>2024-11-11 11:10:48 +0000
commit27e76fae2ee2d84dc7db63af1d9ed7358ba35b7a (patch)
treeed19c0e35e272b3c4cc5a2f2c595e035b2428337 /vendor/github.com/alexkohler
parent621e84e063b0e15b23e17780338627c509e1b9e8 (diff)
vendor: update
Diffstat (limited to 'vendor/github.com/alexkohler')
-rw-r--r--vendor/github.com/alexkohler/nakedret/v2/nakedret.go27
1 files changed, 17 insertions, 10 deletions
diff --git a/vendor/github.com/alexkohler/nakedret/v2/nakedret.go b/vendor/github.com/alexkohler/nakedret/v2/nakedret.go
index f78bb8cb6..a55735928 100644
--- a/vendor/github.com/alexkohler/nakedret/v2/nakedret.go
+++ b/vendor/github.com/alexkohler/nakedret/v2/nakedret.go
@@ -22,10 +22,11 @@ import (
const pwd = "./"
-func NakedReturnAnalyzer(defaultLines uint) *analysis.Analyzer {
+func NakedReturnAnalyzer(defaultLines uint, skipTestFiles bool) *analysis.Analyzer {
nakedRet := &NakedReturnRunner{}
flags := flag.NewFlagSet("nakedret", flag.ExitOnError)
flags.UintVar(&nakedRet.MaxLength, "l", defaultLines, "maximum number of lines for a naked return function")
+ flags.BoolVar(&nakedRet.SkipTestFiles, "skip-test-files", skipTestFiles, "set to true to skip test files")
var analyzer = &analysis.Analyzer{
Name: "nakedret",
Doc: "Checks that functions with naked returns are not longer than a maximum size (can be zero).",
@@ -37,7 +38,8 @@ func NakedReturnAnalyzer(defaultLines uint) *analysis.Analyzer {
}
type NakedReturnRunner struct {
- MaxLength uint
+ MaxLength uint
+ SkipTestFiles bool
}
func (n *NakedReturnRunner) run(pass *analysis.Pass) (any, error) {
@@ -49,18 +51,20 @@ func (n *NakedReturnRunner) run(pass *analysis.Pass) (any, error) {
(*ast.ReturnStmt)(nil),
}
retVis := &returnsVisitor{
- pass: pass,
- f: pass.Fset,
- maxLength: n.MaxLength,
+ pass: pass,
+ f: pass.Fset,
+ maxLength: n.MaxLength,
+ skipTestFiles: n.SkipTestFiles,
}
inspector.Nodes(nodeFilter, retVis.NodesVisit)
return nil, nil
}
type returnsVisitor struct {
- pass *analysis.Pass
- f *token.FileSet
- maxLength uint
+ pass *analysis.Pass
+ f *token.FileSet
+ maxLength uint
+ skipTestFiles bool
// functions contains funcInfo for each nested function definition encountered while visiting the AST.
functions []funcInfo
@@ -74,7 +78,7 @@ type funcInfo struct {
reportNaked bool
}
-func checkNakedReturns(args []string, maxLength *uint, setExitStatus bool) error {
+func checkNakedReturns(args []string, maxLength *uint, skipTestFiles bool, setExitStatus bool) error {
fset := token.NewFileSet()
@@ -87,7 +91,7 @@ func checkNakedReturns(args []string, maxLength *uint, setExitStatus bool) error
return errors.New("max length nil")
}
- analyzer := NakedReturnAnalyzer(*maxLength)
+ analyzer := NakedReturnAnalyzer(*maxLength, skipTestFiles)
pass := &analysis.Pass{
Analyzer: analyzer,
Fset: fset,
@@ -292,6 +296,9 @@ func (v *returnsVisitor) NodesVisit(node ast.Node, push bool) bool {
if push && funcType != nil {
// Push function info to track returns for this function
file := v.f.File(node.Pos())
+ if v.skipTestFiles && strings.HasSuffix(file.Name(), "_test.go") {
+ return false
+ }
length := file.Position(node.End()).Line - file.Position(node.Pos()).Line
if length == 0 {
// consider functions that finish on the same line as they start as single line functions, not zero lines!