Skip to content

Commit

Permalink
cleanup functions declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
mfederowicz committed Dec 25, 2023
1 parent 24df8df commit 3db9ce1
Show file tree
Hide file tree
Showing 34 changed files with 104 additions and 104 deletions.
4 changes: 2 additions & 2 deletions rule/add-constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin
}

// Name returns the rule name.
func (r *AddConstantRule) Name() string {
func (*AddConstantRule) Name() string {
return "add-constant"
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func (r lintAddConstantRule) checkFunc(expr *ast.CallExpr) {
}
}

func (r lintAddConstantRule) getFuncName(expr *ast.CallExpr) string {
func (lintAddConstantRule) getFuncName(expr *ast.CallExpr) string {
switch f := expr.Fun.(type) {
case *ast.SelectorExpr:
switch prefix := f.X.(type) {
Expand Down
2 changes: 1 addition & 1 deletion rule/argument-limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []
}

// Name returns the rule name.
func (r *ArgumentsLimitRule) Name() string {
func (*ArgumentsLimitRule) Name() string {
return "argument-limit"
}

Expand Down
16 changes: 8 additions & 8 deletions rule/atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type AtomicRule struct{}

// Apply applies the rule to given file.
func (r *AtomicRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (*AtomicRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
walker := atomic{
pkgTypesInfo: file.Pkg.TypesInfo(),
Expand All @@ -27,7 +27,7 @@ func (r *AtomicRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
}

// Name returns the rule name.
func (r *AtomicRule) Name() string {
func (*AtomicRule) Name() string {
return "atomic"
}

Expand All @@ -36,10 +36,10 @@ type atomic struct {
onFailure func(lint.Failure)
}

func (r atomic) Visit(node ast.Node) ast.Visitor {
func (w atomic) Visit(node ast.Node) ast.Visitor {
n, ok := node.(*ast.AssignStmt)
if !ok {
return r
return w
}

if len(n.Lhs) != len(n.Rhs) {
Expand All @@ -59,8 +59,8 @@ func (r atomic) Visit(node ast.Node) ast.Visitor {
continue
}
pkgIdent, _ := sel.X.(*ast.Ident)
if r.pkgTypesInfo != nil {
pkgName, ok := r.pkgTypesInfo.Uses[pkgIdent].(*types.PkgName)
if w.pkgTypesInfo != nil {
pkgName, ok := w.pkgTypesInfo.Uses[pkgIdent].(*types.PkgName)
if !ok || pkgName.Imported().Path() != "sync/atomic" {
continue
}
Expand All @@ -82,13 +82,13 @@ func (r atomic) Visit(node ast.Node) ast.Visitor {
}

if broken {
r.onFailure(lint.Failure{
w.onFailure(lint.Failure{
Confidence: 1,
Failure: "direct assignment to atomic value",
Node: n,
})
}
}
}
return r
return w
}
2 changes: 1 addition & 1 deletion rule/banned-characters.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lin
}

// Name returns the rule name
func (r *BannedCharsRule) Name() string {
func (*BannedCharsRule) Name() string {
return bannedCharsRuleName
}

Expand Down
4 changes: 2 additions & 2 deletions rule/bare-return.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type BareReturnRule struct{}

// Apply applies the rule to given file.
func (r *BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

onFailure := func(failure lint.Failure) {
Expand All @@ -23,7 +23,7 @@ func (r *BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure
}

// Name returns the rule name.
func (r *BareReturnRule) Name() string {
func (*BareReturnRule) Name() string {
return "bare-return"
}

Expand Down
4 changes: 2 additions & 2 deletions rule/blank-imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type BlankImportsRule struct{}

// Name returns the rule name.
func (r *BlankImportsRule) Name() string {
func (*BlankImportsRule) Name() string {
return "blank-imports"
}

Expand Down Expand Up @@ -62,7 +62,7 @@ func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failu
return failures
}

func (r *BlankImportsRule) fileHasValidEmbedComment(fileAst *ast.File) bool {
func (*BlankImportsRule) fileHasValidEmbedComment(fileAst *ast.File) bool {
for _, commentGroup := range fileAst.Comments {
for _, comment := range commentGroup.List {
if strings.HasPrefix(comment.Text, "//go:embed ") {
Expand Down
4 changes: 2 additions & 2 deletions rule/bool-literal-in-expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type BoolLiteralRule struct{}

// Apply applies the rule to given file.
func (r *BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

onFailure := func(failure lint.Failure) {
Expand All @@ -26,7 +26,7 @@ func (r *BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failur
}

// Name returns the rule name.
func (r *BoolLiteralRule) Name() string {
func (*BoolLiteralRule) Name() string {
return "bool-literal-in-expr"
}

Expand Down
4 changes: 2 additions & 2 deletions rule/call-to-gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type CallToGCRule struct{}

// Apply applies the rule to given file.
func (r *CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
Expand All @@ -27,7 +27,7 @@ func (r *CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
}

// Name returns the rule name.
func (r *CallToGCRule) Name() string {
func (*CallToGCRule) Name() string {
return "call-to-gc"
}

Expand Down
76 changes: 38 additions & 38 deletions rule/cognitive-complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Argument
}

// Name returns the rule name.
func (r *CognitiveComplexityRule) Name() string {
func (*CognitiveComplexityRule) Name() string {
return "cognitive-complexity"
}

Expand All @@ -66,17 +66,17 @@ type cognitiveComplexityLinter struct {
onFailure func(lint.Failure)
}

func (r cognitiveComplexityLinter) lintCognitiveComplexity() {
f := r.file
func (w cognitiveComplexityLinter) lintCognitiveComplexity() {
f := w.file
for _, decl := range f.AST.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok && fn.Body != nil {
v := cognitiveComplexityVisitor{}
c := v.subTreeComplexity(fn.Body)
if c > r.maxComplexity {
r.onFailure(lint.Failure{
if c > w.maxComplexity {
w.onFailure(lint.Failure{
Confidence: 1,
Category: "maintenance",
Failure: fmt.Sprintf("function %s has cognitive complexity %d (> max enabled %d)", funcName(fn), c, r.maxComplexity),
Failure: fmt.Sprintf("function %s has cognitive complexity %d (> max enabled %d)", funcName(fn), c, w.maxComplexity),
Node: fn,
})
}
Expand All @@ -90,67 +90,67 @@ type cognitiveComplexityVisitor struct {
}

// subTreeComplexity calculates the cognitive complexity of an AST-subtree.
func (r cognitiveComplexityVisitor) subTreeComplexity(n ast.Node) int {
ast.Walk(&r, n)
return r.complexity
func (v cognitiveComplexityVisitor) subTreeComplexity(n ast.Node) int {
ast.Walk(&v, n)
return v.complexity
}

// Visit implements the ast.Visitor interface.
func (r *cognitiveComplexityVisitor) Visit(n ast.Node) ast.Visitor {
func (v *cognitiveComplexityVisitor) Visit(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.IfStmt:
targets := []ast.Node{n.Cond, n.Body, n.Else}
r.walk(1, targets...)
v.walk(1, targets...)
return nil
case *ast.ForStmt:
targets := []ast.Node{n.Cond, n.Body}
r.walk(1, targets...)
v.walk(1, targets...)
return nil
case *ast.RangeStmt:
r.walk(1, n.Body)
v.walk(1, n.Body)
return nil
case *ast.SelectStmt:
r.walk(1, n.Body)
v.walk(1, n.Body)
return nil
case *ast.SwitchStmt:
r.walk(1, n.Body)
v.walk(1, n.Body)
return nil
case *ast.TypeSwitchStmt:
r.walk(1, n.Body)
v.walk(1, n.Body)
return nil
case *ast.FuncLit:
r.walk(0, n.Body) // do not increment the complexity, just do the nesting
v.walk(0, n.Body) // do not increment the complexity, just do the nesting
return nil
case *ast.BinaryExpr:
r.complexity += r.binExpComplexity(n)
v.complexity += v.binExpComplexity(n)
return nil // skip visiting binexp sub-tree (already visited by binExpComplexity)
case *ast.BranchStmt:
if n.Label != nil {
r.complexity++
v.complexity++
}
}
// TODO handle (at least) direct recursion

return r
return v
}

func (r *cognitiveComplexityVisitor) walk(complexityIncrement int, targets ...ast.Node) {
r.complexity += complexityIncrement + r.nestingLevel
nesting := r.nestingLevel
r.nestingLevel++
func (v *cognitiveComplexityVisitor) walk(complexityIncrement int, targets ...ast.Node) {
v.complexity += complexityIncrement + v.nestingLevel
nesting := v.nestingLevel
v.nestingLevel++

for _, t := range targets {
if t == nil {
continue
}

ast.Walk(r, t)
ast.Walk(v, t)
}

r.nestingLevel = nesting
v.nestingLevel = nesting
}

func (r cognitiveComplexityVisitor) binExpComplexity(n *ast.BinaryExpr) int {
func (cognitiveComplexityVisitor) binExpComplexity(n *ast.BinaryExpr) int {
calculator := binExprComplexityCalculator{opsStack: []token.Token{}}

astutil.Apply(n, calculator.pre, calculator.post)
Expand All @@ -164,48 +164,48 @@ type binExprComplexityCalculator struct {
subexpStarted bool
}

func (r *binExprComplexityCalculator) pre(c *astutil.Cursor) bool {
func (becc *binExprComplexityCalculator) pre(c *astutil.Cursor) bool {
switch n := c.Node().(type) {
case *ast.BinaryExpr:
isBoolOp := n.Op == token.LAND || n.Op == token.LOR
if !isBoolOp {
break
}

ops := len(r.opsStack)
ops := len(becc.opsStack)
// if
// is the first boolop in the expression OR
// is the first boolop inside a subexpression (...) OR
// is not the same to the previous one
// then
// increment complexity
if ops == 0 || r.subexpStarted || n.Op != r.opsStack[ops-1] {
r.complexity++
r.subexpStarted = false
if ops == 0 || becc.subexpStarted || n.Op != becc.opsStack[ops-1] {
becc.complexity++
becc.subexpStarted = false
}

r.opsStack = append(r.opsStack, n.Op)
becc.opsStack = append(becc.opsStack, n.Op)
case *ast.ParenExpr:
r.subexpStarted = true
becc.subexpStarted = true
}

return true
}

func (r *binExprComplexityCalculator) post(c *astutil.Cursor) bool {
func (becc *binExprComplexityCalculator) post(c *astutil.Cursor) bool {
switch n := c.Node().(type) {
case *ast.BinaryExpr:
isBoolOp := n.Op == token.LAND || n.Op == token.LOR
if !isBoolOp {
break
}

ops := len(r.opsStack)
ops := len(becc.opsStack)
if ops > 0 {
r.opsStack = r.opsStack[:ops-1]
becc.opsStack = becc.opsStack[:ops-1]
}
case *ast.ParenExpr:
r.subexpStarted = false
becc.subexpStarted = false
}

return true
Expand Down
2 changes: 1 addition & 1 deletion rule/comment-spacings.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) []lint
}

// Name yields this rule name.
func (r *CommentSpacingsRule) Name() string {
func (*CommentSpacingsRule) Name() string {
return "comment-spacings"
}

Expand Down
4 changes: 2 additions & 2 deletions rule/confusing-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var allPkgs = packages{pkgs: make([]pkgMethods, 1)}
type ConfusingNamingRule struct{}

// Apply applies the rule to given file.
func (r *ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (*ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
fileAst := file.AST
pkgm := allPkgs.methodNames(file.Pkg)
Expand All @@ -65,7 +65,7 @@ func (r *ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa
}

// Name returns the rule name.
func (r *ConfusingNamingRule) Name() string {
func (*ConfusingNamingRule) Name() string {
return "confusing-naming"
}

Expand Down
4 changes: 2 additions & 2 deletions rule/confusing-results.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type ConfusingResultsRule struct{}

// Apply applies the rule to given file.
func (r *ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

fileAst := file.AST
Expand All @@ -26,7 +26,7 @@ func (r *ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.F
}

// Name returns the rule name.
func (r *ConfusingResultsRule) Name() string {
func (*ConfusingResultsRule) Name() string {
return "confusing-results"
}

Expand Down
Loading

0 comments on commit 3db9ce1

Please sign in to comment.