-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to cuddle assignments for a whole block
- Loading branch information
1 parent
ae57516
commit 8a4ee0e
Showing
4 changed files
with
309 additions
and
11 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
testdata/src/default_config/fix_cuddle_whole_blocks/fix_cuddle_whole_blocks.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package testpkg | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func ForCuddleAssignmentWholeBlock() { | ||
x := 1 | ||
y := 2 | ||
|
||
var numbers []int | ||
for i := 0; i < 10; i++ { | ||
if x == y { | ||
numbers = append(numbers, i) | ||
} | ||
} | ||
|
||
var numbers2 []int | ||
for i := range 10 { | ||
if x == y { | ||
numbers2 = append(numbers2, i) | ||
} | ||
} | ||
|
||
var numbers3 []int | ||
for { // want "for statement without condition should never be cuddled" | ||
if x == y { | ||
numbers3 = append(numbers3, i) | ||
} | ||
} | ||
|
||
environment = make(map[string]string) | ||
for _, env := range req.GetConfig().GetEnvs() { | ||
switch env.GetKey() { | ||
case "user-data": | ||
cloudInitUserData = env.GetValue() | ||
default: | ||
environment[env.GetKey()] = env.GetValue() | ||
} | ||
} | ||
} | ||
|
||
func IfCuddleAssignmentWholeBlock() { | ||
x := 1 | ||
y := 2 | ||
|
||
counter := 0 | ||
if somethingTrue { | ||
checker := getAChecker() | ||
if !checker { | ||
return | ||
} | ||
|
||
counter++ // Cuddled variable used in block, but not as first statement | ||
} | ||
|
||
var number2 []int | ||
if x == y { | ||
fmt.Println("a") | ||
} else { | ||
if x > y { | ||
number2 = append(number2, i) | ||
} | ||
} | ||
|
||
var number3 []int | ||
if x == y { | ||
fmt.Println("a") | ||
} else if x > y { | ||
if x == y { | ||
number3 = append(number3, i) | ||
} | ||
} | ||
|
||
var number4 []int | ||
if x == y { | ||
if x == y { | ||
number4 = append(number4, i) | ||
} | ||
} else if x > y { | ||
if x == y { | ||
number4 = append(number4, i) | ||
} | ||
} else { | ||
if x > y { | ||
number4 = append(number4, i) | ||
} | ||
} | ||
} | ||
|
||
func SwitchCuddleAssignmentWholeBlock() { | ||
var id string | ||
var b bool // want "declarations should never be cuddled" | ||
switch b { // want "only one cuddle assignment allowed before switch statement" | ||
case true: | ||
id = "a" | ||
case false: | ||
id = "b" | ||
} | ||
|
||
var id2 string | ||
switch i := objectID.(type) { | ||
case int: | ||
if true { | ||
id2 = strconv.Itoa(i) | ||
} | ||
case uint32: | ||
if true { | ||
id2 = strconv.Itoa(int(i)) | ||
} | ||
case string: | ||
if true { | ||
id2 = i | ||
} | ||
} | ||
|
||
var id3 string | ||
switch { // want "anonymous switch statements should never be cuddled" | ||
case int: | ||
if true { | ||
id3 = strconv.Itoa(i) | ||
} | ||
case uint32: | ||
if true { | ||
id3 = strconv.Itoa(int(i)) | ||
} | ||
case string: | ||
if true { | ||
id3 = i | ||
} | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
testdata/src/default_config/fix_cuddle_whole_blocks/fix_cuddle_whole_blocks.go.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package testpkg | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func ForCuddleAssignmentWholeBlock() { | ||
x := 1 | ||
y := 2 | ||
|
||
var numbers []int | ||
for i := 0; i < 10; i++ { | ||
if x == y { | ||
numbers = append(numbers, i) | ||
} | ||
} | ||
|
||
var numbers2 []int | ||
for i := range 10 { | ||
if x == y { | ||
numbers2 = append(numbers2, i) | ||
} | ||
} | ||
|
||
var numbers3 []int | ||
|
||
for { // want "for statement without condition should never be cuddled" | ||
if x == y { | ||
numbers3 = append(numbers3, i) | ||
} | ||
} | ||
|
||
environment = make(map[string]string) | ||
for _, env := range req.GetConfig().GetEnvs() { | ||
switch env.GetKey() { | ||
case "user-data": | ||
cloudInitUserData = env.GetValue() | ||
default: | ||
environment[env.GetKey()] = env.GetValue() | ||
} | ||
} | ||
} | ||
|
||
func IfCuddleAssignmentWholeBlock() { | ||
x := 1 | ||
y := 2 | ||
|
||
counter := 0 | ||
if somethingTrue { | ||
checker := getAChecker() | ||
if !checker { | ||
return | ||
} | ||
|
||
counter++ // Cuddled variable used in block, but not as first statement | ||
} | ||
|
||
var number2 []int | ||
if x == y { | ||
fmt.Println("a") | ||
} else { | ||
if x > y { | ||
number2 = append(number2, i) | ||
} | ||
} | ||
|
||
var number3 []int | ||
if x == y { | ||
fmt.Println("a") | ||
} else if x > y { | ||
if x == y { | ||
number3 = append(number3, i) | ||
} | ||
} | ||
|
||
var number4 []int | ||
if x == y { | ||
if x == y { | ||
number4 = append(number4, i) | ||
} | ||
} else if x > y { | ||
if x == y { | ||
number4 = append(number4, i) | ||
} | ||
} else { | ||
if x > y { | ||
number4 = append(number4, i) | ||
} | ||
} | ||
} | ||
|
||
func SwitchCuddleAssignmentWholeBlock() { | ||
var id string | ||
|
||
var b bool // want "declarations should never be cuddled" | ||
switch b { // want "only one cuddle assignment allowed before switch statement" | ||
case true: | ||
id = "a" | ||
case false: | ||
id = "b" | ||
} | ||
|
||
var id2 string | ||
switch i := objectID.(type) { | ||
case int: | ||
if true { | ||
id2 = strconv.Itoa(i) | ||
} | ||
case uint32: | ||
if true { | ||
id2 = strconv.Itoa(int(i)) | ||
} | ||
case string: | ||
if true { | ||
id2 = i | ||
} | ||
} | ||
|
||
var id3 string | ||
|
||
switch { // want "anonymous switch statements should never be cuddled" | ||
case int: | ||
if true { | ||
id3 = strconv.Itoa(i) | ||
} | ||
case uint32: | ||
if true { | ||
id3 = strconv.Itoa(int(i)) | ||
} | ||
case string: | ||
if true { | ||
id3 = i | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters