Skip to content

Commit

Permalink
Treat IncDecStmt as AssignStmt
Browse files Browse the repository at this point in the history
`++` and `--` operator are very similar to `x = x + 1` so we make it
simple and just treat it as an assignment.

Closes #120
  • Loading branch information
bombsimon committed Mar 15, 2023
1 parent 9f5e329 commit ddb4332
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
16 changes: 16 additions & 0 deletions testdata/src/default_config/generic_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,19 @@ func OnlyCheckTwoLinesAboveIfAssignment() {
return result, err
}
}

func IncrementDecrement() {
a := 1
a++
a--
b := 2

if true {
b--
}
b++ // want "assignments should only be cuddled with other assignments"

go func() {}()
b++ // want "assignments should only be cuddled with other assignments"
go func() {}() // want "only one cuddle assignment allowed before go statement"
}
19 changes: 19 additions & 0 deletions testdata/src/default_config/generic_handling.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,22 @@ func OnlyCheckTwoLinesAboveIfAssignment() {
return result, err
}
}

func IncrementDecrement() {
a := 1
a++
a--
b := 2

if true {
b--
}

b++ // want "assignments should only be cuddled with other assignments"

go func() {}()

b++ // want "assignments should only be cuddled with other assignments"

go func() {}() // want "only one cuddle assignment allowed before go statement"
}
11 changes: 10 additions & 1 deletion wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ func (p *processor) parseBlockStatements(statements []ast.Stmt) {
continue
}

if _, ok := previousStatement.(*ast.AssignStmt); ok {
switch previousStatement.(type) {
case *ast.AssignStmt, *ast.IncDecStmt:
continue
}

Expand All @@ -533,6 +534,14 @@ func (p *processor) parseBlockStatements(statements []ast.Stmt) {
}

p.addWhitespaceBeforeError(t, reasonAssignsCuddleAssign)
case *ast.IncDecStmt:
switch previousStatement.(type) {
case *ast.AssignStmt, *ast.IncDecStmt:
continue
}

p.addWhitespaceBeforeError(t, reasonAssignsCuddleAssign)

case *ast.DeclStmt:
if !p.config.AllowCuddleDeclaration {
p.addWhitespaceBeforeError(t, reasonNeverCuddleDeclare)
Expand Down

0 comments on commit ddb4332

Please sign in to comment.