github.com/songshiyun/revive@v1.1.5-0.20220323112655-f8433a19b3c5/testdata/unused-param.go (about) 1 package fixtures 2 3 import ( 4 "fmt" 5 "go/ast" 6 "io/ioutil" 7 "os" 8 "runtime" 9 "testing" 10 "time" 11 12 "github.com/songshiyun/revive/lint" 13 ) 14 15 func f0(param int) { 16 param := param 17 } 18 19 func f1(param int) { // MATCH /parameter 'param' seems to be unused, consider removing or renaming it as _/ 20 if param := fn(); predicate(param) { 21 // do stuff 22 } 23 } 24 25 func f2(param int) { // MATCH /parameter 'param' seems to be unused, consider removing or renaming it as _/ 26 switch param := fn(); param { 27 default: 28 29 } 30 } 31 32 func f3(param myStruct) { 33 a := param.field 34 } 35 36 func f4(param myStruct, c int) { // MATCH /parameter 'c' seems to be unused, consider removing or renaming it as _/ 37 param.field = "aString" 38 param.c = "sss" 39 } 40 41 func f5(a int, _ float) { // MATCH /parameter 'a' seems to be unused, consider removing or renaming it as _/ 42 fmt.Printf("Hello, Golang\n") 43 { 44 if true { 45 a := 2 46 b := a 47 } 48 } 49 } 50 51 func f6(unused string) { // MATCH /parameter 'unused' seems to be unused, consider removing or renaming it as _/ 52 switch unused := runtime.GOOS; unused { 53 case "darwin": 54 fmt.Println("OS X.") 55 case "linux": 56 fmt.Println("Linux.") 57 default: 58 fmt.Printf("%s.", unused) 59 } 60 for unused := 0; unused < 10; unused++ { 61 sum += unused 62 } 63 { 64 unused := 1 65 } 66 } 67 68 func f6bis(unused string) { 69 switch unused := runtime.GOOS; unused { 70 case "darwin": 71 fmt.Println("OS X.") 72 case "linux": 73 fmt.Println("Linux.") 74 default: 75 fmt.Printf("%s.", unused) 76 } 77 for unused := 0; unused < 10; unused++ { 78 sum += unused 79 } 80 { 81 unused := 1 82 } 83 84 fmt.Print(unused) 85 } 86 87 func f7(pl int) { 88 for i := 0; pl < i; i-- { 89 90 } 91 } 92 93 func getCompareFailCause(n *node, which int, prevValue string, prevIndex uint64) string { 94 switch which { 95 case CompareIndexNotMatch: 96 return fmt.Sprintf("[%v != %v]", prevIndex, n.ModifiedIndex) 97 case CompareValueNotMatch: 98 return fmt.Sprintf("[%v != %v]", prevValue, n.Value) 99 default: 100 return fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) 101 } 102 } 103 104 func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error { // MATCH /parameter 'src' seems to be unused, consider removing or renaming it as _/ 105 l := lint.New(func(file string) ([]byte, error) { 106 return ioutil.ReadFile(baseDir + file) 107 }) 108 109 ps, err := l.Lint([][]string{[]string{fi.Name()}}, rules, lint.Config{ 110 Rules: config, 111 }) 112 if err != nil { 113 return err 114 } 115 116 failures := "" 117 for p := range ps { 118 failures += p.Failure 119 } 120 if failures != "" { 121 t.Errorf("Expected the rule to pass but got the following failures: %s", failures) 122 } 123 return nil 124 } 125 126 func (w lintCyclomatic) Visit(n ast.Node) ast.Visitor { // MATCH /parameter 'n' seems to be unused, consider removing or renaming it as _/ 127 f := w.file 128 for _, decl := range f.AST.Decls { 129 if fn, ok := decl.(*ast.FuncDecl); ok { 130 c := complexity(fn) 131 if c > w.complexity { 132 w.onFailure(lint.Failure{ 133 Confidence: 1, 134 Category: "maintenance", 135 Failure: fmt.Sprintf("function %s has cyclomatic complexity %d", funcName(fn), c), 136 Node: fn, 137 }) 138 } 139 } 140 } 141 return nil 142 } 143 144 func ext۰time۰Sleep(fr *frame, args []value) value { // MATCH /parameter 'fr' seems to be unused, consider removing or renaming it as _/ 145 time.Sleep(time.Duration(args[0].(int64))) 146 return nil 147 } 148 149 func (c *chanList) remove(id uint32) { 150 id -= c.offset 151 } 152 153 func (c *chanList) remove1(id uint32) { 154 id *= c.offset 155 } 156 157 func (c *chanList) remove2(id uint32) { 158 id /= c.offset 159 } 160 161 func (c *chanList) remove3(id uint32) { 162 id += c.offset 163 } 164 165 func encodeFixed64Rpc(dAtA []byte, offset int, v uint64, i int) int { 166 dAtA[offset+i] = uint8(v) 167 168 return 8 169 }