Skip to content

Commit 678ae9f

Browse files
authored
gofumpt: Add lang-version option (#2069)
1 parent 9ce20f9 commit 678ae9f

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

.golangci.example.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ linters-settings:
259259
simplify: true
260260

261261
gofumpt:
262+
# Select the Go version to target. The default is `1.15`.
263+
lang-version: "1.15"
264+
262265
# Choose whether or not to use the extra rules that are disabled
263266
# by default
264267
extra-rules: false

pkg/config/linters_settings.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ var defaultLintersSettings = LintersSettings{
5959
DefaultSignifiesExhaustive: false,
6060
},
6161
Gofumpt: GofumptSettings{
62-
ExtraRules: false,
62+
LangVersion: "",
63+
ExtraRules: false,
6364
},
6465
ErrorLint: ErrorLintSettings{
6566
Errorf: true,
@@ -232,7 +233,8 @@ type GoFmtSettings struct {
232233
}
233234

234235
type GofumptSettings struct {
235-
ExtraRules bool `mapstructure:"extra-rules"`
236+
LangVersion string `mapstructure:"lang-version"`
237+
ExtraRules bool `mapstructure:"extra-rules"`
236238
}
237239

238240
type GoHeaderSettings struct {

pkg/golinters/gofumpt.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.org/x/tools/go/analysis"
1212
"mvdan.cc/gofumpt/format"
1313

14+
"github.com/golangci/golangci-lint/pkg/config"
1415
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1516
"github.com/golangci/golangci-lint/pkg/lint/linter"
1617
)
@@ -32,6 +33,13 @@ func NewGofumpt() *goanalysis.Linter {
3233
[]*analysis.Analyzer{analyzer},
3334
nil,
3435
).WithContextSetter(func(lintCtx *linter.Context) {
36+
settings := lintCtx.Settings().Gofumpt
37+
38+
options := format.Options{
39+
LangVersion: getLangVersion(settings),
40+
ExtraRules: settings.ExtraRules,
41+
}
42+
3543
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
3644
var fileNames []string
3745
for _, f := range pass.Files {
@@ -46,12 +54,12 @@ func NewGofumpt() *goanalysis.Linter {
4654
if err != nil {
4755
return nil, fmt.Errorf("unable to open file %s: %w", f, err)
4856
}
49-
output, err := format.Source(input, format.Options{
50-
ExtraRules: lintCtx.Settings().Gofumpt.ExtraRules,
51-
})
57+
58+
output, err := format.Source(input, options)
5259
if err != nil {
5360
return nil, fmt.Errorf("error while running gofumpt: %w", err)
5461
}
62+
5563
if !bytes.Equal(input, output) {
5664
out := bytes.Buffer{}
5765
_, err = out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f))
@@ -90,3 +98,11 @@ func NewGofumpt() *goanalysis.Linter {
9098
return resIssues
9199
}).WithLoadMode(goanalysis.LoadModeSyntax)
92100
}
101+
102+
func getLangVersion(settings config.GofumptSettings) string {
103+
if settings.LangVersion == "" {
104+
// TODO: defaults to "1.15", in the future (v2) must be set by using build.Default.ReleaseTags like staticcheck.
105+
return "1.15"
106+
}
107+
return settings.LangVersion
108+
}

0 commit comments

Comments
 (0)