Skip to content

Commit d5e6be2

Browse files
committed
revive: fix add-constant rule support.
1 parent fcef824 commit d5e6be2

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

.golangci.example.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,21 @@ linters-settings:
496496
rowserrcheck:
497497
packages:
498498
- github.com/jmoiron/sqlx
499-
-
499+
500500
revive:
501501
# see https://github.com/mgechev/revive#available-rules for details.
502502
ignore-generated-header: true
503503
severity: warning
504504
rules:
505505
- name: indent-error-flow
506506
severity: warning
507+
- name: add-constant
508+
severity: warning
509+
arguments:
510+
- maxLitCount: "3"
511+
allowStrs: '""'
512+
allowInts: "0,1,2"
513+
allowFloats: "0.0,0.,1.0,1.,2.0,2."
507514

508515
staticcheck:
509516
# Select the Go version to target. The default is '1.13'.

pkg/commands/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"crypto/sha256"
7-
"encoding/json"
87
"io"
98
"os"
109
"path/filepath"
@@ -16,6 +15,7 @@ import (
1615
"github.com/pkg/errors"
1716
"github.com/spf13/cobra"
1817
"github.com/spf13/pflag"
18+
"gopkg.in/yaml.v3"
1919

2020
"github.com/golangci/golangci-lint/internal/cache"
2121
"github.com/golangci/golangci-lint/internal/pkgcache"
@@ -194,7 +194,7 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {
194194
// We don't hash all config fields to reduce meaningless cache
195195
// invalidations. At least, it has a huge impact on tests speed.
196196

197-
lintersSettingsBytes, err := json.Marshal(cfg.LintersSettings)
197+
lintersSettingsBytes, err := yaml.Marshal(cfg.LintersSettings)
198198
if err != nil {
199199
return nil, errors.Wrap(err, "failed to json marshal config linter settings")
200200
}

pkg/golinters/revive.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"reflect"
1010

1111
"github.com/BurntSushi/toml"
12+
"github.com/golangci/golangci-lint/pkg/logutils"
1213
"github.com/mgechev/dots"
1314
reviveConfig "github.com/mgechev/revive/config"
1415
"github.com/mgechev/revive/lint"
1516
"github.com/mgechev/revive/rule"
17+
"github.com/pkg/errors"
1618
"golang.org/x/tools/go/analysis"
1719

1820
"github.com/golangci/golangci-lint/pkg/config"
@@ -31,6 +33,8 @@ type jsonObject struct {
3133

3234
// NewRevive returns a new Revive linter.
3335
func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter {
36+
reviveDebugf := logutils.Debug("revive")
37+
3438
var issues []goanalysis.Issue
3539

3640
analyzer := &analysis.Analyzer{
@@ -56,6 +60,8 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter {
5660
return nil, err
5761
}
5862

63+
reviveDebugf("revive configuration: %#v", conf)
64+
5965
formatter, err := reviveConfig.GetFormatter("json")
6066
if err != nil {
6167
return nil, err
@@ -145,13 +151,13 @@ func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
145151

146152
err := toml.NewEncoder(buf).Encode(rawRoot)
147153
if err != nil {
148-
return nil, err
154+
return nil, errors.Wrap(err, "failed to encode configuration")
149155
}
150156

151157
conf = &lint.Config{}
152158
_, err = toml.DecodeReader(buf, conf)
153159
if err != nil {
154-
return nil, err
160+
return nil, errors.Wrap(err, "failed to decode configuration")
155161
}
156162
}
157163

@@ -184,7 +190,7 @@ func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} {
184190
for _, s := range cfg.Rules {
185191
rawRules[s.Name] = map[string]interface{}{
186192
"severity": s.Severity,
187-
"arguments": s.Arguments,
193+
"arguments": safeTomlSlice(s.Arguments),
188194
}
189195
}
190196

@@ -195,6 +201,28 @@ func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} {
195201
return rawRoot
196202
}
197203

204+
func safeTomlSlice(r []interface{}) []interface{} {
205+
if len(r) == 0 {
206+
return nil
207+
}
208+
209+
if _, ok := r[0].(map[interface{}]interface{}); !ok {
210+
return r
211+
}
212+
213+
var typed []interface{}
214+
for _, elt := range r {
215+
item := map[string]interface{}{}
216+
for k, v := range elt.(map[interface{}]interface{}) {
217+
item[k.(string)] = v
218+
}
219+
220+
typed = append(typed, item)
221+
}
222+
223+
return typed
224+
}
225+
198226
// This element is not exported by revive, so we need copy the code.
199227
// Extracted from https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L15
200228
var defaultRules = []lint.Rule{

0 commit comments

Comments
 (0)