diff --git a/.golangci.yml b/.golangci.yml index 6a349af..bc79b83 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -60,6 +60,7 @@ linters: - prealloc - rowserrcheck - testpackage + - tparallel - varnamelen - wastedassign diff --git a/analyzer.go b/analyzer.go index 9de53c3..e51df89 100644 --- a/analyzer.go +++ b/analyzer.go @@ -79,12 +79,17 @@ func (wa *wslAnalyzer) flags() flag.FlagSet { func (wa *wslAnalyzer) run(pass *analysis.Pass) (interface{}, error) { for _, file := range pass.Files { - if !wa.config.IncludeGenerated && ast.IsGenerated(file) { + filename := getFilename(pass.Fset, file) + if !strings.HasSuffix(filename, ".go") { continue } - filename := getFilename(pass.Fset, file) - if !strings.HasSuffix(filename, ".go") { + // if the file is related to cgo the filename of the unadjusted position is a not a '.go' file. + fn := pass.Fset.PositionFor(file.Pos(), false).Filename + + // The file is skipped if the "unadjusted" file is a Go file, and it's a generated file (ex: "_test.go" file). + // The other non-Go files are skipped by the first 'if' with the adjusted position. + if !wa.config.IncludeGenerated && ast.IsGenerated(file) && strings.HasSuffix(fn, ".go") { continue } diff --git a/testdata/src/default_config/cgo.go.golden b/testdata/src/default_config/cgo.go.golden deleted file mode 100644 index 5789e81..0000000 --- a/testdata/src/default_config/cgo.go.golden +++ /dev/null @@ -1,46 +0,0 @@ -package testpkg - -/* - #include - #include - - void myprint(char* s) { - printf("%d\n", s); - } -*/ -import "C" - -import ( - "unsafe" -) - -func _() { - cs := C.CString("Hello from stdio\n") - C.myprint(cs) - C.free(unsafe.Pointer(cs)) -} - -func Advanced() { - var foo = 1 - - var bar = 2 // want "declarations should never be cuddled" - - var biz int // want "declarations should never be cuddled" - - x := []string{} - x = append(x, "literal") - notUsed := "just assigning, don't mind me" - - x = append(x, "not z..") // want "append only allowed to cuddle with appended value" - useMe := "right away" - alsoNotUsed := ":(" - - x = append(x, "just noise") // want "append only allowed to cuddle with appended value" - x = append(x, useMe) - - _ = foo - _ = bar - _ = biz - _ = notUsed - _ = alsoNotUsed -} diff --git a/testdata/src/default_config/cgo.go b/testdata/src/default_config/cgo/cgo.go similarity index 100% rename from testdata/src/default_config/cgo.go rename to testdata/src/default_config/cgo/cgo.go diff --git a/testdata/src/default_config/line_directive.go b/testdata/src/default_config/line_directive/line_directive.go similarity index 100% rename from testdata/src/default_config/line_directive.go rename to testdata/src/default_config/line_directive/line_directive.go diff --git a/testdata/src/default_config/line_directive.go.golden b/testdata/src/default_config/line_directive/line_directive.go.golden similarity index 100% rename from testdata/src/default_config/line_directive.go.golden rename to testdata/src/default_config/line_directive/line_directive.go.golden diff --git a/testdata/src/default_config/line_directive.tmpl b/testdata/src/default_config/line_directive/line_directive.tmpl similarity index 100% rename from testdata/src/default_config/line_directive.tmpl rename to testdata/src/default_config/line_directive/line_directive.tmpl diff --git a/wsl_test.go b/wsl_test.go index b845657..0070d6f 100644 --- a/wsl_test.go +++ b/wsl_test.go @@ -15,8 +15,6 @@ func TestWIP(t *testing.T) { } func TestDefaultConfig(t *testing.T) { - t.Parallel() - testdata := analysistest.TestData() testCases := []struct { @@ -32,6 +30,7 @@ func TestDefaultConfig(t *testing.T) { {dir: "generic_handling"}, {dir: "multiline_case"}, {dir: "remove_whitespace"}, + {dir: "line_directive"}, } for _, test := range testCases { @@ -44,6 +43,13 @@ func TestDefaultConfig(t *testing.T) { } } +func TestCGo(t *testing.T) { + testdata := analysistest.TestData() + + analyzer := NewAnalyzer(nil) + analysistest.Run(t, testdata, analyzer, filepath.Join("default_config", "cgo")) +} + func TestWithConfig(t *testing.T) { testdata := analysistest.TestData()