github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/define_emptyslice_test.go (about)

     1  // Copyright 2019 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package check
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestEmptySlice(t *testing.T) {
    12  	const code = `package main
    13  func main(){
    14  	a := []string{} // error (zero-length but non-nil slice)
    15  	var b []string // valid case (zero-length and nil slice)
    16  	c := []int{} // error (zero-length but non-nil slice)
    17  	d := [2]float{} // valid (not empty)
    18  	e := []bool{true, true, false,} // valid (not empty)
    19  	for _, f := range e {
    20  		if !f {
    21  			g := []newtype.new{} // error (zero-length but non-nil slice)
    22  			h := []*star{} // error (zero-length but non-nil slice)
    23  		}
    24  	}
    25  	i = []string{} // valid (not define-declaration)
    26  	same := [][2]string{} // error (zero-length but non-nil slice)
    27  	lst := []struct { // error (zero-length but non-nil slice)
    28  		ID string
    29  	}{}
    30  	j = []struct { // valid (not define-declaration)
    31  		ID string
    32  	}{}
    33  	k, l := 100, []float{} // error: "l" should be warned
    34  	m, n = "str", []string{} // valid (not define-declaration)
    35  	x, y := someFunction() // valid (not defines empty slice)
    36  	a, b, c := []int{}, []int{}, "string" // error: a and b is invalid
    37  	one, two, three, four, five := []int{}, []string{}, []float{}, 100, []byte{}
    38  	if a := []int{}; len(a) == 1 { // this should be ignored
    39  		print(a)
    40  	}
    41  }`
    42  	const path = "/src/go.chromium.org/tast-tests/cros/local/foo.go"
    43  	f, fs := parse(code, path)
    44  	issues := EmptySlice(fs, f, false)
    45  	expects := []string{
    46  		path + ":3:2: Use 'var' statement when you declare empty slice(s): a",
    47  		path + ":5:2: Use 'var' statement when you declare empty slice(s): c",
    48  		path + ":10:4: Use 'var' statement when you declare empty slice(s): g",
    49  		path + ":11:4: Use 'var' statement when you declare empty slice(s): h",
    50  		path + ":15:2: Use 'var' statement when you declare empty slice(s): same",
    51  		path + ":16:2: Use 'var' statement when you declare empty slice(s): lst",
    52  		path + ":22:2: Use 'var' statement when you declare empty slice(s): l",
    53  		path + ":25:2: Use 'var' statement when you declare empty slice(s): a, b",
    54  		path + ":26:2: Use 'var' statement when you declare empty slice(s): one, two, three, five",
    55  	}
    56  	verifyIssues(t, issues, expects)
    57  }
    58  
    59  func TestAutoFixEmptySlice(t *testing.T) {
    60  	const filename = "foo.go"
    61  	files := make(map[string]string)
    62  	files[filename] = `package newpackage
    63  
    64  // main do nothing
    65  func main() {
    66  	a := []string{} // error a
    67  	var suji int
    68  	b := []int{} // error b
    69  	x + y
    70  	for _, f := range e {
    71  		if !f {
    72  			g := []newtype.new{} // error g
    73  			var h []*star        // error h
    74  		}
    75  	}
    76  	var same [][2]string // no error same
    77  	lst := []struct {
    78  		ID string
    79  	}{}
    80  	k, l := 0, []string{}                 // comment kl
    81  	a, b, c := []int{}, []int{}, "string" // comment a b c
    82  	var n int                             // comment n
    83  	samesame := [4][]string{}             // comment samesame
    84  	// Comment something.
    85  	someFunction()
    86  	one, two, three, four, five := []int{}, []float{}, []float{}, 100, []byte{} // comment 5
    87  }
    88  `
    89  	expects := make(map[string]string)
    90  	expects[filename] = `package newpackage
    91  
    92  // main do nothing
    93  func main() {
    94  	var a []string // error a
    95  	var suji int
    96  	var b []int // error b
    97  	x + y
    98  	for _, f := range e {
    99  		if !f {
   100  			var g []newtype.new // error g
   101  			var h []*star       // error h
   102  		}
   103  	}
   104  	var same [][2]string // no error same
   105  	var lst []struct {
   106  		ID string
   107  	}
   108  	k, l := 0, []string{}                 // comment kl
   109  	a, b, c := []int{}, []int{}, "string" // comment a b c
   110  	var n int                             // comment n
   111  	samesame := [4][]string{}             // comment samesame
   112  	// Comment something.
   113  	someFunction()
   114  	one, two, three, four, five := []int{}, []float{}, []float{}, 100, []byte{} // comment 5
   115  }
   116  `
   117  	verifyAutoFix(t, EmptySlice, files, expects)
   118  }