diff --git a/.golangci.example.yml b/.golangci.example.yml index fd2f2cc6e8d1..4ed39ebbe2e0 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -496,7 +496,7 @@ linters-settings: rowserrcheck: packages: - github.com/jmoiron/sqlx - - + revive: # see https://github.com/mgechev/revive#available-rules for details. ignore-generated-header: true @@ -504,6 +504,13 @@ linters-settings: rules: - name: indent-error-flow severity: warning + - name: add-constant + severity: warning + arguments: + - maxLitCount: "3" + allowStrs: '""' + allowInts: "0,1,2" + allowFloats: "0.0,0.,1.0,1.,2.0,2." staticcheck: # Select the Go version to target. The default is '1.13'. diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 7f4702ed6adc..a060709ef713 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "crypto/sha256" - "encoding/json" "io" "os" "path/filepath" @@ -16,6 +15,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" + "gopkg.in/yaml.v3" "github.com/golangci/golangci-lint/internal/cache" "github.com/golangci/golangci-lint/internal/pkgcache" @@ -194,7 +194,7 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) { // We don't hash all config fields to reduce meaningless cache // invalidations. At least, it has a huge impact on tests speed. - lintersSettingsBytes, err := json.Marshal(cfg.LintersSettings) + lintersSettingsBytes, err := yaml.Marshal(cfg.LintersSettings) if err != nil { return nil, errors.Wrap(err, "failed to json marshal config linter settings") } diff --git a/pkg/golinters/revive.go b/pkg/golinters/revive.go index c3a135adae9f..7119a472a180 100644 --- a/pkg/golinters/revive.go +++ b/pkg/golinters/revive.go @@ -13,16 +13,20 @@ import ( reviveConfig "github.com/mgechev/revive/config" "github.com/mgechev/revive/lint" "github.com/mgechev/revive/rule" + "github.com/pkg/errors" "golang.org/x/tools/go/analysis" "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/lint/linter" + "github.com/golangci/golangci-lint/pkg/logutils" "github.com/golangci/golangci-lint/pkg/result" ) const reviveName = "revive" +var reviveDebugf = logutils.Debug("revive") + // jsonObject defines a JSON object of an failure type jsonObject struct { Severity lint.Severity @@ -145,18 +149,20 @@ func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) { err := toml.NewEncoder(buf).Encode(rawRoot) if err != nil { - return nil, err + return nil, errors.Wrap(err, "failed to encode configuration") } conf = &lint.Config{} _, err = toml.DecodeReader(buf, conf) if err != nil { - return nil, err + return nil, errors.Wrap(err, "failed to decode configuration") } } normalizeConfig(conf) + reviveDebugf("revive configuration: %#v", conf) + return conf, nil } @@ -184,7 +190,7 @@ func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} { for _, s := range cfg.Rules { rawRules[s.Name] = map[string]interface{}{ "severity": s.Severity, - "arguments": s.Arguments, + "arguments": safeTomlSlice(s.Arguments), } } @@ -195,6 +201,28 @@ func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} { return rawRoot } +func safeTomlSlice(r []interface{}) []interface{} { + if len(r) == 0 { + return nil + } + + if _, ok := r[0].(map[interface{}]interface{}); !ok { + return r + } + + var typed []interface{} + for _, elt := range r { + item := map[string]interface{}{} + for k, v := range elt.(map[interface{}]interface{}) { + item[k.(string)] = v + } + + typed = append(typed, item) + } + + return typed +} + // This element is not exported by revive, so we need copy the code. // Extracted from https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L15 var defaultRules = []lint.Rule{