diff --git a/checkers.go b/checkers.go index cec159b..74d9339 100644 --- a/checkers.go +++ b/checkers.go @@ -676,3 +676,39 @@ func (c *unitImportChecker) Visit(n ast.Node) bool { } return false } + +type sameTypeArgumentsChecker struct { + checkerBase + + allTypesPresent opVariant + optionalTypesOmited opVariant +} + +func newSameTypeArgumentsChecker(ctxt *context) checker { + c := &sameTypeArgumentsChecker{} + c.ctxt = ctxt + c.allTypesPresent.warning = "use types always after each argument" + c.optionalTypesOmited.warning = "use only one type declaration after several arguments of the same type" + c.op = &operation{ + name: "same type arguments", + variants: []*opVariant{&c.allTypesPresent, &c.optionalTypesOmited}, + } + return c +} + +func (c *sameTypeArgumentsChecker) Visit(n ast.Node) bool { + fun, ok := n.(*ast.FuncDecl) + if !ok { + return true + } + + for _, param := range fun.Type.Params.List { + if len(param.Names) > 1 { + c.ctxt.mark(n, &c.optionalTypesOmited) + } else { + c.ctxt.mark(n, &c.allTypesPresent) + } + } + + return false +} diff --git a/end2end_test.go b/end2end_test.go index 22bb6de..18c3b91 100644 --- a/end2end_test.go +++ b/end2end_test.go @@ -16,6 +16,7 @@ func TestEnd2End(t *testing.T) { "negative_tests1.go", "negative_tests2.go", "negative_tests3.go", + "negative_tests4.go", } for _, filename := range filenames { diff --git a/go.mod b/go.mod index f3f06c9..c80b17c 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,11 @@ module github.com/Quasilyte/go-consistent require ( - github.com/go-toolsmith/astequal v0.0.0-20180903214952-dcb477bfacd6 + github.com/go-toolsmith/astcast v1.0.0 + github.com/go-toolsmith/astequal v1.0.0 github.com/go-toolsmith/astinfo v0.0.0-20180906194353-9809ff7efb21 + github.com/go-toolsmith/pkgload v1.0.0 + github.com/go-toolsmith/typep v1.0.0 github.com/kisielk/gotool v1.0.0 - golang.org/x/tools v0.0.0-20181024171208-a2dc47679d30 + golang.org/x/tools v0.0.0-20190110163146-51295c7ec13a ) diff --git a/go.sum b/go.sum index de86f36..e46b953 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,19 @@ +github.com/go-toolsmith/astcast v1.0.0 h1:JojxlmI6STnFVG9yOImLeGREv8W2ocNUM+iOhR6jE7g= +github.com/go-toolsmith/astcast v1.0.0/go.mod h1:mt2OdQTeAQcY4DQgPSArJjHCcOwlX+Wl/kwN+LbLGQ4= github.com/go-toolsmith/astequal v0.0.0-20180903214952-dcb477bfacd6 h1:aTBUNRTatDDU24gbOEKEoLiDwxtc98ga6K/iMTm6fvs= github.com/go-toolsmith/astequal v0.0.0-20180903214952-dcb477bfacd6/go.mod h1:H+xSiq0+LtiDC11+h1G32h7Of5O3CYFJ99GVbS5lDKY= +github.com/go-toolsmith/astequal v1.0.0 h1:4zxD8j3JRFNyLN46lodQuqz3xdKSrur7U/sr0SDS/gQ= +github.com/go-toolsmith/astequal v1.0.0/go.mod h1:H+xSiq0+LtiDC11+h1G32h7Of5O3CYFJ99GVbS5lDKY= github.com/go-toolsmith/astinfo v0.0.0-20180906194353-9809ff7efb21 h1:wP6mXeB2V/d1P1K7bZ5vDUO3YqEzcvOREOxZPEu3gVI= github.com/go-toolsmith/astinfo v0.0.0-20180906194353-9809ff7efb21/go.mod h1:dDStQCHtmZpYOmjRP/8gHHnCCch3Zz3oEgCdZVdtweU= +github.com/go-toolsmith/pkgload v1.0.0 h1:4DFWWMXVfbcN5So1sBNW9+yeiMqLFGl1wFLTL5R0Tgg= +github.com/go-toolsmith/pkgload v1.0.0/go.mod h1:5eFArkbO80v7Z0kdngIxsRXRMTaX4Ilcwuh3clNrQJc= +github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8= +github.com/go-toolsmith/typep v1.0.0 h1:zKymWyA1TRYvqYrYDrfEMZULyrhcnGY3x7LDKU2XQaA= +github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU= github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= golang.org/x/tools v0.0.0-20181024171208-a2dc47679d30 h1:iZIABIEHjQFp5zqGZgQiaXi5Ue5czJhXyylr2CTtdRY= golang.org/x/tools v0.0.0-20181024171208-a2dc47679d30/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190110163146-51295c7ec13a h1:sSIx6lCxXoSnDgf7mm3vA/EYXI/gFaJupV18uYCUDmQ= +golang.org/x/tools v0.0.0-20190110163146-51295c7ec13a/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/testdata/negative_tests4.go b/testdata/negative_tests4.go new file mode 100644 index 0000000..9e7d2bb --- /dev/null +++ b/testdata/negative_tests4.go @@ -0,0 +1,13 @@ +package ntests4 + +// In this test suite function definitions have all types. No warnings should be generated. + +func someFunc( + a int, b int, c int, d string, e string, +) { + return +} + +func someOtherFunc(a int, b int, c int) { + return +} diff --git a/testdata/positive_tests1.go b/testdata/positive_tests1.go index bdae181..6a35b54 100644 --- a/testdata/positive_tests1.go +++ b/testdata/positive_tests1.go @@ -157,3 +157,15 @@ func defaultCaseOrder(x int, v interface{}) { default: } } + +func allTypes(a int, b int, c int) { + return +} + +func allTypes2(a int, b int, c int) { + return +} + +func omitTypes(a, b, c int) { //= use types always after each argument + return +} diff --git a/testdata/positive_tests2.go b/testdata/positive_tests2.go index 03968ef..464cb24 100644 --- a/testdata/positive_tests2.go +++ b/testdata/positive_tests2.go @@ -160,3 +160,15 @@ func defaultCaseOrder(x int, v interface{}) { default: } } + +func omitTypes(a, b, c int) { + return +} + +func omitTypes2(a, b, c int) { + return +} + +func allTypes(a int, b int, c int) { //= use only one type declaration after several arguments of the same type + return +}