Skip to content

Commit 41aa843

Browse files
committed
feat: drop allow-leading-space and add "all"
1 parent eddfdbb commit 41aa843

22 files changed

+67
-76
lines changed

Diff for: .golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ linters-settings:
6161
misspell:
6262
locale: US
6363
nolintlint:
64-
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
64+
allow-leading-space: false
6565
allow-unused: false # report any unused nolint directives
6666
require-explanation: false # don't require an explanation for nolint directives
6767
require-specific: false # don't require nolint directives to be specific about which linter is being skipped

Diff for: docs/src/docs/usage/false-positives.mdx

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ run:
9595

9696
## Nolint Directive
9797

98-
To exclude issues from all linters use `//nolint`.
98+
To exclude issues from all linters use `//nolint:all`.
9999
For example, if it's used inline (not from the beginning of the line) it excludes issues only for this line.
100100

101101
```go
102-
var bad_name int //nolint
102+
var bad_name int //nolint:all
103103
```
104104

105105
To exclude issues from specific linters only:
@@ -111,7 +111,7 @@ var bad_name int //nolint:golint,unused
111111
To exclude issues for the block of code use this directive on the beginning of a line:
112112

113113
```go
114-
//nolint
114+
//nolint:all
115115
func allIssuesInThisFunctionAreExcluded() *string {
116116
// ...
117117
}

Diff for: pkg/config/linters_settings.go

-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ var defaultLintersSettings = LintersSettings{
7777
},
7878
NoLintLint: NoLintLintSettings{
7979
RequireExplanation: false,
80-
AllowLeadingSpace: true,
8180
RequireSpecific: false,
8281
AllowUnused: false,
8382
},
@@ -488,7 +487,6 @@ type NlreturnSettings struct {
488487

489488
type NoLintLintSettings struct {
490489
RequireExplanation bool `mapstructure:"require-explanation"`
491-
AllowLeadingSpace bool `mapstructure:"allow-leading-space"`
492490
RequireSpecific bool `mapstructure:"require-specific"`
493491
AllowNoExplanation []string `mapstructure:"allow-no-explanation"`
494492
AllowUnused bool `mapstructure:"allow-unused"`

Diff for: pkg/golinters/godot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewGodot(settings *config.GodotSettings) *goanalysis.Linter {
3030

3131
// Convert deprecated setting
3232
// todo(butuzov): remove on v2 release
33-
if settings.CheckAll { // nolint:staticcheck // Keep for retro-compatibility.
33+
if settings.CheckAll { //nolint:staticcheck // Keep for retro-compatibility.
3434
dotSettings.Scope = godot.AllScope
3535
}
3636
}

Diff for: pkg/golinters/importas.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"strconv"
66

7-
"github.com/julz/importas" // nolint: misspell
7+
"github.com/julz/importas" //nolint:misspell
88
"golang.org/x/tools/go/analysis"
99

1010
"github.com/golangci/golangci-lint/pkg/config"
@@ -25,7 +25,7 @@ func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
2525
return
2626
}
2727
if len(settings.Alias) == 0 {
28-
lintCtx.Log.Infof("importas settings found, but no aliases listed. List aliases under alias: key.") // nolint: misspell
28+
lintCtx.Log.Infof("importas settings found, but no aliases listed. List aliases under alias: key.") //nolint:misspell
2929
}
3030

3131
if err := analyzer.Flags.Set("no-unaliased", strconv.FormatBool(settings.NoUnaliased)); err != nil {

Diff for: pkg/golinters/nolintlint.go

-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ func runNoLintLint(pass *analysis.Pass, settings *config.NoLintLintSettings) ([]
5757
if settings.RequireExplanation {
5858
needs |= nolintlint.NeedsExplanation
5959
}
60-
if !settings.AllowLeadingSpace {
61-
needs |= nolintlint.NeedsMachineOnly
62-
}
6360
if settings.RequireSpecific {
6461
needs |= nolintlint.NeedsSpecific
6562
}

Diff for: pkg/golinters/nolintlint/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# nolintlint
22

3-
nolintlint is a Go static analysis tool to find ill-formed or insufficiently explained `// nolint` directives for golangci
3+
nolintlint is a Go static analysis tool to find ill-formed or insufficiently explained `//nolint` directives for golangci
44
(or any other linter, using th )
55

