Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linter asasalint to lint pass []any as any #2968

Merged
merged 10 commits into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -107,6 +107,22 @@ output:

# All available settings of specific linters.
linters-settings:
asasalint:
# To specify a set of function names to exclude.
# The values are merged with the builtin exclusions.
# The builtin exclusions can be disabled by setting `use-builtin-exclusions` to `false`.
# Default: ["^(fmt|log|logger)\.(Print|Fprint|Sprint|Fatal|Panic|Error|Warn|Warning|Info|Debug)(|f|ln)$"]
exclude:
- Append
- \.Wrapf
# To enable/disable the asasalint builtin exclusions of function names.
# See the default value of `exclude` to get the builtin exclusions.
# Default: true
use-builtin-exclusions: false
# Ignore *_test.go files.
# Default: false
ignore-test: true

bidichk:
# The following configurations check for all mentioned invisible unicode runes.
# All runes are enabled by default.
Expand Down Expand Up @@ -1823,6 +1839,7 @@ linters:
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default-linters
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
Expand Down Expand Up @@ -1922,6 +1939,7 @@ linters:
# Disable specific linter
# https://golangci-lint.run/usage/linters/#disabled-by-default-linters--e--enable
disable:
- asasalint
- asciicheck
- bidichk
- bodyclose
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -11,6 +11,7 @@ require (
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.2.0
github.com/OpenPeeDeeP/depguard v1.1.0
github.com/alexkohler/prealloc v1.0.0
github.com/alingse/asasalint v0.0.10
github.com/ashanbrown/forbidigo v1.3.0
github.com/ashanbrown/makezero v1.1.1
github.com/bkielbasa/cyclop v1.2.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.

10 changes: 10 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -7,6 +7,9 @@ import (
)

var defaultLintersSettings = LintersSettings{
Asasalint: AsasalintSettings{
UseBuiltinExclusions: true,
},
Decorder: DecorderSettings{
DecOrder: []string{"type", "const", "var", "func"},
DisableDecNumCheck: true,
Expand Down Expand Up @@ -113,6 +116,7 @@ var defaultLintersSettings = LintersSettings{
}

type LintersSettings struct {
Asasalint AsasalintSettings
BiDiChk BiDiChkSettings
Cyclop Cyclop
Decorder DecorderSettings
Expand Down Expand Up @@ -184,6 +188,12 @@ type LintersSettings struct {
Custom map[string]CustomLinterSettings
}

type AsasalintSettings struct {
Exclude []string `mapstructure:"exclude"`
UseBuiltinExclusions bool `mapstructure:"use-builtin-exclusions"`
IgnoreTest bool `mapstructure:"ignore-test"`
}

type BiDiChkSettings struct {
LeftToRightEmbedding bool `mapstructure:"left-to-right-embedding"`
RightToLeftEmbedding bool `mapstructure:"right-to-left-embedding"`
Expand Down
30 changes: 30 additions & 0 deletions pkg/golinters/asasalint.go
@@ -0,0 +1,30 @@
package golinters

import (
"github.com/alingse/asasalint"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewAsasalint(setting *config.AsasalintSettings) *goanalysis.Linter {
cfg := asasalint.LinterSetting{}
if setting != nil {
cfg.Exclude = setting.Exclude
cfg.NoBuiltinExclusions = !setting.UseBuiltinExclusions
cfg.IgnoreTest = setting.IgnoreTest
}

a, err := asasalint.NewAnalyzer(cfg)
if err != nil {
linterLogger.Fatalf("asasalint: create analyzer: %v", err)
}
alingse marked this conversation as resolved.
Show resolved Hide resolved

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
8 changes: 8 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -101,6 +101,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config)
//nolint:funlen
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var (
asasalintCfg *config.AsasalintSettings
bidichkCfg *config.BiDiChkSettings
cyclopCfg *config.Cyclop
decorderCfg *config.DecorderSettings
Expand Down Expand Up @@ -171,6 +172,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
)

if m.cfg != nil {
asasalintCfg = &m.cfg.LintersSettings.Asasalint
bidichkCfg = &m.cfg.LintersSettings.BiDiChk
cyclopCfg = &m.cfg.LintersSettings.Cyclop
decorderCfg = &m.cfg.LintersSettings.Decorder
Expand Down Expand Up @@ -266,6 +268,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
// The linters are sorted in the alphabetical order (case-insensitive).
// When a new linter is added the version in `WithSince(...)` must be the next minor version of golangci-lint.
lcs := []*linter.Config{
linter.NewConfig(golinters.NewAsasalint(asasalintCfg)).
WithSince("1.47.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/alingse/asasalint"),

linter.NewConfig(golinters.NewAsciicheck()).
WithSince("v1.26.0").
WithPresets(linter.PresetBugs, linter.PresetStyle).
Expand Down
20 changes: 20 additions & 0 deletions test/testdata/asasalint.go
@@ -0,0 +1,20 @@
//golangcitest:args -Easasalint
package testdata
ldez marked this conversation as resolved.
Show resolved Hide resolved

import "fmt"

func getArgsLength(args ...interface{}) int {
// this line will not report as error
fmt.Println(args)
return len(args)
}

func checkArgsLength(args ...interface{}) int {
return getArgsLength(args) // ERROR `pass \[\]any as any to func getArgsLength func\(args \.\.\.interface\{\}\)`
}

func someCall() {
var a = []interface{}{1, 2, 3}
fmt.Println(checkArgsLength(a...) == getArgsLength(a)) // ERROR `pass \[\]any as any to func getArgsLength func\(args \.\.\.interface\{\}\)`
fmt.Println(checkArgsLength(a...) == getArgsLength(a...))
}