Skip to content

Commit

Permalink
Assign and call (not really...) cuddle
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Jan 6, 2025
1 parent 9003d77 commit f70204c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions testdata/src/default_config/assign/assign.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package testpkg

type T struct {
I int
}

func NewT() *T {
return &T{}
}

func (*T) Fn() int {
return 1
}

func strictAppend() {
s := []string{}
s = append(s, "a")
Expand All @@ -19,3 +31,11 @@ func incDec() {

_ = y
}

func assignAndCall() {
t1 := NewT()
t2 := NewT()

t1.Fn()
t2.I = t1.Fn()
}
20 changes: 20 additions & 0 deletions testdata/src/default_config/assign/assign.go.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package testpkg

type T struct {
I int
}

func NewT() *T {
return &T{}
}

func (*T) Fn() int {
return 1
}

func strictAppend() {
s := []string{}
s = append(s, "a")
Expand All @@ -20,3 +32,11 @@ func incDec() {

_ = y
}

func assignAndCall() {
t1 := NewT()
t2 := NewT()

t1.Fn()
t2.I = t1.Fn()
}
18 changes: 18 additions & 0 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,27 @@ func (w *WSL) CheckCuddlingWithoutIntersection(stmt ast.Node, cursor *Cursor) {
_, prevIsAssign := previousNode.(*ast.AssignStmt)
_, prevIsDecl := previousNode.(*ast.DeclStmt)
_, prevIsIncDec := previousNode.(*ast.IncDecStmt)
_, currIsAssign := stmt.(*ast.AssignStmt)

prevIsValidType := previousNode == nil || prevIsAssign || prevIsDecl || prevIsIncDec

// TODO: This is `allow-assign-and-call`, should we deprecate it?
// ref: https://github.com/bombsimon/wsl/blob/52299dcd5c1c2a8baf77b4be4508937486d43656/wsl.go#L559-L563
// 1. It's not actually checking call - just that we have intersections
// 2. It's a bit too niche I think, either we support assign and call (or
// whatever) or we don't.
// 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 {
return
}
}

if w.numberOfStatementsAbove(cursor) > 0 && !prevIsValidType {
w.addError(
stmt.Pos(),
Expand Down

0 comments on commit f70204c

Please sign in to comment.