Skip to content

Commit 20c72ce

Browse files
committed
stylecheck: emit one problem per set of duplicate imports
1 parent 10f9868 commit 20c72ce

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

stylecheck/lint.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"honnef.co/go/tools/config"
1515
"honnef.co/go/tools/internal/passes/buildssa"
16+
"honnef.co/go/tools/lint"
1617
. "honnef.co/go/tools/lint/lintdsl"
1718
"honnef.co/go/tools/ssa"
1819

@@ -98,9 +99,12 @@ func CheckDuplicatedImports(pass *analysis.Pass) (interface{}, error) {
9899
}
99100
// If there's more than one import per path, we flag that
100101
if len(value) > 1 {
101-
for _, imp := range value {
102-
ReportNodefFG(pass, imp, "should not import the same package multiple times")
102+
s := fmt.Sprintf("duplicate import %s", path)
103+
for _, imp := range value[1:] {
104+
pos := lint.DisplayPosition(pass.Fset, imp.Pos())
105+
s += "\n\t" + "also imported at " + pos.String()
103106
}
107+
ReportNodefFG(pass, value[0], s)
104108
}
105109
}
106110
}

stylecheck/testdata/src/CheckDuplicatedImports/CheckDuplicatedImports.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
package pkg
33

44
import (
5-
"fmt" // want `should not import the same package multiple times`
6-
fmt1 "fmt" // want `should not import the same package multiple times`
7-
fmt2 "fmt" // want `should not import the same package multiple times`
5+
"fmt" // want `duplicate import "fmt"`
6+
fmt1 "fmt"
7+
fmt2 "fmt"
88

99
fine "net/http"
1010

11-
"os" // want `should not import the same package multiple times`
12-
os1 "os" // want `should not import the same package multiple times`
11+
"os" // want `duplicate import "os"`
12+
os1 "os"
1313

1414
"C"
1515
_ "unsafe"

0 commit comments

Comments
 (0)