Skip to content

Commit f8f8f9a

Browse files
authored
nolint: drop allow-leading-space option and add "nolint:all" (#3002)
1 parent 39fc81c commit f8f8f9a

File tree

22 files changed

+68
-76
lines changed

22 files changed

+68
-76
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
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

docs/src/docs/usage/false-positives.mdx

Lines changed: 3 additions & 3 deletions
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
}

pkg/config/linters_settings.go

Lines changed: 0 additions & 2 deletions
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"`

pkg/golinters/godot.go

Lines changed: 1 addition & 1 deletion
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
}

pkg/golinters/importas.go

Lines changed: 2 additions & 2 deletions
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 {

pkg/golinters/nolintlint.go

Lines changed: 0 additions & 3 deletions
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
}

pkg/golinters/nolintlint/README.md

Lines changed: 4 additions & 4 deletions
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

pkg/golinters/nolintlint/nolintlint.go

Lines changed: 8 additions & 5 deletions
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:")

pkg/golinters/nolintlint/nolintlint_test.go

Lines changed: 13 additions & 25 deletions
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
{

pkg/golinters/staticcheck_common.go

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)