Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,7 @@ linters:
- tparallel
- typecheck
- unconvert
- unexportedglobal
- unparam
- unused
- usestdlibvars
Expand Down Expand Up @@ -2289,6 +2290,7 @@ linters:
- tparallel
- typecheck
- unconvert
- unexportedglobal
- unparam
- unused
- usestdlibvars
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ require (
github.com/yeya24/promlinter v0.2.0
github.com/ykadowak/zerologlint v0.1.3
gitlab.com/bosi/decorder v0.2.3
go.abhg.dev/unexportedglobal v0.2.1
go.tmz.dev/musttag v0.7.1
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
golang.org/x/tools v0.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pkg/golinters/unexportedglobal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package golinters

import (
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"go.abhg.dev/unexportedglobal"
"golang.org/x/tools/go/analysis"
)

func NewUnexportedGlobal() *goanalysis.Linter {
return goanalysis.NewLinter(
"unexportedglobal",
"Disallows unexported globals without a '_' prefix",
[]*analysis.Analyzer{unexportedglobal.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,13 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/mdempsky/unconvert"),

linter.NewConfig(golinters.NewUnexportedGlobal()).
WithSince("v1.54.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle).
WithAutoFix().
WithURL("https://github.com/abhinav/unexportedglobal"),

linter.NewConfig(golinters.NewUnparam(unparamCfg)).
WithSince("v1.9.0").
WithPresets(linter.PresetUnused).
Expand Down
15 changes: 15 additions & 0 deletions test/testdata/unexportedglobal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//golangcitest:args -Eunexportedglobal
package testdata

import "fmt"

var ExportedVar = 1 // ok

var unexportedVar = 1 // want `unexported global "unexportedVar" should be prefixed with '_'`

var _unexportedVar = 1 // ok

func _1() {
var local = 42
fmt.Println(local) // ok
}