gopkg.in/alecthomas/gometalinter.v3@v3.0.0/regressiontests/unparam_test.go (about)

     1  package regressiontests
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestUnparam(t *testing.T) {
     8  	t.Parallel()
     9  	source := `package foo
    10  
    11  import (
    12  	"errors"
    13  	"log"
    14  	"net/http"
    15  )
    16  
    17  type FooType int
    18  
    19  func AllUsed(a, b FooType) FooType { return a + b }
    20  
    21  func OneUnused(a, b FooType) FooType { return a }
    22  
    23  func doWork() {}
    24  
    25  var Sink interface{}
    26  
    27  func Parent() {
    28  	oneUnused := func(f FooType) {
    29  		doWork()
    30  	}
    31  	Sink = oneUnused
    32  }
    33  
    34  func Handler(w http.ResponseWriter, r *http.Request) {
    35  	w.Write([]byte("hi"))
    36  }
    37  
    38  type FooIface interface {
    39  	foo(w http.ResponseWriter, code FooType) error
    40  }
    41  
    42  func FooImpl(w http.ResponseWriter, code FooType) error {
    43  	w.Write([]byte("hi"))
    44  	return nil
    45  }
    46  
    47  func (f FooType) AllUsed(a FooType) FooType { return f + a }
    48  
    49  func (f FooType) OneUnused(a FooType) FooType { return f }
    50  
    51  func DummyImpl(f FooType) {}
    52  
    53  func PanicImpl(f FooType) { panic("dummy") }
    54  
    55  func NonPanicImpl(w http.ResponseWriter, f FooType) {
    56  	for i := 0; i < 10; i++ {
    57  		w.Write([]byte("foo"))
    58  	}
    59  	panic("default")
    60  }
    61  
    62  func endlessLoop(w http.ResponseWriter) {
    63  	for {
    64  		w.Write([]byte("foo"))
    65  	}
    66  }
    67  
    68  func NonPanicImpl2(w http.ResponseWriter, f FooType) {
    69  	endlessLoop(w)
    70  	panic("unreachable")
    71  }
    72  
    73  func throw(v ...interface{}) {}
    74  
    75  func ThrowImpl(f FooType) { throw("dummy") }
    76  
    77  func ZeroImpl(f FooType) (int, string, []byte) { return 0, "", nil }
    78  
    79  func ErrorsImpl(f FooType) error { return errors.New("unimpl") }
    80  
    81  const ConstFoo = FooType(123)
    82  
    83  func (f FooType) Error() string { return "foo" }
    84  
    85  func CustomErrImpl(f FooType) error { return ConstFoo }
    86  
    87  func NonConstImpl(f FooType, s string) error { return f }
    88  
    89  func LogImpl(f FooType) { log.Print("not implemented") }
    90  
    91  type BarFunc func(a FooType, s string) int
    92  
    93  func BarImpl(a FooType, s string) int { return int(a) }
    94  
    95  func NoName(FooType) { doWork() }
    96  
    97  func UnderscoreName(_ FooType) { doWork() }
    98  
    99  type BarStruct struct {
   100  	fn func(a FooType, b byte)
   101  }
   102  
   103  func BarField(a FooType, b byte) { doWork() }
   104  
   105  type Bar2Struct struct {
   106  	st struct {
   107  		fn func(a FooType, r rune)
   108  	}
   109  }
   110  
   111  func Bar2Field(a FooType, r rune) { doWork() }
   112  
   113  func FuncAsParam(fn func(FooType) string) { fn(0) }
   114  
   115  func PassedAsParam(f FooType) string {
   116  	doWork()
   117  	return "foo"
   118  }
   119  
   120  func (f FooType) FuncAsParam2(fn func(FooType) []byte) { fn(0) }
   121  
   122  func PassedAsParam2(f FooType) []byte {
   123  	doWork()
   124  	return nil
   125  }
   126  
   127  type RecursiveIface interface {
   128  	Foo(RecursiveIface)
   129  }
   130  
   131  func AsSliceElem(f FooType) []int {
   132  	doWork()
   133  	return nil
   134  }
   135  
   136  var SliceElems = []func(FooType) []int{AsSliceElem} `
   137  	expected := Issues{
   138  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 13, Col: 19, Message: "parameter b is unused"},
   139  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 20, Col: 20, Message: "parameter f is unused"},
   140  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 34, Col: 37, Message: "parameter code is unused"},
   141  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 41, Col: 28, Message: "parameter a is unused"},
   142  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 47, Col: 42, Message: "parameter f is unused"},
   143  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 60, Col: 43, Message: "parameter f is unused"},
   144  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 79, Col: 30, Message: "parameter s is unused"},
   145  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 85, Col: 25, Message: "parameter s is unused"},
   146  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 95, Col: 15, Message: "parameter a is unused"},
   147  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 95, Col: 26, Message: "parameter b is unused"},
   148  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 103, Col: 16, Message: "parameter a is unused"},
   149  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 103, Col: 27, Message: "parameter r is unused"},
   150  		Issue{Linter: "unparam", Severity: "warning", Path: "test.go", Line: 123, Col: 18, Message: "parameter f is unused"},
   151  	}
   152  	ExpectIssues(t, "unparam", source, expected)
   153  }