github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/test/testdata/wsl.go (about)

     1  //golangcitest:args -Ewsl
     2  //golangcitest:config_path testdata/configs/wsl.yml
     3  package testdata
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  )
     9  
    10  func main() {
    11  	var (
    12  		y = 0
    13  	)
    14  	if y < 1 { // want "if statements should only be cuddled with assignments"
    15  		fmt.Println("tight")
    16  	}
    17  
    18  	thisIsNotUsedInIf := true
    19  	if 2 > 1 { // want "if statements should only be cuddled with assignments used in the if statement itself"
    20  		return
    21  	}
    22  
    23  	one := 1
    24  	two := 2
    25  	three := 3
    26  	if three == 3 { // want "only one cuddle assignment allowed before if statement"
    27  		fmt.Println("too many cuddled assignments", one, two, thisIsNotUsedInIf)
    28  	}
    29  
    30  	var a = "a"
    31  	var b = "b" // want "declarations should never be cuddled"
    32  
    33  	if true {
    34  		return
    35  	}
    36  	if false { // want "if statements should only be cuddled with assignments"
    37  		return
    38  	}
    39  
    40  	for i := range make([]int, 10) {
    41  		fmt.Println(i)
    42  		fmt.Println(i + i)
    43  		continue // want "branch statements should not be cuddled if block has more than two lines"
    44  	}
    45  
    46  	assignOne := a
    47  	fmt.Println(assignOne)
    48  	assignTwo := b // want "assignments should only be cuddled with other assignments"
    49  	fmt.Println(assignTwo)
    50  
    51  	_, cf1 := context.WithCancel(context.Background())
    52  	_, cf2 := context.WithCancel(context.Background())
    53  	defer cf1() // want "only one cuddle assignment allowed before defer statement"
    54  	defer cf2()
    55  
    56  	err := multiline(
    57  		"spanning",
    58  		"multiple",
    59  	)
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  
    64  	notErr := multiline(
    65  		"spanning",
    66  		"multiple",
    67  	)
    68  	if err != nil { // want "if statements should only be cuddled with assignments used in the if statement itself"
    69  		panic("not from the line above")
    70  	}
    71  
    72  	// This is OK since we use a variable from the line above, even if we don't
    73  	// check it with the if.
    74  	xx := notErr
    75  	if err != nil {
    76  		panic(xx)
    77  	}
    78  }
    79  
    80  func multiline(s ...string) error {
    81  	return nil
    82  }
    83  
    84  func f1() int {
    85  	x := 1
    86  	return x
    87  }
    88  
    89  func f2() int {
    90  	x := 1
    91  	y := 3
    92  	return x + y // want "return statements should not be cuddled if block has more than two lines"
    93  }
    94  
    95  func f3() int {
    96  	sum := 0
    97  	for _, v := range []int{2, 4, 8} {
    98  		sum += v
    99  	}
   100  
   101  	notSum := 0
   102  	for _, v := range []int{1, 2, 4} { // want "ranges should only be cuddled with assignments used in the iteration"
   103  		sum += v
   104  	}
   105  
   106  	return sum + notSum
   107  }
   108  
   109  func onelineShouldNotError() error { return nil }
   110  
   111  func multilineCase() {
   112  	// Multiline cases
   113  	switch {
   114  	case true,
   115  		false:
   116  		fmt.Println("ok")
   117  	case false ||
   118  		true:
   119  		fmt.Println("ok")
   120  	case true,
   121  		false:
   122  		fmt.Println("ok")
   123  	}
   124  }
   125  
   126  func sliceExpr() {
   127  	// Index- and slice expressions.
   128  	var aSlice = []int{1, 2, 3}
   129  
   130  	start := 2
   131  	if v := aSlice[start]; v == 1 {
   132  		fmt.Println("ok")
   133  	}
   134  
   135  	notOk := 1
   136  	if v := aSlice[start]; v == 1 { // want "if statements should only be cuddled with assignments used in the if statement itself"
   137  		fmt.Println("notOk")
   138  		fmt.Println(notOk)
   139  	}
   140  
   141  	end := 2
   142  	if len(aSlice[start:end]) > 2 {
   143  		fmt.Println("ok")
   144  	}
   145  }
   146  
   147  func indexExpr() {
   148  	var aMap = map[string]struct{}{"key": {}}
   149  
   150  	key := "key"
   151  	if k, ok := aMap[key]; ok {
   152  		fmt.Println(k)
   153  	}
   154  
   155  	xxx := "xxx"
   156  	if _, ok := aMap[key]; ok { // want "if statements should only be cuddled with assignments used in the if statement itself"
   157  		fmt.Println("not ok")
   158  		fmt.Println(xxx)
   159  	}
   160  }
   161  
   162  func allowTrailing(i int) {
   163  	switch i {
   164  	case 1:
   165  		fmt.Println("one")
   166  
   167  	case 2:
   168  		fmt.Println("two")
   169  		// Comments OK too!
   170  	case 3:
   171  		fmt.Println("three")
   172  	}
   173  }
   174  
   175  // ExampleSomeOutput simulates an example function.
   176  func ExampleSomeOutput() {
   177  	fmt.Println("Hello, world")
   178  
   179  	// Output:
   180  	// Hello, world
   181  }
   182  
   183  func IncDecStmt() {
   184  	counter := 0
   185  	for range make([]int, 5) {
   186  		counter++
   187  	}
   188  
   189  	type t struct {
   190  		counter int
   191  	}
   192  
   193  	x := t{5}
   194  
   195  	x.counter--
   196  	if x.counter > 0 {
   197  		fmt.Println("not yet 0")
   198  	}
   199  }
   200  
   201  func AnonymousBlock() {
   202  	func(a, b int) { // want "block should not start with a whitespace"
   203  
   204  		fmt.Println(a + b)
   205  	}(1, 1)
   206  }
   207  
   208  func MultilineComment() {
   209  	if true {
   210  		/*
   211  			Ok to start block with
   212  			a
   213  			long
   214  			multiline
   215  			cmoment
   216  		*/
   217  		fmt.Println("true")
   218  	}
   219  }