66
## Purpose
77

88
To ensure that lint exceptions have explanations. Consider the case below:
99

1010
```Go
11-
import "crypto/md5" //nolint
11+
import "crypto/md5" //nolint:all
1212

1313
func hash(data []byte) []byte {
14-
return md5.New().Sum(data) //nolint
14+
return md5.New().Sum(data) //nolint:all
1515
}
1616
```
1717

@@ -27,5 +27,5 @@ func hash(data []byte) []byte {
2727
```
2828

2929
`nolintlint` can also identify cases where you may have written `// nolint`. Finally `nolintlint`, can also enforce that you
30-
use the machine-readable nolint directive format `//nolint` and that you mention what linter is being suppressed, as shown above when we write `//nolint:gosec`.
30+
use the machine-readable nolint directive format `//nolint:all` and that you mention what linter is being suppressed, as shown above when we write `//nolint:gosec`.
3131

Diff for: pkg/golinters/nolintlint/nolintlint.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func NewLinter(needs Needs, excludes []string) (*Linter, error) {
152152
}
153153

154154
return &Linter{
155-
needs: needs,
155+
needs: needs | NeedsMachineOnly,
156156
excludeByLinter: excludeByName,
157157
}, nil
158158
}
@@ -184,12 +184,14 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) {
184184
leadingSpace = leadingSpaceMatches[1]
185185
}
186186

187-
directiveWithOptionalLeadingSpace := comment.Text
187+
directiveWithOptionalLeadingSpace := "//"
188188
if len(leadingSpace) > 0 {
189-
split := strings.Split(strings.SplitN(comment.Text, ":", 2)[0], "//")
190-
directiveWithOptionalLeadingSpace = "// " + strings.TrimSpace(split[1])
189+
directiveWithOptionalLeadingSpace += " "
191190
}
192191

192+
split := strings.Split(strings.SplitN(comment.Text, ":", 2)[0], "//")
193+
directiveWithOptionalLeadingSpace += strings.TrimSpace(split[1])
194+
193195
pos := fset.Position(comment.Pos())
194196
end := fset.Position(comment.End())
195197

@@ -227,8 +229,9 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) {
227229
}
228230

