Skip to content

Commit 7db400b

Browse files
committed
fix golangci#521: explain //nolint usage in README
Also, add more tests for block-wise usage of //nolint.
1 parent ad9de15 commit 7db400b

File tree

5 files changed

+112
-14
lines changed

5 files changed

+112
-14
lines changed

README.md

+37-7
Original file line numberDiff line numberDiff line change
@@ -883,20 +883,50 @@ service:
883883
False positives are inevitable, but we did our best to reduce their count. For example, we have a default enabled set of [exclude patterns](#command-line-options). If a false positive occurred you have the following choices:
884884
885885
1. Exclude issue by text using command-line option `-e` or config option `issues.exclude`. It's helpful when you decided to ignore all issues of this type. Also, you can use `issues.exclude-rules` config option for per-path or per-linter configuration.
886-
2. Exclude this one issue by using special comment `//nolint[:linter1,linter2,...]` on issued line.
887-
Comment `//nolint` disables all issues reporting on this line. Comment e.g. `//nolint:govet` disables only govet issues for this line.
888-
If you would like to completely exclude all issues for some function prepend this comment
889-
above function:
886+
2. Exclude this one issue by using special comment `//nolint` (see [the section](#nolint) below).
890887
3. Exclude issues in path by `run.skip-dirs`, `run.skip-files` or `issues.exclude-rules` config options.
891888
889+
Please create [GitHub Issues here](https://github.com/golangci/golangci-lint/issues/new) if you find any false positives. We will add it to the default exclude list if it's common or we will fix underlying linter.
890+
891+
### Nolint
892+
893+
To exclude issues from all linters use `//nolint`. For example, if it's used inline (not from the beginning of the line) it excludes issues only for this line.
894+
895+
```go
896+
var bad_name int //nolint
897+
```
898+
899+
To exclude issues from specific linters only:
900+
901+
```go
902+
var bad_name int //nolint:golint,unused
903+
```
904+
905+
To exclude issues for the block of code use this directive on the beginning of a line:
906+
892907
```go
893908
//nolint
894-
func f() {
895-
...
909+
func allIssuesInThisFunctionAreExcluded() *string {
910+
// ...
896911
}
912+
913+
//nolint:govet
914+
var (
915+
a int
916+
b int
917+
)
897918
```
898919
899-
Please create [GitHub Issues here](https://github.com/golangci/golangci-lint/issues/new) if you find any false positives. We will add it to the default exclude list if it's common or we will fix underlying linter.
920+
Also, you can exclude all issues in a file by:
921+
922+
```go
923+
//nolint: unparam
924+
package pkg
925+
```
926+
927+
You can see more examples of using `//nolint` in [our tests](https://github.com/golangci/golangci-lint/tree/master/pkg/result/processors/testdata) for it.
928+
929+
Use `//nolint` instead of `// nolint` because machine-readable comments should have no space by Go convention.
900930
901931
## FAQ
902932

README.tmpl.md

+37-7
Original file line numberDiff line numberDiff line change
@@ -430,20 +430,50 @@ than the default and have more strict settings:
430430
False positives are inevitable, but we did our best to reduce their count. For example, we have a default enabled set of [exclude patterns](#command-line-options). If a false positive occurred you have the following choices:
431431

432432
1. Exclude issue by text using command-line option `-e` or config option `issues.exclude`. It's helpful when you decided to ignore all issues of this type. Also, you can use `issues.exclude-rules` config option for per-path or per-linter configuration.
433-
2. Exclude this one issue by using special comment `//nolint[:linter1,linter2,...]` on issued line.
434-
Comment `//nolint` disables all issues reporting on this line. Comment e.g. `//nolint:govet` disables only govet issues for this line.
435-
If you would like to completely exclude all issues for some function prepend this comment
436-
above function:
433+
2. Exclude this one issue by using special comment `//nolint` (see [the section](#nolint) below).
437434
3. Exclude issues in path by `run.skip-dirs`, `run.skip-files` or `issues.exclude-rules` config options.
438435

436+
Please create [GitHub Issues here](https://github.com/golangci/golangci-lint/issues/new) if you find any false positives. We will add it to the default exclude list if it's common or we will fix underlying linter.
437+
438+
### Nolint
439+
440+
To exclude issues from all linters use `//nolint`. For example, if it's used inline (not from the beginning of the line) it excludes issues only for this line.
441+
442+
```go
443+
var bad_name int //nolint
444+
```
445+
446+
To exclude issues from specific linters only:
447+
448+
```go
449+
var bad_name int //nolint:golint,unused
450+
```
451+
452+
To exclude issues for the block of code use this directive on the beginning of a line:
453+
439454
```go
440455
//nolint
441-
func f() {
442-
...
456+
func allIssuesInThisFunctionAreExcluded() *string {
457+
// ...
443458
}
459+
460+
//nolint:govet
461+
var (
462+
a int
463+
b int
464+
)
444465
```
445466

446-
Please create [GitHub Issues here](https://github.com/golangci/golangci-lint/issues/new) if you find any false positives. We will add it to the default exclude list if it's common or we will fix underlying linter.
467+
Also, you can exclude all issues in a file by:
468+
469+
```go
470+
//nolint: unparam
471+
package pkg
472+
```
473+
474+
You can see more examples of using `//nolint` in [our tests](https://github.com/golangci/golangci-lint/tree/master/pkg/result/processors/testdata) for it.
475+
476+
Use `//nolint` instead of `// nolint` because machine-readable comments should have no space by Go convention.
447477

448478
## FAQ
449479

pkg/result/processors/nolint_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func newTestNolintProcessor(log logutils.Log) *Nolint {
3737
filepath.Join("testdata", "nolint.go"),
3838
filepath.Join("testdata", "nolint2.go"),
3939
filepath.Join("testdata", "nolint_bad_names.go"),
40+
filepath.Join("testdata", "nolint_whole_file.go"),
4041
)
4142
return NewNolint(cache, log, lintersdb.NewManager(nil))
4243
}
@@ -123,6 +124,11 @@ func TestNolint(t *testing.T) {
123124
for i := 15; i <= 18; i++ {
124125
processAssertSame(t, p, newNolint2FileIssue(i))
125126
}
127+
128+
// variables block exclude
129+
for i := 55; i <= 56; i++ {
130+
processAssertSame(t, p, newNolint2FileIssue(i))
131+
}
126132
}
127133

128134
func TestNolintInvalidLinterName(t *testing.T) {
@@ -226,3 +232,25 @@ func TestIgnoredRangeMatches(t *testing.T) {
226232
assert.Equal(t, testcase.expected, ir.doesMatch(&testcase.issue), testcase.doc)
227233
}
228234
}
235+
236+
func TestNolintWholeFile(t *testing.T) {
237+
fileName := filepath.Join("testdata", "nolint_whole_file.go")
238+
239+
p := newTestNolintProcessor(nil)
240+
defer p.Finish()
241+
242+
processAssertEmpty(t, p, result.Issue{
243+
Pos: token.Position{
244+
Filename: fileName,
245+
Line: 4,
246+
},
247+
FromLinter: "unparam",
248+
})
249+
processAssertSame(t, p, result.Issue{
250+
Pos: token.Position{
251+
Filename: fileName,
252+
Line: 4,
253+
},
254+
FromLinter: "deadcode",
255+
})
256+
}

pkg/result/processors/testdata/nolint.go

+6
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ var nolintAliasGAS bool //nolint:gas
4949
var nolintAliasGosec bool //nolint:gosec
5050

5151
var nolintAliasUpperCase int // nolint: GAS
52+
53+
//nolint:errcheck
54+
var (
55+
nolintVarBlockVar1 int
56+
nolintVarBlockVar2 int
57+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//nolint: unparam
2+
package testdata
3+
4+
var nolintUnparam int

0 commit comments

Comments
 (0)