Skip to content

Commit cd6644d

Browse files
authored
revive: the default configuration is only applied when no dedicated configuration. (#1831)
1 parent e381b33 commit cd6644d

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

pkg/golinters/revive.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"go/token"
88
"io/ioutil"
9+
"reflect"
910

1011
"github.com/BurntSushi/toml"
1112
"github.com/mgechev/dots"
@@ -136,20 +137,22 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter {
136137
// https://github.com/golangci/golangci-lint/issues/1745
137138
// https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L155
138139
func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
139-
rawRoot := createConfigMap(cfg)
140-
141-
buf := bytes.NewBuffer(nil)
140+
conf := defaultConfig()
142141

143-
err := toml.NewEncoder(buf).Encode(rawRoot)
144-
if err != nil {
145-
return nil, err
146-
}
142+
if !reflect.DeepEqual(cfg, &config.ReviveSettings{}) {
143+
rawRoot := createConfigMap(cfg)
144+
buf := bytes.NewBuffer(nil)
147145

148-
conf := defaultConfig()
146+
err := toml.NewEncoder(buf).Encode(rawRoot)
147+
if err != nil {
148+
return nil, err
149+
}
149150

150-
_, err = toml.DecodeReader(buf, conf)
151-
if err != nil {
152-
return nil, err
151+
conf = &lint.Config{}
152+
_, err = toml.DecodeReader(buf, conf)
153+
if err != nil {
154+
return nil, err
155+
}
153156
}
154157

155158
normalizeConfig(conf)

test/testdata/configs/revive.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ linters-settings:
33
ignore-generated-header: true
44
severity: warning
55
rules:
6-
- name: indent-error-flow
7-
severity: warning
86
- name: cognitive-complexity
97
arguments: [ 7 ]
108
- name: line-length-limit
11-
arguments: [ 110 ]
9+
arguments: [ 130 ]
1210
- name: function-result-limit
1311
arguments: [ 3 ]
1412
- name: argument-limit

test/testdata/revive.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,29 @@
22
//config_path: testdata/configs/revive.yml
33
package testdata
44

5-
import "time"
5+
import (
6+
"net/http"
7+
"time"
8+
)
69

710
func testRevive(t *time.Duration) error {
811
if t == nil {
912
return nil
10-
} else { // ERROR "indent-error-flow: if block ends with a return statement, .*"
13+
} else {
1114
return nil
1215
}
1316
}
17+
18+
func testReviveComplexity(s string) { // ERROR "cyclomatic: function testReviveComplexity has cyclomatic complexity 22"
19+
if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
20+
return
21+
}
22+
23+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
24+
return
25+
}
26+
27+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
28+
return
29+
}
30+
}

test/testdata/revive_default.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//args: -Erevive
2+
package testdata
3+
4+
import (
5+
"net/http"
6+
"time"
7+
)
8+
9+
func testReviveDefault(t *time.Duration) error {
10+
if t == nil {
11+
return nil
12+
} else { // ERROR "indent-error-flow: if block ends with a return statement, .*"
13+
return nil
14+
}
15+
}
16+
17+
func testReviveComplexityDefault(s string) {
18+
if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
19+
return
20+
}
21+
22+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
23+
return
24+
}
25+
26+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
27+
return
28+
}
29+
}

0 commit comments

Comments
 (0)