From 680d64e191e747d67d6add8dd341ebbdb2c15f33 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 10 Mar 2021 11:33:01 +0100 Subject: [PATCH] revive: the default configuration is only applied when no dedicated configuration. --- pkg/golinters/revive.go | 36 +++++++++++++++++++++++++++++++- test/testdata/configs/revive.yml | 2 -- test/testdata/revive.go | 21 +++++++++++++++++-- test/testdata/revive_default.go | 29 +++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 test/testdata/revive_default.go diff --git a/pkg/golinters/revive.go b/pkg/golinters/revive.go index 0420731fd395..e2451e1f8fbb 100644 --- a/pkg/golinters/revive.go +++ b/pkg/golinters/revive.go @@ -6,6 +6,7 @@ import ( "fmt" "go/token" "io/ioutil" + "reflect" "github.com/BurntSushi/toml" "github.com/mgechev/dots" @@ -135,7 +136,7 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter { // This allow to get default values and right types. // https://github.com/golangci/golangci-lint/issues/1745 // https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L155 -func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) { +func getReviveConfigOLD(cfg *config.ReviveSettings) (*lint.Config, error) { rawRoot := createConfigMap(cfg) buf := bytes.NewBuffer(nil) @@ -162,6 +163,39 @@ func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) { return conf, nil } +// This function mimics the GetConfig function of revive. +// This allow to get default values and right types. +// https://github.com/golangci/golangci-lint/issues/1745 +// https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L155 +func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) { + conf := defaultConfig() + + if !reflect.DeepEqual(cfg, &config.ReviveSettings{}) { + rawRoot := createConfigMap(cfg) + buf := bytes.NewBuffer(nil) + + err := toml.NewEncoder(buf).Encode(rawRoot) + if err != nil { + return nil, err + } + + conf = &lint.Config{} + _, err = toml.DecodeReader(buf, conf) + if err != nil { + return nil, err + } + } + + normalizeConfig(conf) + + // By default golangci-lint ignores missing doc comments, follow same convention by removing this default rule + // Relevant issue: https://github.com/golangci/golangci-lint/issues/456 + delete(conf.Rules, "package-comments") + delete(conf.Rules, "exported") + + return conf, nil +} + func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} { rawRoot := map[string]interface{}{ "ignoreGeneratedHeader": cfg.IgnoreGeneratedHeader, diff --git a/test/testdata/configs/revive.yml b/test/testdata/configs/revive.yml index 84ee90e9fe4e..1bf311d6bf9f 100644 --- a/test/testdata/configs/revive.yml +++ b/test/testdata/configs/revive.yml @@ -3,8 +3,6 @@ linters-settings: ignore-generated-header: true severity: warning rules: - - name: indent-error-flow - severity: warning - name: cognitive-complexity arguments: [ 7 ] - name: line-length-limit diff --git a/test/testdata/revive.go b/test/testdata/revive.go index 83e3d18d3c7e..ce6a396e7b14 100644 --- a/test/testdata/revive.go +++ b/test/testdata/revive.go @@ -2,12 +2,29 @@ //config_path: testdata/configs/revive.yml package testdata -import "time" +import ( + "net/http" + "time" +) func testRevive(t *time.Duration) error { if t == nil { return nil - } else { // ERROR "indent-error-flow: if block ends with a return statement, .*" + } else { return nil } } + +func testReviveComplexity(s string) { // ERROR "cyclomatic: function testReviveComplexity has cyclomatic complexity 22" + if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" { + return + } + + if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" { + return + } + + if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" { + return + } +} diff --git a/test/testdata/revive_default.go b/test/testdata/revive_default.go new file mode 100644 index 000000000000..dff44f881a77 --- /dev/null +++ b/test/testdata/revive_default.go @@ -0,0 +1,29 @@ +//args: -Erevive +package testdata + +import ( + "net/http" + "time" +) + +func testReviveDefault(t *time.Duration) error { + if t == nil { + return nil + } else { // ERROR "indent-error-flow: if block ends with a return statement, .*" + return nil + } +} + +func testReviveComplexityDefault(s string) { + if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" { + return + } + + if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" { + return + } + + if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" { + return + } +}