Skip to content

Commit

Permalink
Defer
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Jan 1, 2025
1 parent b749b98 commit 5b7b0bd
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
60 changes: 60 additions & 0 deletions testdata/src/default_config/defer/defer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package testpkg

import (
"fmt"
"sync"
)

type T int

func Fn() T {
return T(0)
}

func (*T) Close() {}

func Fn() {}

func fn() {
a := Fn()
b := Fn()

defer a.Close()
defer b.Close()

a = Fn()
defer a.Close()

b = Fn()
defer b.Close()

a = Fn()
defer a.Close()
b = Fn() // want "missing whitespace decreases readability"
defer b.Close()

a = Fn()
b = Fn()
defer a.Close() // want "missing whitespace decreases readability"
defer b.Close()

m := sync.Mutex{}

m.Lock()
defer m.Unlock()

c := true
defer func(b bool) { // want "missing whitespace decreases readability"
fmt.Printf("%v", b)
}()

_ = c
}

func fn2() {
a := 1
b := Fn() // want "missing whitespace decreases readability"
defer b.Close()

_ = a
}
64 changes: 64 additions & 0 deletions testdata/src/default_config/defer/defer.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package testpkg

import (
"fmt"
"sync"
)

type T int

func Fn() T {
return T(0)
}

func (*T) Close() {}

func Fn() {}

func fn() {
a := Fn()
b := Fn()

defer a.Close()
defer b.Close()

a = Fn()
defer a.Close()

b = Fn()
defer b.Close()

a = Fn()
defer a.Close()

b = Fn() // want "missing whitespace decreases readability"
defer b.Close()

a = Fn()
b = Fn()

defer a.Close() // want "missing whitespace decreases readability"
defer b.Close()

m := sync.Mutex{}

m.Lock()
defer m.Unlock()

c := true

defer func(b bool) { // want "missing whitespace decreases readability"
fmt.Printf("%v", b)
}()

_ = c
}

func fn2() {
a := 1

b := Fn() // want "missing whitespace decreases readability"
defer b.Close()

_ = a
}
20 changes: 18 additions & 2 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ func (w *WSL) CheckCuddling(stmt ast.Node, cursor *Cursor, maxAllowedStatements

_, prevIsAssign := previousNode.(*ast.AssignStmt)
_, prevIsDecl := previousNode.(*ast.DeclStmt)
_, currIsDefer := stmt.(*ast.DeferStmt)
n := w.numberOfStatementsAbove(cursor)

if n > 0 {
if prevIsAssign || prevIsDecl {
if prevIsAssign || prevIsDecl || currIsDefer {
intersects := identIntersection(currentIdents, previousIdents)

// No idents above share name with one in the if statement.
Expand Down Expand Up @@ -266,6 +267,18 @@ func (w *WSL) CheckGo(stmt *ast.GoStmt, cursor *Cursor) {
w.CheckExpr(stmt.Call, cursor)
}

func (w *WSL) CheckDefer(stmt *ast.DeferStmt, cursor *Cursor) {
previousNode := cursor.PreviousNode()

// We can cuddle any amount `defer` statements so only check cuddling if the
// previous one isn't a `defer` call.
if _, ok := previousNode.(*ast.DeferStmt); !ok {
w.CheckCuddling(stmt, cursor, 1)
}

w.CheckExpr(stmt.Call, cursor)
}

func (w *WSL) CheckBranch(stmt *ast.BranchStmt, cursor *Cursor) {
if _, ok := w.Config.Checks[CheckBreak]; !ok && stmt.Tok == token.BREAK {
return
Expand Down Expand Up @@ -389,6 +402,7 @@ func (w *WSL) CheckStmt(stmt ast.Stmt, cursor *Cursor) {
case *ast.IncDecStmt:
// defer func() {}
case *ast.DeferStmt:
w.CheckDefer(s, cursor)
// go func() {}
case *ast.GoStmt:
w.CheckGo(s, cursor)
Expand Down Expand Up @@ -550,6 +564,8 @@ func allIdents(node ast.Node) []*ast.Ident {
}
case *ast.GoStmt:
idents = append(idents, allIdents(n.Call)...)
case *ast.DeferStmt:
idents = append(idents, allIdents(n.Call)...)
case *ast.ValueSpec:
for _, name := range n.Names {
idents = append(idents, allIdents(name)...)
Expand Down Expand Up @@ -611,7 +627,7 @@ func allIdents(node ast.Node) []*ast.Ident {
*ast.ArrayType:
default:
spew.Dump(node)
fmt.Printf("%T\n", node)
fmt.Printf("missing ident detection for %T\n", node)
}

return idents
Expand Down

0 comments on commit 5b7b0bd

Please sign in to comment.