Skip to content

Commit cf8fd68

Browse files
authored
revive: fix add-constant rule support. (#2003)
1 parent b73972f commit cf8fd68

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

.golangci.example.yml

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

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

pkg/commands/executor.go

+2-2
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

+31-3
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ import (
1313
reviveConfig "github.com/mgechev/revive/config"
1414
"github.com/mgechev/revive/lint"
1515
"github.com/mgechev/revive/rule"
16+
"github.com/pkg/errors"
1617
"golang.org/x/tools/go/analysis"
1718

1819
"github.com/golangci/golangci-lint/pkg/config"
1920
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
2021
"github.com/golangci/golangci-lint/pkg/lint/linter"
22+
"github.com/golangci/golangci-lint/pkg/logutils"
2123
"github.com/golangci/golangci-lint/pkg/result"
2224
)
2325

2426
const reviveName = "revive"
2527

28+
var reviveDebugf = logutils.Debug("revive")
29+
2630
// jsonObject defines a JSON object of an failure
2731
type jsonObject struct {
2832
Severity lint.Severity
@@ -145,18 +149,20 @@ func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
145149

146150
err := toml.NewEncoder(buf).Encode(rawRoot)
147151
if err != nil {
148-
return nil, err
152+
return nil, errors.Wrap(err, "failed to encode configuration")
149153
}
150154

151155
conf = &lint.Config{}
152156
_, err = toml.DecodeReader(buf, conf)
153157
if err != nil {
154-
return nil, err
158+
return nil, errors.Wrap(err, "failed to decode configuration")
155159
}
156160
}
157161

158162
normalizeConfig(conf)
159163

164+
reviveDebugf("revive configuration: %#v", conf)
165+
160166
return conf, nil
161167
}
162168

@@ -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)