Skip to content

feat: [gocritic] support disabled-tags #1029

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

Merged
merged 1 commit into from
Apr 22, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ linters-settings:
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
enabled-tags:
- performance
disabled-tags:
- experimental

settings: # settings passed to gocritic
captLocal: # must be valid enabled check name
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ linters-settings:
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
enabled-tags:
- performance
disabled-tags:
- experimental

settings: # settings passed to gocritic
captLocal: # must be valid enabled check name
Expand Down
74 changes: 69 additions & 5 deletions pkg/config/config_gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ import (

const gocriticDebugKey = "gocritic"

var gocriticDebugf = logutils.Debug(gocriticDebugKey)
var isGocriticDebug = logutils.HaveDebugTag(gocriticDebugKey)

var allGocriticCheckers = lintpack.GetCheckersInfo()
var (
gocriticDebugf = logutils.Debug(gocriticDebugKey)
isGocriticDebug = logutils.HaveDebugTag(gocriticDebugKey)
allGocriticCheckers = lintpack.GetCheckersInfo()
allGocriticCheckerMap = func() map[string]*lintpack.CheckerInfo {
checkInfoMap := make(map[string]*lintpack.CheckerInfo)
for _, checkInfo := range allGocriticCheckers {
checkInfoMap[checkInfo.Name] = checkInfo
}
return checkInfoMap
}()
)

type GocriticCheckSettings map[string]interface{}

type GocriticSettings struct {
EnabledChecks []string `mapstructure:"enabled-checks"`
DisabledChecks []string `mapstructure:"disabled-checks"`
EnabledTags []string `mapstructure:"enabled-tags"`
DisabledTags []string `mapstructure:"disabled-tags"`
SettingsPerCheck map[string]GocriticCheckSettings `mapstructure:"settings"`

inferredEnabledChecks map[string]bool
Expand Down Expand Up @@ -107,6 +116,8 @@ func (s *GocriticSettings) InferEnabledChecks(log logutils.Log) {
debugChecksListf(disabledByDefaultChecks, "Disabled by default")

var enabledChecks []string

// EnabledTags
if len(s.EnabledTags) != 0 {
tagToCheckers := buildGocriticTagToCheckersMap()
for _, tag := range s.EnabledTags {
Expand All @@ -120,6 +131,12 @@ func (s *GocriticSettings) InferEnabledChecks(log logutils.Log) {
enabledChecks = append(enabledChecks, enabledByDefaultChecks...)
}

// DisabledTags
if len(s.DisabledTags) != 0 {
enabledChecks = filterByDisableTags(enabledChecks, s.DisabledTags, log)
}

// EnabledChecks
if len(s.EnabledChecks) != 0 {
debugChecksListf(s.EnabledChecks, "Enabled by config")

Expand All @@ -133,6 +150,7 @@ func (s *GocriticSettings) InferEnabledChecks(log logutils.Log) {
}
}

// DisabledChecks
if len(s.DisabledChecks) != 0 {
debugChecksListf(s.DisabledChecks, "Disabled by config")

Expand Down Expand Up @@ -174,6 +192,22 @@ func validateStringsUniq(ss []string) error {
return nil
}

func intersectStringSlice(s1, s2 []string) []string {
s1Map := make(map[string]struct{})
for _, s := range s1 {
s1Map[s] = struct{}{}
}

result := make([]string, 0)
for _, s := range s2 {
if _, exists := s1Map[s]; exists {
result = append(result, s)
}
}

return result
}

func (s *GocriticSettings) Validate(log logutils.Log) error {
if len(s.EnabledTags) == 0 {
if len(s.EnabledChecks) != 0 && len(s.DisabledChecks) != 0 {
Expand All @@ -187,7 +221,16 @@ func (s *GocriticSettings) Validate(log logutils.Log) error {
tagToCheckers := buildGocriticTagToCheckersMap()
for _, tag := range s.EnabledTags {
if _, ok := tagToCheckers[tag]; !ok {
return fmt.Errorf("gocritic tag %q doesn't exist", tag)
return fmt.Errorf("gocritic [enabled]tag %q doesn't exist", tag)
}
}
}

if len(s.DisabledTags) > 0 {
tagToCheckers := buildGocriticTagToCheckersMap()
for _, tag := range s.EnabledTags {
if _, ok := tagToCheckers[tag]; !ok {
return fmt.Errorf("gocritic [disabled]tag %q doesn't exist", tag)
}
}
}
Expand Down Expand Up @@ -301,3 +344,24 @@ func (s *GocriticSettings) GetLowercasedParams() map[string]GocriticCheckSetting
}
return ret
}

func filterByDisableTags(enabledChecks, disableTags []string, log logutils.Log) []string {
enabledChecksSet := stringsSliceToSet(enabledChecks)
for _, enabledCheck := range enabledChecks {
checkInfo, checkInfoExists := allGocriticCheckerMap[enabledCheck]
if !checkInfoExists {
log.Warnf("Gocritic check %q was not exists via filtering disabled tags", enabledCheck)
continue
}
hitTags := intersectStringSlice(checkInfo.Tags, disableTags)
if len(hitTags) != 0 {
delete(enabledChecksSet, enabledCheck)
}
debugChecksListf(enabledChecks, "Disabled by config tags %s", sprintStrings(disableTags))
}
enabledChecks = nil
for enabledCheck := range enabledChecksSet {
enabledChecks = append(enabledChecks, enabledCheck)
}
return enabledChecks
}
54 changes: 54 additions & 0 deletions pkg/config/config_gocritic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config

import (
"fmt"
"sort"
"testing"

"github.com/golangci/golangci-lint/pkg/logutils"

"github.com/stretchr/testify/assert"
)

func TestUtils(t *testing.T) {
s1 := []string{"diagnostic", "experimental", "opinionated"}
s2 := []string{"opinionated", "experimental"}
s3 := intersectStringSlice(s1, s2)
sort.Strings(s3)
assert.Equal(t, s3, []string{"experimental", "opinionated"})
}

type tLog struct{}

func (l *tLog) Fatalf(format string, args ...interface{}) {
fmt.Printf(fmt.Sprintf(format, args...) + "\n")
}

func (l *tLog) Panicf(format string, args ...interface{}) {
fmt.Printf(fmt.Sprintf(format, args...) + "\n")
}

func (l *tLog) Errorf(format string, args ...interface{}) {
fmt.Printf(fmt.Sprintf(format, args...) + "\n")
}

func (l *tLog) Warnf(format string, args ...interface{}) {
fmt.Printf(fmt.Sprintf(format, args...) + "\n")
}

func (l *tLog) Infof(format string, args ...interface{}) {
fmt.Printf(fmt.Sprintf(format, args...) + "\n")
}

func (l *tLog) Child(name string) logutils.Log { return nil }

func (l *tLog) SetLevel(level logutils.LogLevel) {}

func TestFilterByDisableTags(t *testing.T) {
testLog := &tLog{}
disabledTags := []string{"experimental", "opinionated"}
enabledChecks := []string{"appendAssign", "argOrder", "caseOrder", "codegenComment"}
filterEnabledChecks := filterByDisableTags(enabledChecks, disabledTags, testLog)
sort.Strings(filterEnabledChecks)
assert.Equal(t, []string{"appendAssign", "caseOrder"}, filterEnabledChecks)
}