From caa47cc45480eb4140aaa6275811ae764339ee4c Mon Sep 17 00:00:00 2001 From: Sourya Vatsyayan Date: Sat, 11 Apr 2020 13:36:51 +0530 Subject: [PATCH] simple: fix suggested fixes for S1004 Also update x/tools to latest version, as `analysistest.RunWithSuggestedFixes` is very recent. Signed-off-by: Sourya Vatsyayan (cherry picked from commit a4eb707c4ac4bb176e9241aeb886ea7d135f66a4) --- go.mod | 4 ++-- go.sum | 12 ++++++++++++ ir/source_test.go | 4 ++-- lint/testutil/util.go | 11 ++++++++--- simple/lint.go | 4 ++-- simple/lint_test.go | 2 +- simple/testdata/src/compare/compare.go | 2 ++ simple/testdata/src/compare/compare.go.golden | 12 ++++++++++++ simple/testdata/src/compare/dummy.go | 3 +++ .../testdata/src/compare_windows/compare-windows.go | 12 ++++++++++++ simple/testdata/src/compare_windows/dummy.go | 3 +++ 11 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 simple/testdata/src/compare/compare.go.golden create mode 100644 simple/testdata/src/compare/dummy.go create mode 100644 simple/testdata/src/compare_windows/compare-windows.go create mode 100644 simple/testdata/src/compare_windows/dummy.go diff --git a/go.mod b/go.mod index 545cd4b37..5a61c1078 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,6 @@ require ( github.com/google/renameio v0.1.0 github.com/kisielk/gotool v1.0.0 github.com/rogpeppe/go-internal v1.3.0 - golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e - golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d + golang.org/x/mod v0.2.0 + golang.org/x/tools v0.0.0-20200410194907-79a7a3126eef ) diff --git a/go.sum b/go.sum index 894ea2fa7..9d95629a1 100644 --- a/go.sum +++ b/go.sum @@ -9,23 +9,35 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e h1:JgcxKXxCjrA2tyDP/aNU9K0Ck5Czfk6C7e2tMw7+bSI= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d h1:/iIZNFGxc/a7C3yWjGcnboV+Tkc7mxr+p6fDztwoxuM= golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200410194907-79a7a3126eef h1:RHORRhs540cYZYrzgU2CPUyykkwZM78hGdzocOo9P8A= +golang.org/x/tools v0.0.0-20200410194907-79a7a3126eef/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/ir/source_test.go b/ir/source_test.go index 513207027..c3f010f4b 100644 --- a/ir/source_test.go +++ b/ir/source_test.go @@ -52,7 +52,7 @@ func TestObjValueLookup(t *testing.T) { // Each note of the form @ir(x, "BinOp") in testdata/objlookup.go // specifies an expectation that an object named x declared on the // same line is associated with an an ir.Value of type *ir.BinOp. - notes, err := expect.Extract(conf.Fset, f) + notes, err := expect.ExtractGo(conf.Fset, f) if err != nil { t.Fatal(err) } @@ -273,7 +273,7 @@ func testValueForExpr(t *testing.T, testfile string) { return true }) - notes, err := expect.Extract(prog.Fset, f) + notes, err := expect.ExtractGo(prog.Fset, f) if err != nil { t.Fatal(err) } diff --git a/lint/testutil/util.go b/lint/testutil/util.go index c93210a44..b4ce68bec 100644 --- a/lint/testutil/util.go +++ b/lint/testutil/util.go @@ -8,8 +8,9 @@ import ( ) type Test struct { - Dir string - Version string + Dir string + Version string + SuggestedFixes bool } func Run(t *testing.T, analyzers map[string]*analysis.Analyzer, tests map[string][]Test) { @@ -27,7 +28,11 @@ func Run(t *testing.T, analyzers map[string]*analysis.Analyzer, tests map[string t.Fatal(err) } } - analysistest.Run(t, analysistest.TestData(), a, test.Dir) + if test.SuggestedFixes { + analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, test.Dir) + } else { + analysistest.Run(t, analysistest.TestData(), a, test.Dir) + } } }) } diff --git a/simple/lint.go b/simple/lint.go index 91571b6ee..5a9571deb 100644 --- a/simple/lint.go +++ b/simple/lint.go @@ -271,8 +271,8 @@ func CheckStringsContains(pass *analysis.Pass) (interface{}, error) { var ( checkBytesCompareQ = pattern.MustParse(`(BinaryExpr (CallExpr (Function "bytes.Compare") args) op@(Or "==" "!=") (BasicLit "INT" "0"))`) - checkBytesCompareRn = pattern.MustParse(`(CallExpr (SelectorExpr (Ident "bytes") (Ident "Equal")) args)`) - checkBytesCompareRe = pattern.MustParse(`(UnaryExpr "!" (CallExpr (SelectorExpr (Ident "bytes") (Ident "Equal")) args))`) + checkBytesCompareRe = pattern.MustParse(`(CallExpr (SelectorExpr (Ident "bytes") (Ident "Equal")) args)`) + checkBytesCompareRn = pattern.MustParse(`(UnaryExpr "!" (CallExpr (SelectorExpr (Ident "bytes") (Ident "Equal")) args))`) ) func CheckBytesCompare(pass *analysis.Pass) (interface{}, error) { diff --git a/simple/lint_test.go b/simple/lint_test.go index 2ea3238ea..365f883db 100644 --- a/simple/lint_test.go +++ b/simple/lint_test.go @@ -12,7 +12,7 @@ func TestAll(t *testing.T) { "S1001": {{Dir: "copy"}}, "S1002": {{Dir: "bool-cmp"}}, "S1003": {{Dir: "contains"}}, - "S1004": {{Dir: "compare"}}, + "S1004": {{Dir: "compare", SuggestedFixes: true}, {Dir: "compare_windows"}}, "S1005": {{Dir: "CheckBlankOK"}, {Dir: "receive-blank"}, {Dir: "range_go13", Version: "1.3"}, {Dir: "range_go14", Version: "1.4"}}, "S1006": {{Dir: "for-true"}, {Dir: "generated"}}, "S1007": {{Dir: "regexp-raw"}}, diff --git a/simple/testdata/src/compare/compare.go b/simple/testdata/src/compare/compare.go index 435191635..c5d16aa34 100644 --- a/simple/testdata/src/compare/compare.go +++ b/simple/testdata/src/compare/compare.go @@ -1,3 +1,5 @@ +// +build darwin linux + package pkg import "bytes" diff --git a/simple/testdata/src/compare/compare.go.golden b/simple/testdata/src/compare/compare.go.golden new file mode 100644 index 000000000..c9c8c056f --- /dev/null +++ b/simple/testdata/src/compare/compare.go.golden @@ -0,0 +1,12 @@ +// +build darwin linux + +package pkg + +import "bytes" + +func fn() { + _ = bytes.Equal(nil, nil) // want ` bytes.Equal` + _ = !bytes.Equal(nil, nil) // want `!bytes.Equal` + _ = bytes.Compare(nil, nil) > 0 + _ = bytes.Compare(nil, nil) < 0 +} diff --git a/simple/testdata/src/compare/dummy.go b/simple/testdata/src/compare/dummy.go new file mode 100644 index 000000000..71fe5d93b --- /dev/null +++ b/simple/testdata/src/compare/dummy.go @@ -0,0 +1,3 @@ +// This file exists just to make sure the package is still defined +// when tests DO NOT run on darwin/linux +package pkg diff --git a/simple/testdata/src/compare_windows/compare-windows.go b/simple/testdata/src/compare_windows/compare-windows.go new file mode 100644 index 000000000..4a2907c7f --- /dev/null +++ b/simple/testdata/src/compare_windows/compare-windows.go @@ -0,0 +1,12 @@ +// +build !linux,!darwin + +package pkg + +import "bytes" + +func fn() { + _ = bytes.Compare(nil, nil) == 0 // want ` bytes.Equal` + _ = bytes.Compare(nil, nil) != 0 // want `!bytes.Equal` + _ = bytes.Compare(nil, nil) > 0 + _ = bytes.Compare(nil, nil) < 0 +} diff --git a/simple/testdata/src/compare_windows/dummy.go b/simple/testdata/src/compare_windows/dummy.go new file mode 100644 index 000000000..08afc55a4 --- /dev/null +++ b/simple/testdata/src/compare_windows/dummy.go @@ -0,0 +1,3 @@ +// This file exists just to make sure the package is still defined +// when tests run on darwin/linux +package pkg