Skip to content

Commit b415402

Browse files
authored
Add linter asasalint to lint pass []any as any (#2968)
1 parent e60937a commit b415402

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

Diff for: .golangci.reference.yml

+18
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ output:
107107

108108
# All available settings of specific linters.
109109
linters-settings:
110+
asasalint:
111+
# To specify a set of function names to exclude.
112+
# The values are merged with the builtin exclusions.
113+
# The builtin exclusions can be disabled by setting `use-builtin-exclusions` to `false`.
114+
# Default: ["^(fmt|log|logger)\.(Print|Fprint|Sprint|Fatal|Panic|Error|Warn|Warning|Info|Debug)(|f|ln)$"]
115+
exclude:
116+
- Append
117+
- \.Wrapf
118+
# To enable/disable the asasalint builtin exclusions of function names.
119+
# See the default value of `exclude` to get the builtin exclusions.
120+
# Default: true
121+
use-builtin-exclusions: false
122+
# Ignore *_test.go files.
123+
# Default: false
124+
ignore-test: true
125+
110126
bidichk:
111127
# The following configurations check for all mentioned invisible unicode runes.
112128
# All runes are enabled by default.
@@ -1823,6 +1839,7 @@ linters:
18231839
# Enable specific linter
18241840
# https://golangci-lint.run/usage/linters/#enabled-by-default-linters
18251841
enable:
1842+
- asasalint
18261843
- asciicheck
18271844
- bidichk
18281845
- bodyclose
@@ -1922,6 +1939,7 @@ linters:
19221939
# Disable specific linter
19231940
# https://golangci-lint.run/usage/linters/#disabled-by-default-linters--e--enable
19241941
disable:
1942+
- asasalint
19251943
- asciicheck
19261944
- bidichk
19271945
- bodyclose

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.2.0
1212
github.com/OpenPeeDeeP/depguard v1.1.0
1313
github.com/alexkohler/prealloc v1.0.0
14+
github.com/alingse/asasalint v0.0.10
1415
github.com/ashanbrown/forbidigo v1.3.0
1516
github.com/ashanbrown/makezero v1.1.1
1617
github.com/bkielbasa/cyclop v1.2.0

Diff for: go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/config/linters_settings.go

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
)
88

99
var defaultLintersSettings = LintersSettings{
10+
Asasalint: AsasalintSettings{
11+
UseBuiltinExclusions: true,
12+
},
1013
Decorder: DecorderSettings{
1114
DecOrder: []string{"type", "const", "var", "func"},
1215
DisableDecNumCheck: true,
@@ -113,6 +116,7 @@ var defaultLintersSettings = LintersSettings{
113116
}
114117

115118
type LintersSettings struct {
119+
Asasalint AsasalintSettings
116120
BiDiChk BiDiChkSettings
117121
Cyclop Cyclop
118122
Decorder DecorderSettings
@@ -184,6 +188,12 @@ type LintersSettings struct {
184188
Custom map[string]CustomLinterSettings
185189
}
186190

191+
type AsasalintSettings struct {
192+
Exclude []string `mapstructure:"exclude"`
193+
UseBuiltinExclusions bool `mapstructure:"use-builtin-exclusions"`
194+
IgnoreTest bool `mapstructure:"ignore-test"`
195+
}
196+
187197
type BiDiChkSettings struct {
188198
LeftToRightEmbedding bool `mapstructure:"left-to-right-embedding"`
189199
RightToLeftEmbedding bool `mapstructure:"right-to-left-embedding"`

Diff for: pkg/golinters/asasalint.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package golinters
2+
3+
import (
4+
"github.com/alingse/asasalint"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
11+
func NewAsasalint(setting *config.AsasalintSettings) *goanalysis.Linter {
12+
cfg := asasalint.LinterSetting{}
13+
if setting != nil {
14+
cfg.Exclude = setting.Exclude
15+
cfg.NoBuiltinExclusions = !setting.UseBuiltinExclusions
16+
cfg.IgnoreTest = setting.IgnoreTest
17+
}
18+
19+
a, err := asasalint.NewAnalyzer(cfg)
20+
if err != nil {
21+
linterLogger.Fatalf("asasalint: create analyzer: %v", err)
22+
}
23+
24+
return goanalysis.NewLinter(
25+
a.Name,
26+
a.Doc,
27+
[]*analysis.Analyzer{a},
28+
nil,
29+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
30+
}

Diff for: pkg/lint/lintersdb/manager.go

+8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config)
101101
//nolint:funlen
102102
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
103103
var (
104+
asasalintCfg *config.AsasalintSettings
104105
bidichkCfg *config.BiDiChkSettings
105106
cyclopCfg *config.Cyclop
106107
decorderCfg *config.DecorderSettings
@@ -171,6 +172,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
171172
)
172173

173174
if m.cfg != nil {
175+
asasalintCfg = &m.cfg.LintersSettings.Asasalint
174176
bidichkCfg = &m.cfg.LintersSettings.BiDiChk
175177
cyclopCfg = &m.cfg.LintersSettings.Cyclop
176178
decorderCfg = &m.cfg.LintersSettings.Decorder
@@ -266,6 +268,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
266268
// The linters are sorted in the alphabetical order (case-insensitive).
267269
// When a new linter is added the version in `WithSince(...)` must be the next minor version of golangci-lint.
268270
lcs := []*linter.Config{
271+
linter.NewConfig(golinters.NewAsasalint(asasalintCfg)).
272+
WithSince("1.47.0").
273+
WithPresets(linter.PresetBugs).
274+
WithLoadForGoAnalysis().
275+
WithURL("https://github.com/alingse/asasalint"),
276+
269277
linter.NewConfig(golinters.NewAsciicheck()).
270278
WithSince("v1.26.0").
271279
WithPresets(linter.PresetBugs, linter.PresetStyle).

Diff for: test/testdata/asasalint.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//golangcitest:args -Easasalint
2+
package testdata
3+
4+
import "fmt"
5+
6+
func getArgsLength(args ...interface{}) int {
7+
// this line will not report as error
8+
fmt.Println(args)
9+
return len(args)
10+
}
11+
12+
func checkArgsLength(args ...interface{}) int {
13+
return getArgsLength(args) // ERROR `pass \[\]any as any to func getArgsLength func\(args \.\.\.interface\{\}\)`
14+
}
15+
16+
func someCall() {
17+
var a = []interface{}{1, 2, 3}
18+
fmt.Println(checkArgsLength(a...) == getArgsLength(a)) // ERROR `pass \[\]any as any to func getArgsLength func\(args \.\.\.interface\{\}\)`
19+
fmt.Println(checkArgsLength(a...) == getArgsLength(a...))
20+
}

0 commit comments

Comments
 (0)