Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat IncDecStmt as AssignStmt #123

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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