Skip to content

Commit

Permalink
Improve defer
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Jan 21, 2025
1 parent e2ee994 commit de5e4d6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
8 changes: 2 additions & 6 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,12 @@ func (c *Cursor) PreviousNode() ast.Node {
}

func (c *Cursor) PeekNext() bool {
reset := c.Save()
defer reset()

defer c.Save()()
return c.Next()
}

func (c *Cursor) PeekPrevious() bool {
reset := c.Save()
defer reset()

defer c.Save()()
return c.Previous()
}

Expand Down
9 changes: 9 additions & 0 deletions testdata/src/default_config/defer/defer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testpkg

import (
"fmt"
"os"
"sync"
)

Expand Down Expand Up @@ -58,3 +59,11 @@ func fn2() {

_ = a
}

func fn3() {
f, err := os.Open("x")
if err != nil {
panic(err)
}
defer f.Close()
}
9 changes: 9 additions & 0 deletions testdata/src/default_config/defer/defer.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testpkg

import (
"fmt"
"os"
"sync"
)

Expand Down Expand Up @@ -62,3 +63,11 @@ func fn2() {

_ = a
}

func fn3() {
f, err := os.Open("x")
if err != nil {
panic(err)
}
defer f.Close()
}
46 changes: 34 additions & 12 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,7 @@ func (w *WSL) CheckCuddlingWithoutIntersection(stmt ast.Node, cursor *Cursor) {
// 3. With the new check config, one could just disable checks for assign
// and it would allow cuddling with anything.
if !prevIsValidType && currIsAssign {
currentIdents := allIdents(stmt)
previousIdents := allIdents(previousNode)
intersects := identIntersection(currentIdents, previousIdents)

if len(intersects) > 0 {
if hasIntersection(stmt, previousNode) {
return
}
}
Expand Down Expand Up @@ -390,8 +386,27 @@ func (w *WSL) CheckDefer(stmt *ast.DeferStmt, cursor *Cursor) {
stmt.Call,
cursor,
func(n ast.Node) (int, bool) {
_, ok := n.(*ast.DeferStmt)
return 1, !ok
_, previousIsDefer := n.(*ast.DeferStmt)
_, previousIsIf := n.(*ast.IfStmt)

// We allow defer as a third node only if we if check between. E.g.
// f, err := os.Open(file)
// if err != nil {
// return err
// }
// defer f.Close()
if previousIsIf && w.numberOfStatementsAbove(cursor) >= 2 {
defer cursor.Save()()

cursor.Previous()
cursor.Previous()

if hasIntersection(cursor.Stmt(), stmt) {
return 1, false
}
}

return 1, !previousIsDefer
},
CheckDefer,
)
Expand Down Expand Up @@ -588,11 +603,7 @@ func (w *WSL) strictAppendCheck(stmt *ast.AssignStmt, cursor *Cursor) {
return
}

appendIdents := allIdents(appendNode)
previousIdents := allIdents(previousNode)
intersects := identIntersection(appendIdents, previousIdents)

if len(intersects) == 0 {
if !hasIntersection(appendNode, previousNode) {
w.addError(stmt.Pos(), stmt.Pos(), stmt.Pos(), MessageAddWhitespace)
}
}
Expand Down Expand Up @@ -1029,6 +1040,17 @@ func allIdents(node ast.Node) []*ast.Ident {
return idents
}

func hasIntersection(a, b ast.Node) bool {
return len(nodeIdentIntersection(a, b)) > 0
}

func nodeIdentIntersection(a, b ast.Node) []*ast.Ident {
aI := allIdents(a)
bI := allIdents(b)

return identIntersection(aI, bI)
}

func identIntersection(a, b []*ast.Ident) []*ast.Ident {
intersects := []*ast.Ident{}

Expand Down

0 comments on commit de5e4d6

Please sign in to comment.