229231
lintersText, explanation := fullMatches[1], fullMatches[2]
232+
230233
var linters []string
231-
if len(lintersText) > 0 {
234+
if len(lintersText) > 0 && !strings.HasPrefix(lintersText, "all") {
232235
lls := strings.Split(lintersText, ",")
233236
linters = make([]string, 0, len(lls))
234237
rangeStart := (pos.Column - 1) + len("//") + len(leadingSpace) + len("nolint:")

Diff for: pkg/golinters/nolintlint/nolintlint_test.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
//nolint:funlen
15-
func TestNoLintLint(t *testing.T) {
15+
func TestLinter_Run(t *testing.T) {
1616
type issueWithReplacement struct {
1717
issue string
1818
replacement *result.Replacement
@@ -80,21 +80,21 @@ package bar
8080
func foo() {
8181
good() //nolint:my-linter
8282
bad() //nolint
83-
bad() // nolint // because
83+
bad() //nolint // because
8484
}`,
8585
expected: []issueWithReplacement{
8686
{issue: "directive `//nolint` should mention specific linter such as `//nolint:my-linter` at testing.go:6:9"},
87-
{issue: "directive `// nolint // because` should mention specific linter such as `// nolint:my-linter` at testing.go:7:9"},
87+
{issue: "directive `//nolint // because` should mention specific linter such as `//nolint:my-linter` at testing.go:7:9"},
8888
},
8989
},
9090
{
91-
desc: "when machine-readable style isn't used",
92-
needs: NeedsMachineOnly,
91+
desc: "when machine-readable style isn't used",
9392
contents: `
9493
package bar
9594
9695
func foo() {
9796
bad() // nolint
97+
bad() // nolint
9898
good() //nolint
9999
}`,
100100
expected: []issueWithReplacement{
@@ -108,25 +108,13 @@ func foo() {
108108
},
109109
},
110110
},
111-
},
112-
},
113-
{
114-
desc: "extra spaces in front of directive are reported",
115-
contents: `
116-
package bar
117-
118-
func foo() {
119-
bad() // nolint
120-
good() // nolint
121-
}`,
122-
expected: []issueWithReplacement{
123111
{
124-
issue: "directive `// nolint` should not have more than one leading space at testing.go:5:9",
112+
issue: "directive `// nolint` should be written without leading space as `//nolint` at testing.go:6:9",
125113
replacement: &result.Replacement{
126114
Inline: &result.InlineFix{
127115
StartCol: 10,
128-
Length: 2,
129-
NewString: " ",
116+
Length: 3,
117+
NewString: "",
130118
},
131119
},
132120
},
@@ -138,13 +126,13 @@ func foo() {
138126
package bar
139127
140128
func foo() {
141-
good() // nolint:linter1,linter-two
142-
bad() // nolint:linter1 linter2
143-
good() // nolint: linter1,linter2
144-
good() // nolint: linter1, linter2
129+
good() //nolint:linter1,linter-two
130+
bad() //nolint:linter1 linter2
131+
good() //nolint: linter1,linter2
132+
good() //nolint: linter1, linter2
145133
}`,
146134
expected: []issueWithReplacement{
147-
{issue: "directive `// nolint:linter1 linter2` should match `// nolint[:<comma-separated-linters>] [// <explanation>]` at testing.go:6:9"}, //nolint:lll // this is a string
135+
{issue: "directive `//nolint:linter1 linter2` should match `//nolint[:<comma-separated-linters>] [// <explanation>]` at testing.go:6:9"}, //nolint:lll // this is a string
148136
},
149137
},
150138
{

Diff for: pkg/golinters/staticcheck_common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func staticCheckConfig(settings *config.StaticCheckSettings) *scconfig.Config {
9898
}
9999

100100
// https://github.com/dominikh/go-tools/blob/9bf17c0388a65710524ba04c2d821469e639fdc2/lintcmd/lint.go#L437-L477
101-
// nolint // Keep the original source code.
101+
//nolint:gocritic // Keep the original source code.
102102
func filterAnalyzerNames(analyzers []string, checks []string) map[string]bool {
103103
allowedChecks := map[string]bool{}
104104

Diff for: pkg/result/processors/base_rule.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (r *baseRule) matchLinter(issue *result.Issue) bool {
5858
return false
5959
}
6060

61-
func (r *baseRule) matchSource(issue *result.Issue, lineCache *fsutils.LineCache, log logutils.Log) bool { // nolint:interfacer
61+
func (r *baseRule) matchSource(issue *result.Issue, lineCache *fsutils.LineCache, log logutils.Log) bool { //nolint:interfacer
6262
sourceLine, errSourceLine := lineCache.GetLine(issue.FilePath(), issue.Line())
6363
if errSourceLine != nil {
6464
log.Warnf("Failed to get line %s:%d from line cache: %s", issue.FilePath(), issue.Line(), errSourceLine)

Diff for: pkg/result/processors/nolint.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,19 @@ func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *to
252252
}
253253
}
254254

255-
if !strings.HasPrefix(text, "nolint:") {
255+
if strings.HasPrefix(text, "nolint:all") || !strings.HasPrefix(text, "nolint:") {
256256
return buildRange(nil) // ignore all linters
257257
}
258258

259259
// ignore specific linters
260260
var linters []string
261261
text = strings.Split(text, "//")[0] // allow another comment after this comment
262262
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
263-
for _, linter := range linterItems {
264-
linterName := strings.ToLower(strings.TrimSpace(linter))
263+
for _, item := range linterItems {
264+
linterName := strings.ToLower(strings.TrimSpace(item))
265+
if linterName == "all" {
266+
panic("OOPS")
267+
}
265268

266269
lcs := p.dbManager.GetLinterConfigs(linterName)
267270
if lcs == nil {

Diff for: pkg/result/processors/testdata/autogen_exclude_doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package testdata
44

55
import "fmt"
66

7-
// nolint
7+
//nolint:all
88
func PrintString(s string) {
99
fmt.Printf("%s", s)
1010
}

Diff for: pkg/result/processors/testdata/nolint.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
package testdata
22

3-
var nolintSpecific int // nolint:gofmt
3+
var nolintSpecific int //nolint:gofmt
44
var nolintSpace int // nolint: gofmt
5-
var nolintSpaces int //nolint: gofmt, govet
6-
var nolintAll int // nolint
7-
var nolintAndAppendix int // nolint // another comment
5+
var nolintSpaces int //nolint:gofmt, govet
6+
var nolintAll int // nolint:all
7+
var nolintAndAppendix int // nolint:all // another comment
88

9-
//nolint
9+
//nolint:all
1010
var nolintVarByPrecedingComment int
1111

12-
//nolint
12+
//nolint:all
1313

1414
var dontNolintVarByPrecedingCommentBecauseOfNewLine int
1515

16-
var nolintPrecedingVar string //nolint
16+
var nolintPrecedingVar string //nolint:all
1717
var dontNolintVarByPrecedingCommentBecauseOfDifferentColumn int
1818

19-
//nolint
19+
//nolint:all
2020
func nolintFuncByPrecedingComment() *string {
2121
xv := "v"
2222
return &xv
2323
}
2424

25-
//nolint
25+
//nolint:all
2626
// second line
2727
func nolintFuncByPrecedingMultilineComment1() *string {
2828
xv := "v"
2929
return &xv
3030
}
3131

3232
// first line
33-
//nolint
33+
//nolint:all
3434
func nolintFuncByPrecedingMultilineComment2() *string {
3535
xv := "v"
3636
return &xv
3737
}
3838

3939
// first line
40-
//nolint
40+
//nolint:all
4141
// third line
4242
func nolintFuncByPrecedingMultilineComment3() *string {
4343
xv := "v"

Diff for: pkg/result/processors/testdata/nolint_bad_names.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package testdata
22

3-
var nolintUnknownLinter1 bool // nolint:bad1,deadcode,varcheck,megacheck
3+
var nolintUnknownLinter1 bool //nolint:bad1,deadcode,varcheck,megacheck
44

55
//nolint:bad2,deadcode,megacheck
66
func nolintUnknownLinter2() {

Diff for: pkg/result/processors/testdata/nolint_unused.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package testdata
22

3-
var nolintVarcheck int // nolint:varcheck
3+
var nolintVarcheck int //nolint:varcheck
44

5-
var nolintVarcheckUnusedOK int // nolint:varcheck,nolintlint
5+
var nolintVarcheckUnusedOK int //nolint:varcheck,nolintlint

Diff for: scripts/expand_website_templates/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ type latestRelease struct {
137137
}
138138

139139
func getLatestVersion() (string, error) {
140-
req, err := http.NewRequest( // nolint:noctx
140+
req, err := http.NewRequest( //nolint:noctx
141141
http.MethodGet,
142142
"https://api.github.com/repos/golangci/golangci-lint/releases/latest",
143143
http.NoBody,

Diff for: test/testdata/default_exclude.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ func ExportedFunc1() {
1111
}
1212

1313
// InvalidFuncComment // ERROR stylecheck `ST1020: comment on exported function ExportedFunc2 should be of the form "ExportedFunc2 ..."`
14-
// nolint:golint
14+
//
15+
//nolint:golint
1516
func ExportedFunc2() {
1617
}
1718

18-
// nolint:stylecheck
19+
//nolint:stylecheck
1920
func IgnoreAll() {
2021
}

Diff for: test/testdata/fix/in/nolintlint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func nolintlint() {
1010
fmt.Println() // nolint:bob // leading spaces should be dropped
1111

1212
// note that the next lines will retain trailing whitespace when fixed
13-
fmt.Println() //nolint // nolint should be dropped
13+
fmt.Println() //nolint:all // nolint should be dropped
1414
fmt.Println() //nolint:lll // nolint should be dropped
1515

1616
fmt.Println() //nolint:alice,lll // we don't drop individual linters from lists

0 commit comments

Comments
 (0)