From d1118fe3c8eb967df0d91ae0f47d551d42387801 Mon Sep 17 00:00:00 2001 From: Maik Schreiber Date: Tue, 29 Mar 2022 12:46:29 +0200 Subject: [PATCH] improve unit tests --- testdata/src/chan-recv-ok/test.go | 14 -- testdata/src/chanrecv/chanrecv.go | 11 + testdata/src/conventional/conventional.go | 51 +++++ testdata/src/decl/test.go | 204 ------------------ testdata/src/distance/distance.go | 10 + testdata/src/distancereceiver/receiver.go | 7 + testdata/src/distancereturn/return.go | 6 + .../src/falsenegativechanrecv/chanrecv .go | 21 ++ .../src/falsenegativemapindex/mapindex.go | 21 ++ .../src/falsenegativetypeassert/typeassert.go | 21 ++ testdata/src/falsepositive/compositelit.go | 25 +++ testdata/src/ignoredecls/ignoredecls.go | 94 ++++++++ testdata/src/ignoredeclsreceiver/receiver.go | 21 ++ testdata/src/ignoredeclsreturn/return.go | 11 + testdata/src/ignorenames/ignorenames.go | 31 +++ testdata/src/ignorenamesreceiver/receiver.go | 12 ++ testdata/src/ignorenamesreturn/return.go | 11 + testdata/src/map-index-ok/test.go | 14 -- testdata/src/mapindex/mapindex.go | 11 + testdata/src/namelen/namelen.go | 20 ++ testdata/src/namelenreturn/return.go | 11 + testdata/src/receiver/test.go | 14 -- testdata/src/return/test.go | 14 -- testdata/src/test/test.go | 169 --------------- testdata/src/type-assert-ok/test.go | 38 ---- testdata/src/typeassert/typeassert.go | 11 + testdata/src/warnings/chanrecv.go | 11 + testdata/src/warnings/compositelit.go | 17 ++ testdata/src/warnings/const.go | 12 ++ testdata/src/warnings/conventional.go | 51 +++++ testdata/src/warnings/mapindex.go | 11 + testdata/src/warnings/param.go | 10 + testdata/src/warnings/typeassert.go | 11 + testdata/src/warnings/variable.go | 55 +++++ testdata/src/warningsreceiver/receiver.go | 12 ++ testdata/src/warningsreturn/return.go | 11 + varnamelen.go | 4 +- varnamelen_test.go | 181 ++++++++++------ 38 files changed, 722 insertions(+), 537 deletions(-) delete mode 100644 testdata/src/chan-recv-ok/test.go create mode 100644 testdata/src/chanrecv/chanrecv.go create mode 100644 testdata/src/conventional/conventional.go delete mode 100644 testdata/src/decl/test.go create mode 100644 testdata/src/distance/distance.go create mode 100644 testdata/src/distancereceiver/receiver.go create mode 100644 testdata/src/distancereturn/return.go create mode 100644 testdata/src/falsenegativechanrecv/chanrecv .go create mode 100644 testdata/src/falsenegativemapindex/mapindex.go create mode 100644 testdata/src/falsenegativetypeassert/typeassert.go create mode 100644 testdata/src/falsepositive/compositelit.go create mode 100644 testdata/src/ignoredecls/ignoredecls.go create mode 100644 testdata/src/ignoredeclsreceiver/receiver.go create mode 100644 testdata/src/ignoredeclsreturn/return.go create mode 100644 testdata/src/ignorenames/ignorenames.go create mode 100644 testdata/src/ignorenamesreceiver/receiver.go create mode 100644 testdata/src/ignorenamesreturn/return.go delete mode 100644 testdata/src/map-index-ok/test.go create mode 100644 testdata/src/mapindex/mapindex.go create mode 100644 testdata/src/namelen/namelen.go create mode 100644 testdata/src/namelenreturn/return.go delete mode 100644 testdata/src/receiver/test.go delete mode 100644 testdata/src/return/test.go delete mode 100644 testdata/src/test/test.go delete mode 100644 testdata/src/type-assert-ok/test.go create mode 100644 testdata/src/typeassert/typeassert.go create mode 100644 testdata/src/warnings/chanrecv.go create mode 100644 testdata/src/warnings/compositelit.go create mode 100644 testdata/src/warnings/const.go create mode 100644 testdata/src/warnings/conventional.go create mode 100644 testdata/src/warnings/mapindex.go create mode 100644 testdata/src/warnings/param.go create mode 100644 testdata/src/warnings/typeassert.go create mode 100644 testdata/src/warnings/variable.go create mode 100644 testdata/src/warningsreceiver/receiver.go create mode 100644 testdata/src/warningsreturn/return.go diff --git a/testdata/src/chan-recv-ok/test.go b/testdata/src/chan-recv-ok/test.go deleted file mode 100644 index e354626..0000000 --- a/testdata/src/chan-recv-ok/test.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -func foo() { - x := make(chan int) - - _, ok := <-x - println() - println() - println() - println() - println() - println() - println(ok) -} diff --git a/testdata/src/chanrecv/chanrecv.go b/testdata/src/chanrecv/chanrecv.go new file mode 100644 index 0000000..d7e421d --- /dev/null +++ b/testdata/src/chanrecv/chanrecv.go @@ -0,0 +1,11 @@ +package chanrecv + +func ChanRecvOK() { + _, ok := <-make(chan int) + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/conventional/conventional.go b/testdata/src/conventional/conventional.go new file mode 100644 index 0000000..f7336b1 --- /dev/null +++ b/testdata/src/conventional/conventional.go @@ -0,0 +1,51 @@ +package conventional + +import ( + "context" + "testing" +) + +func Variable() { + var ( + ctx context.Context + b *testing.B + f *testing.F + m *testing.M + pb *testing.PB + t *testing.T + tb testing.TB + ) + // fill + // fill + // fill + // fill + // fill + _ = ctx + _ = b + _ = f + _ = m + _ = pb + _ = t + _ = tb +} + +func Param(ctx context.Context, + b *testing.B, + f *testing.F, + m *testing.M, + pb *testing.PB, + t *testing.T, + tb testing.TB) { + // fill + // fill + // fill + // fill + // fill + _ = ctx + _ = b + _ = f + _ = m + _ = pb + _ = t + _ = tb +} diff --git a/testdata/src/decl/test.go b/testdata/src/decl/test.go deleted file mode 100644 index a9e9ecb..0000000 --- a/testdata/src/decl/test.go +++ /dev/null @@ -1,204 +0,0 @@ -package main - -import ( - bb "bytes" - "context" - "math" - "strings" -) - -const C = 299792458 - -func foo(c context.Context) { - println() - println() - println() - println() - println() - println() - println(c) - - println(C) -} - -func foo2() (c context.Context) { - println() - println() - println() - println() - println() - println() - c = context.Background() - return -} - -func foo3() { - var c context.Context - println() - println() - println() - println() - println() - println() - c = context.Background() - println(c) - return -} - -func foo4(i int) { - println() - println() - println() - println() - println() - println() - println(i) -} - -func foo5() (i int) { - println() - println() - println() - println() - println() - println() - i = 123 - return -} - -func foo6() { - var i int - println() - println() - println() - println() - println() - println() - i = 123 - println(i) - return -} - -func foo7() { - var ip *int - println() - println() - println() - println() - println() - println() - *ip = 123 - println(*ip) - return -} - -func foo8() { - var b *strings.Builder = &strings.Builder{} - println() - println() - println() - println() - println() - println() - println(b.String()) - return -} - -func foo9() { - c := context.Background() - println() - println() - println() - println() - println() - println() - _ = c.Err() -} - -func foo10() { - b := bb.Buffer{} - println() - println() - println() - println() - println() - println() - println(b.String()) - - d := &bb.Buffer{} - println() - println() - println() - println() - println() - println() - println(d.String()) -} - -func foo11() { - f := foo11 - println() - println() - println() - println() - println() - println() - println(f) -} - -func foo12() { - m := map[int]*bb.Buffer{} - println() - println() - println() - println() - println() - println() - println(m) -} - -func foo13() { - mi := math.MinInt8 - println() - println() - println() - println() - println() - println() - println(mi) -} - -func foo14() { - for i := range []string{"a", "b", "c"} { - println() - println() - println() - println() - println() - println() - println(i) - } - - for i, s := range []string{"a", "b", "c"} { - println() - println() - println() - println() - println() - println() - println(i, s) - } - - i, s := getTwo() - println() - println() - println() - println() - println() - println() - println(i, s) -} - -func getTwo() (int, string) { - return 1, "2" -} diff --git a/testdata/src/distance/distance.go b/testdata/src/distance/distance.go new file mode 100644 index 0000000..3e7f867 --- /dev/null +++ b/testdata/src/distance/distance.go @@ -0,0 +1,10 @@ +package distance + +func Variable() { + x := 123 + _ = x +} + +func Param(x int) { + _ = x +} diff --git a/testdata/src/distancereceiver/receiver.go b/testdata/src/distancereceiver/receiver.go new file mode 100644 index 0000000..ad0725e --- /dev/null +++ b/testdata/src/distancereceiver/receiver.go @@ -0,0 +1,7 @@ +package warningsreceiver + +type foo struct{} + +func (f *foo) Receiver() { + _ = f +} diff --git a/testdata/src/distancereturn/return.go b/testdata/src/distancereturn/return.go new file mode 100644 index 0000000..a691823 --- /dev/null +++ b/testdata/src/distancereturn/return.go @@ -0,0 +1,6 @@ +package distancereturn + +func Return() (x int) { + x = 123 + return +} diff --git a/testdata/src/falsenegativechanrecv/chanrecv .go b/testdata/src/falsenegativechanrecv/chanrecv .go new file mode 100644 index 0000000..adaba84 --- /dev/null +++ b/testdata/src/falsenegativechanrecv/chanrecv .go @@ -0,0 +1,21 @@ +package falsenegativechanrecv + +func ChanRecvOK_Name() { + _, o := <-make(chan int) // want `variable name 'o' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = o +} + +func ChanRecvOK_Not2() { + ok := <-make(chan int) // want `variable name 'ok' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/falsenegativemapindex/mapindex.go b/testdata/src/falsenegativemapindex/mapindex.go new file mode 100644 index 0000000..7b6dfdf --- /dev/null +++ b/testdata/src/falsenegativemapindex/mapindex.go @@ -0,0 +1,21 @@ +package falsenegativemapindex + +func MapIndexOK_Name() { + _, o := map[int]int{}[0] // want `variable name 'o' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = o +} + +func MapIndexOK_Not2() { + ok := map[int]int{}[0] // want `variable name 'ok' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/falsenegativetypeassert/typeassert.go b/testdata/src/falsenegativetypeassert/typeassert.go new file mode 100644 index 0000000..37aa3f4 --- /dev/null +++ b/testdata/src/falsenegativetypeassert/typeassert.go @@ -0,0 +1,21 @@ +package falsenegativetypeassert + +func TypeAssertOK_Name() { + _, o := interface{}(1).(int) // want `variable name 'o' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = o +} + +func TypeAssertOK_Not2() { + ok := interface{}(1).(int) // want `variable name 'ok' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/falsepositive/compositelit.go b/testdata/src/falsepositive/compositelit.go new file mode 100644 index 0000000..c7f4630 --- /dev/null +++ b/testdata/src/falsepositive/compositelit.go @@ -0,0 +1,25 @@ +package warnings + +import "testing" + +func CompositeLiteral() { + x := 123 + _ = x + // fill + // fill + // fill + // fill + // fill + i := 123 + _ = struct{ x int }{x: i} +} + +func CompositeLiteral_Conventional() { + t := &testing.T{} + // fill + // fill + // fill + // fill + // fill + _ = struct{ t testing.TB }{t: t} +} diff --git a/testdata/src/ignoredecls/ignoredecls.go b/testdata/src/ignoredecls/ignoredecls.go new file mode 100644 index 0000000..6e78801 --- /dev/null +++ b/testdata/src/ignoredecls/ignoredecls.go @@ -0,0 +1,94 @@ +package ignoredecls + +import ( + "bytes" + strs "strings" +) + +const I = 123 + +func Const() { + // fill + // fill + // fill + // fill + // fill + _ = I +} + +func Type_Int() { + i := 123 + // fill + // fill + // fill + // fill + // fill + _ = i +} + +func Type_Map() { + i := map[string]string{} + // fill + // fill + // fill + // fill + // fill + _ = i["foo"] +} + +func Type_Pointer() { + i := &bytes.Buffer{} + // fill + // fill + // fill + // fill + // fill + _ = i +} + +func Type_ImportAlias() { + i := &strs.Builder{} + // fill + // fill + // fill + // fill + // fill + _ = i +} + +func Param_Int(i int) { + // fill + // fill + // fill + // fill + // fill + _ = i +} + +func Param_Pointer(i *bytes.Buffer) { + // fill + // fill + // fill + // fill + // fill + _ = i +} + +func Param_ImportAlias(i *strs.Builder) { + // fill + // fill + // fill + // fill + // fill + _ = i +} + +func Return() (i int) { + // fill + // fill + // fill + // fill + // fill + i = 123 + return +} diff --git a/testdata/src/ignoredeclsreceiver/receiver.go b/testdata/src/ignoredeclsreceiver/receiver.go new file mode 100644 index 0000000..1de5f10 --- /dev/null +++ b/testdata/src/ignoredeclsreceiver/receiver.go @@ -0,0 +1,21 @@ +package ignoredeclsreceiver + +type foo struct{} + +func (f foo) Receiver_Struct() { + // fill + // fill + // fill + // fill + // fill + _ = f +} + +func (f *foo) Receiver_Pointer() { + // fill + // fill + // fill + // fill + // fill + _ = f +} diff --git a/testdata/src/ignoredeclsreturn/return.go b/testdata/src/ignoredeclsreturn/return.go new file mode 100644 index 0000000..575e04d --- /dev/null +++ b/testdata/src/ignoredeclsreturn/return.go @@ -0,0 +1,11 @@ +package ignoredeclsreturn + +func Return() (i int) { + // fill + // fill + // fill + // fill + // fill + i = 123 + return +} diff --git a/testdata/src/ignorenames/ignorenames.go b/testdata/src/ignorenames/ignorenames.go new file mode 100644 index 0000000..b2e19a5 --- /dev/null +++ b/testdata/src/ignorenames/ignorenames.go @@ -0,0 +1,31 @@ +package ignorenames + +const I = 123 + +func Const() { + // fill + // fill + // fill + // fill + // fill + _ = I +} + +func Variable() { + i := 123 + // fill + // fill + // fill + // fill + // fill + _ = i +} + +func Param(i int) { + // fill + // fill + // fill + // fill + // fill + _ = i +} diff --git a/testdata/src/ignorenamesreceiver/receiver.go b/testdata/src/ignorenamesreceiver/receiver.go new file mode 100644 index 0000000..40e944b --- /dev/null +++ b/testdata/src/ignorenamesreceiver/receiver.go @@ -0,0 +1,12 @@ +package warningsreceiver + +type foo struct{} + +func (f *foo) Receiver() { + // fill + // fill + // fill + // fill + // fill + _ = f +} diff --git a/testdata/src/ignorenamesreturn/return.go b/testdata/src/ignorenamesreturn/return.go new file mode 100644 index 0000000..76194cc --- /dev/null +++ b/testdata/src/ignorenamesreturn/return.go @@ -0,0 +1,11 @@ +package ignorenamesreturn + +func Return() (i int) { + // fill + // fill + // fill + // fill + // fill + i = 123 + return +} diff --git a/testdata/src/map-index-ok/test.go b/testdata/src/map-index-ok/test.go deleted file mode 100644 index 9b6a672..0000000 --- a/testdata/src/map-index-ok/test.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -func foo() { - var x map[int]int - - _, ok := x[123] - println() - println() - println() - println() - println() - println() - println(ok) -} diff --git a/testdata/src/mapindex/mapindex.go b/testdata/src/mapindex/mapindex.go new file mode 100644 index 0000000..eda4987 --- /dev/null +++ b/testdata/src/mapindex/mapindex.go @@ -0,0 +1,11 @@ +package mapindex + +func MapIndexOK() { + _, ok := map[int]int{}[0] + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/namelen/namelen.go b/testdata/src/namelen/namelen.go new file mode 100644 index 0000000..5511763 --- /dev/null +++ b/testdata/src/namelen/namelen.go @@ -0,0 +1,20 @@ +package namelen + +func Variable() { + longName := 123 + // fill + // fill + // fill + // fill + // fill + _ = longName +} + +func Param(longName int) { + // fill + // fill + // fill + // fill + // fill + _ = longName +} diff --git a/testdata/src/namelenreturn/return.go b/testdata/src/namelenreturn/return.go new file mode 100644 index 0000000..433b496 --- /dev/null +++ b/testdata/src/namelenreturn/return.go @@ -0,0 +1,11 @@ +package namelenreturn + +func Return() (longName int) { + // fill + // fill + // fill + // fill + // fill + longName = 123 + return +} diff --git a/testdata/src/receiver/test.go b/testdata/src/receiver/test.go deleted file mode 100644 index 1e4cc12..0000000 --- a/testdata/src/receiver/test.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -type test struct { - x int -} - -func (t *test) foo() { // want "parameter name 't' is too short for the scope of its usage" - t.x++ - t.x++ - t.x++ - t.x++ - t.x++ - t.x++ -} diff --git a/testdata/src/return/test.go b/testdata/src/return/test.go deleted file mode 100644 index bbce990..0000000 --- a/testdata/src/return/test.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -func foo() (r int, r2 int, longRV int, i int) { // want "return value name 'r' is too short for the scope of its usage" - r2++ - longRV++ - r++ - r++ - r++ - r++ - r++ - r++ - i++ - return -} diff --git a/testdata/src/test/test.go b/testdata/src/test/test.go deleted file mode 100644 index c67dc91..0000000 --- a/testdata/src/test/test.go +++ /dev/null @@ -1,169 +0,0 @@ -package main - -import ( - "context" - "testing" -) - -type test struct { - x int -} - -const ( - C = 299792458 // want "constant name 'C' is too short for the scope of its usage" - CI = 123 -) - -func foo(p int, p2 int, ip int, longParam int) (rv string) { // want "parameter name 'p' is too short for the scope of its usage" - p2 += longParam - - x := 123 // want "variable name 'x' is too short for the scope of its usage" - aLongOne := 555 - i := 10 - x++ - x++ - x++ - x++ - x++ - x++ - aLongOne += x + i - - ints := []int{1, 2, 3} - for _, y := range ints { // want "variable name 'y' is too short for the scope of its usage" - y++ - y++ - y++ - y++ - y++ - y++ - y := 123 // want "variable name 'y' is too short for the scope of its usage" - y++ - y++ - y++ - y++ - y++ - y++ - } - - for idx := range ints { - p := idx // want "variable name 'p' is too short for the scope of its usage" - p++ - p++ - p++ - p++ - p++ - p++ - } - - p += ip - - rv = "foo" - - var z int // want "variable name 'z' is too short for the scope of its usage" - println() - println() - println() - println() - println() - println() - println(z) - - println(C) - - println(CI) - - for x := range []struct{}{} { // want "variable name 'x' is too short for the scope of its usage" - println() - println() - println() - println() - println() - println() - println(x) - } - - return -} - -func (t *test) foo() { - t.x++ - t.x++ - t.x++ - t.x++ - t.x++ - t.x++ -} - -func Conventionals(ctx context.Context, t *testing.T, b *testing.B, tb testing.TB, m *testing.M, pb *testing.PB) { - println() - println() - println() - println() - println() - println() - t.Fail() - b.Fail() - tb.Fail() - m.Run() - pb.Next() - ctx.Err() -} - -func Conventionals2(t *testing.T) { - println() - println() - println() - println() - println() - println() - - var _ = struct{ t testing.TB }{t: t} -} - -func Conventionals3() { - var t *testing.T = nil - - println() - println() - println() - println() - println() - println() - - var _ = struct{ t testing.TB }{t: t} -} - -func Conventionals4() { - t := &testing.T{} - - println() - println() - println() - println() - println() - println() - - var _ = struct{ t testing.TB }{t: t} - - println(t) -} - -func switcheroo() { - type inter interface{} - type str struct{} - type str2 struct{} - - var x inter - switch y := x.(type) { // want "variable name 'y' is too short for the scope of its usage" - // fill - // fill - // fill - // fill - // fill - // fill - case *str: - _ = y - case *str2: - _ = y - } -} diff --git a/testdata/src/type-assert-ok/test.go b/testdata/src/type-assert-ok/test.go deleted file mode 100644 index 7713aad..0000000 --- a/testdata/src/type-assert-ok/test.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -func foo() { - var something interface{} - - str, ok := something.(string) - println(str) - println() - println() - println() - println() - println() - println(ok) - - var i int - i, ok = something.(int) - println(i) - println() - println() - println() - println() - println() - println(ok) -} - -func foo2() { - var something interface{} - - if _, ok := something.(string); ok { - println() - println() - println() - println() - println() - println() - println(ok) - } -} diff --git a/testdata/src/typeassert/typeassert.go b/testdata/src/typeassert/typeassert.go new file mode 100644 index 0000000..5381320 --- /dev/null +++ b/testdata/src/typeassert/typeassert.go @@ -0,0 +1,11 @@ +package typeassert + +func TypeAssertOK() { + _, ok := interface{}(1).(int) + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/warnings/chanrecv.go b/testdata/src/warnings/chanrecv.go new file mode 100644 index 0000000..767e285 --- /dev/null +++ b/testdata/src/warnings/chanrecv.go @@ -0,0 +1,11 @@ +package warnings + +func ChanRecvOK() { + _, ok := <-make(chan int) // want `variable name 'ok' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/warnings/compositelit.go b/testdata/src/warnings/compositelit.go new file mode 100644 index 0000000..78a4d43 --- /dev/null +++ b/testdata/src/warnings/compositelit.go @@ -0,0 +1,17 @@ +package warnings + +type compositeLit struct { + x int +} + +func CompositeLiteral() { + i := 123 // want `variable name 'i' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = compositeLit{ + x: i, + } +} diff --git a/testdata/src/warnings/const.go b/testdata/src/warnings/const.go new file mode 100644 index 0000000..4336888 --- /dev/null +++ b/testdata/src/warnings/const.go @@ -0,0 +1,12 @@ +package warnings + +const I = 123 // want `constant name 'I' is too short for the scope of its usage` + +func Const() { + // fill + // fill + // fill + // fill + // fill + _ = I +} diff --git a/testdata/src/warnings/conventional.go b/testdata/src/warnings/conventional.go new file mode 100644 index 0000000..72b81ee --- /dev/null +++ b/testdata/src/warnings/conventional.go @@ -0,0 +1,51 @@ +package warnings + +import ( + "context" + "testing" +) + +func Variable_Conventional() { + var ( + c2 context.Context // want `variable name 'c2' is too short for the scope of its usage` + b2 *testing.B // want `variable name 'b2' is too short for the scope of its usage` + f2 *testing.F // want `variable name 'f2' is too short for the scope of its usage` + m2 *testing.M // want `variable name 'm2' is too short for the scope of its usage` + p2 *testing.PB // want `variable name 'p2' is too short for the scope of its usage` + t2 *testing.T // want `variable name 't2' is too short for the scope of its usage` + t3 testing.TB // want `variable name 't3' is too short for the scope of its usage` + ) + // fill + // fill + // fill + // fill + // fill + _ = c2 + _ = b2 + _ = f2 + _ = m2 + _ = p2 + _ = t2 + _ = t3 +} + +func Param_Conventional(c2 context.Context, // want `parameter name 'c2' is too short for the scope of its usage` + b2 *testing.B, // want `parameter name 'b2' is too short for the scope of its usage` + f2 *testing.F, // want `parameter name 'f2' is too short for the scope of its usage` + m2 *testing.M, // want `parameter name 'm2' is too short for the scope of its usage` + p2 *testing.PB, // want `parameter name 'p2' is too short for the scope of its usage` + t2 *testing.T, // want `parameter name 't2' is too short for the scope of its usage` + t3 testing.TB) { // want `parameter name 't3' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = c2 + _ = b2 + _ = f2 + _ = m2 + _ = p2 + _ = t2 + _ = t3 +} diff --git a/testdata/src/warnings/mapindex.go b/testdata/src/warnings/mapindex.go new file mode 100644 index 0000000..6654c30 --- /dev/null +++ b/testdata/src/warnings/mapindex.go @@ -0,0 +1,11 @@ +package warnings + +func MapIndexOK() { + _, ok := map[int]int{}[0] // want `variable name 'ok' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/warnings/param.go b/testdata/src/warnings/param.go new file mode 100644 index 0000000..051c874 --- /dev/null +++ b/testdata/src/warnings/param.go @@ -0,0 +1,10 @@ +package warnings + +func Param(i int) { // want `parameter name 'i' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + i = 123 +} diff --git a/testdata/src/warnings/typeassert.go b/testdata/src/warnings/typeassert.go new file mode 100644 index 0000000..3d9a39d --- /dev/null +++ b/testdata/src/warnings/typeassert.go @@ -0,0 +1,11 @@ +package warnings + +func TypeAssertOK() { + _, ok := interface{}(1).(int) // want `variable name 'ok' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = ok +} diff --git a/testdata/src/warnings/variable.go b/testdata/src/warnings/variable.go new file mode 100644 index 0000000..771eca0 --- /dev/null +++ b/testdata/src/warnings/variable.go @@ -0,0 +1,55 @@ +package warnings + +func Variable_Assign() { + x := 123 // want `variable name 'x' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = x +} + +func Variable_Assign_Range() { + for i := range []int{1, 2, 3} { // want `variable name 'i' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = i + } +} + +func Variable_Assign_Range_2() { + for _, i := range []int{1, 2, 3} { // want `variable name 'i' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = i + } +} + +func Variable_ValueSpec() { + var x = 123 // want `variable name 'x' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = x +} + +func Variable_TypeSwitch() { + switch x := interface{}(1).(type) { // want `variable name 'x' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + case int: + _ = x + } +} diff --git a/testdata/src/warningsreceiver/receiver.go b/testdata/src/warningsreceiver/receiver.go new file mode 100644 index 0000000..5ca3eb8 --- /dev/null +++ b/testdata/src/warningsreceiver/receiver.go @@ -0,0 +1,12 @@ +package warningsreceiver + +type foo struct{} + +func (f *foo) Receiver() { // want `parameter name 'f' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + _ = f +} diff --git a/testdata/src/warningsreturn/return.go b/testdata/src/warningsreturn/return.go new file mode 100644 index 0000000..3a72d94 --- /dev/null +++ b/testdata/src/warningsreturn/return.go @@ -0,0 +1,11 @@ +package warnings + +func Return() (i int) { // want `return value name 'i' is too short for the scope of its usage` + // fill + // fill + // fill + // fill + // fill + i = 123 + return +} diff --git a/varnamelen.go b/varnamelen.go index 700354f..d6af1d1 100644 --- a/varnamelen.go +++ b/varnamelen.go @@ -121,7 +121,7 @@ var conventionalDecls = []declaration{ parseDeclaration("tb testing.TB"), } -// NewAnalyzer returns a new analyzer that checks variable name length. +// NewAnalyzer returns a new analyzer. func NewAnalyzer() *analysis.Analyzer { vnl := varNameLen{ maxDistance: defaultMaxDistance, @@ -138,7 +138,7 @@ func NewAnalyzer() *analysis.Analyzer { "to comprehend.", Run: func(pass *analysis.Pass) (interface{}, error) { - vnl.run(pass) + (&vnl).run(pass) return nil, nil }, diff --git a/varnamelen_test.go b/varnamelen_test.go index d0254d3..3478795 100644 --- a/varnamelen_test.go +++ b/varnamelen_test.go @@ -4,99 +4,144 @@ import ( "os" "testing" - "github.com/matryer/is" "golang.org/x/tools/go/analysis/analysistest" ) -func TestVarNameLen_Run(t *testing.T) { - a := NewAnalyzer() - _ = a.Flags.Set("minNameLength", "4") - _ = a.Flags.Set("ignoreNames", "i, ip, CI") +func TestVarNameLen_Run_Warnings(t *testing.T) { + run(t, "warnings", nil) +} - wd, _ := os.Getwd() - analysistest.Run(t, wd+"/testdata", a, "test") +func TestVarNameLen_Run_Warnings_Return(t *testing.T) { + run(t, "warningsreturn", map[string]string{ + "checkReturn": "true", + }) } -func TestVarNameLen_Run_CheckReceiver(t *testing.T) { - a := NewAnalyzer() - _ = a.Flags.Set("minNameLength", "4") - _ = a.Flags.Set("checkReceiver", "true") +func TestVarNameLen_Run_Warnings_Receiver(t *testing.T) { + run(t, "warningsreceiver", map[string]string{ + "checkReceiver": "true", + }) +} - wd, _ := os.Getwd() - analysistest.Run(t, wd+"/testdata", a, "receiver") +func TestVarNameLen_Run_NameLen(t *testing.T) { + run(t, "namelen", nil) } -func TestVarNameLen_Run_CheckReturn(t *testing.T) { - analyzer := NewAnalyzer() - _ = analyzer.Flags.Set("minNameLength", "4") - _ = analyzer.Flags.Set("ignoreNames", "i") - _ = analyzer.Flags.Set("checkReturn", "true") +func TestVarNameLen_Run_NameLen_Return(t *testing.T) { + run(t, "namelenreturn", map[string]string{ + "checkReturn": "true", + }) +} - wd, _ := os.Getwd() - analysistest.Run(t, wd+"/testdata", analyzer, "return") +func TestVarNameLen_Run_Distance(t *testing.T) { + run(t, "distance", nil) +} + +func TestVarNameLen_Run_Distance_Return(t *testing.T) { + run(t, "distancereturn", map[string]string{ + "checkReturn": "true", + }) +} + +func TestVarNameLen_Run_Distance_Receiver(t *testing.T) { + run(t, "distancereceiver", map[string]string{ + "checkReceiver": "true", + }) +} + +func TestVarNameLen_Run_IgnoreNames(t *testing.T) { + run(t, "ignorenames", map[string]string{ + "ignoreNames": "i, I", + }) +} + +func TestVarNameLen_Run_IgnoreNames_Return(t *testing.T) { + run(t, "ignorenamesreturn", map[string]string{ + "ignoreNames": "i", + "checkReturn": "true", + }) +} + +func TestVarNameLen_Run_IgnoreNames_Receiver(t *testing.T) { + run(t, "ignorenamesreceiver", map[string]string{ + "ignoreNames": "f", + "checkReceiver": "true", + }) +} + +func TestVarNameLen_Run_IgnoreDecls(t *testing.T) { + run(t, "ignoredecls", map[string]string{ + "ignoreDecls": "i int, i map[string]string, i *bytes.Buffer, i *strs.Builder, const I", + }) +} + +func TestVarNameLen_Run_IgnoreDecls_Return(t *testing.T) { + run(t, "ignoredeclsreturn", map[string]string{ + "ignoreDecls": "i int", + "checkReturn": "true", + }) +} + +func TestVarNameLen_Run_IgnoreDecls_Receiver(t *testing.T) { + run(t, "ignoredeclsreceiver", map[string]string{ + "ignoreDecls": "f foo, f *foo", + "checkReceiver": "true", + }) } func TestVarNameLen_Run_IgnoreTypeAssertOk(t *testing.T) { - analyzer := NewAnalyzer() - _ = analyzer.Flags.Set("minNameLength", "4") - _ = analyzer.Flags.Set("ignoreTypeAssertOk", "true") + run(t, "typeassert", map[string]string{ + "ignoreTypeAssertOk": "true", + }) +} - wd, _ := os.Getwd() - analysistest.Run(t, wd+"/testdata", analyzer, "type-assert-ok") +func TestVarNameLen_Run_IgnoreChanRecvOk(t *testing.T) { + run(t, "chanrecv", map[string]string{ + "ignoreChanRecvOk": "true", + }) } func TestVarNameLen_Run_IgnoreMapIndexOk(t *testing.T) { - analyzer := NewAnalyzer() - _ = analyzer.Flags.Set("minNameLength", "4") - _ = analyzer.Flags.Set("ignoreMapIndexOk", "true") + run(t, "mapindex", map[string]string{ + "ignoreMapIndexOk": "true", + }) +} - wd, _ := os.Getwd() - analysistest.Run(t, wd+"/testdata", analyzer, "map-index-ok") +func TestVarNameLen_Run_Conventional(t *testing.T) { + run(t, "conventional", nil) } -func TestVarNameLen_Run_IgnoreChannelReceiveOk(t *testing.T) { - analyzer := NewAnalyzer() - _ = analyzer.Flags.Set("minNameLength", "4") - _ = analyzer.Flags.Set("ignoreChanRecvOk", "true") +func TestVarNameLen_Run_FalsePositive(t *testing.T) { + run(t, "falsepositive", nil) +} - wd, _ := os.Getwd() - analysistest.Run(t, wd+"/testdata", analyzer, "chan-recv-ok") +func TestVarNameLen_Run_FalseNegative_TypeAssertOk(t *testing.T) { + run(t, "falsenegativetypeassert", map[string]string{ + "ignoreTypeAssertOk": "true", + }) } -func TestVarNameLen_Run_IgnoreDeclarations(t *testing.T) { - analyzer := NewAnalyzer() - _ = analyzer.Flags.Set("minNameLength", "4") - _ = analyzer.Flags.Set("checkReturn", "true") - _ = analyzer.Flags.Set("ignoreDecls", "c context.Context, b bb.Buffer, b *strings.Builder, d *bb.Buffer, i int, ip *int, const C, f func(), m map[int]*bb.Buffer, mi int, s string") +func TestVarNameLen_Run_FalseNegative_ChanRecvOk(t *testing.T) { + run(t, "falsenegativechanrecv", map[string]string{ + "ignoreChanRecvOk": "true", + }) +} - wd, _ := os.Getwd() - analysistest.Run(t, wd+"/testdata", analyzer, "decl") -} - -func TestParseDeclaration(t *testing.T) { - tests := []struct { - givenDecl string - wantDecl declaration - }{ - { - givenDecl: "t *testing.T", - wantDecl: declaration{name: "t", typ: "*testing.T"}, - }, - { - givenDecl: "c echo.Context", - wantDecl: declaration{name: "c", typ: "echo.Context"}, - }, - { - givenDecl: "const C", - wantDecl: declaration{name: "C", constant: true}, - }, - } +func TestVarNameLen_Run_FalseNegative_MapIndexOk(t *testing.T) { + run(t, "falsenegativemapindex", map[string]string{ + "ignoreMapIndexOk": "true", + }) +} + +func run(t *testing.T, pkg string, flags map[string]string) { + t.Helper() - for _, test := range tests { - t.Run(test.givenDecl, func(t *testing.T) { - is := is.New(t) + analyzer := NewAnalyzer() - is.Equal(parseDeclaration(test.givenDecl), test.wantDecl) - }) + for k, v := range flags { + _ = analyzer.Flags.Set(k, v) } + + wd, _ := os.Getwd() + analysistest.Run(t, wd+"/testdata", analyzer, pkg) }