Skip to content

Commit ab39c45

Browse files
committed
unexportedglobal: Linter to require '_' prefix on globals
This adds a unexportedglobal to golangci-lint. [unexportedglobal][1] is a linter that requires that unexported global variables and constants are prefixed with '_'. [1]: https://github.com/abhinav/unexportedglobal ```go package foo // Bad var pool = sync.Pool{ /* ... */ } // Good var _pool = sync.Pool{ /* ... */ } ``` The idea is to eliminate the risk of conflict between names of local variables and names of global variables. The linter is inspired by the [Prefix Unexported Globals with `_`][2] guidance in [Uber's Go Style Guide][3]. [2]: https://github.com/uber-go/guide/blob/master/style.md#prefix-unexported-globals-with-_ [3]: https://github.com/uber-go/guide/blob/master/style.md The linter is not enabled by default. I have also not added it to the 'style' preset because not everyone may agree with this change.
1 parent 0d191c9 commit ab39c45

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

.golangci.reference.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,7 @@ linters:
21752175
- tparallel
21762176
- typecheck
21772177
- unconvert
2178+
- unexportedglobal
21782179
- unparam
21792180
- unused
21802181
- usestdlibvars
@@ -2289,6 +2290,7 @@ linters:
22892290
- tparallel
22902291
- typecheck
22912292
- unconvert
2293+
- unexportedglobal
22922294
- unparam
22932295
- unused
22942296
- usestdlibvars

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ require (
115115
github.com/yeya24/promlinter v0.2.0
116116
github.com/ykadowak/zerologlint v0.1.3
117117
gitlab.com/bosi/decorder v0.2.3
118+
go.abhg.dev/unexportedglobal v0.2.0
118119
go.tmz.dev/musttag v0.7.1
119120
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
120-
golang.org/x/tools v0.10.0
121+
golang.org/x/tools v0.11.0
121122
gopkg.in/yaml.v3 v3.0.1
122123
honnef.co/go/tools v0.4.3
123124
mvdan.cc/gofumpt v0.5.0
@@ -185,9 +186,9 @@ require (
185186
go.uber.org/multierr v1.6.0 // indirect
186187
go.uber.org/zap v1.24.0 // indirect
187188
golang.org/x/exp/typeparams v0.0.0-20230224173230-c95f2b4c22f2 // indirect
188-
golang.org/x/mod v0.11.0 // indirect
189+
golang.org/x/mod v0.12.0 // indirect
189190
golang.org/x/sync v0.3.0 // indirect
190-
golang.org/x/sys v0.9.0 // indirect
191+
golang.org/x/sys v0.10.0 // indirect
191192
golang.org/x/text v0.9.0 // indirect
192193
google.golang.org/protobuf v1.28.0 // indirect
193194
gopkg.in/ini.v1 v1.67.0 // indirect

go.sum

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/unexportedglobal.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package golinters
2+
3+
import (
4+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
5+
"go.abhg.dev/unexportedglobal"
6+
"golang.org/x/tools/go/analysis"
7+
)
8+
9+
func NewUnexportedGlobal() *goanalysis.Linter {
10+
return goanalysis.NewLinter(
11+
"unexportedglobal",
12+
"Disallows unexported globals without a '_' prefix",
13+
[]*analysis.Analyzer{unexportedglobal.Analyzer},
14+
nil,
15+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
16+
}

pkg/lint/lintersdb/manager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,13 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
817817
WithPresets(linter.PresetStyle).
818818
WithURL("https://github.com/mdempsky/unconvert"),
819819

820+
linter.NewConfig(golinters.NewUnexportedGlobal()).
821+
WithSince("v1.54.0").
822+
WithLoadForGoAnalysis().
823+
WithPresets(linter.PresetStyle).
824+
WithAutoFix().
825+
WithURL("https://github.com/abhinav/unexportedglobal"),
826+
820827
linter.NewConfig(golinters.NewUnparam(unparamCfg)).
821828
WithSince("v1.9.0").
822829
WithPresets(linter.PresetUnused).

test/testdata/unexportedglobal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//golangcitest:args -Eunexportedglobal
2+
package testdata
3+
4+
import "fmt"
5+
6+
var ExportedVar = 1 // ok
7+
8+
var unexportedVar = 1 // want `unexported global "unexportedVar" should be prefixed with '_'`
9+
10+
var _unexportedVar = 1 // ok
11+
12+
func _1() {
13+
var local = 42
14+
fmt.Println(local) // ok
15+
}

0 commit comments

Comments
 (0)