False Positives
False positives are inevitable, but we did our best to reduce their count.
If a false positive occurred, you have several choices.
Specific Linter Excludes
Most of the linters have a configuration, sometimes false-positives can be related to a bad configuration of a linter. So it’s recommended to check the linter configurations.
Otherwise, some linters have dedicated configuration to exclude or disable rules.
An example with staticcheck:
linters:
settings:
staticcheck:
checks:
- all
- '-SA1000' # disable the rule SA1000
- '-SA1004' # disable the rule SA1004Exclude
Exclude Issue by Text
You can use linters.exclusions.rules config option for per-path or per-linter configuration.
In the following example, all the reports from the linters (linters) that contains the text (text) are excluded:
linters:
exclusions:
rules:
- linters:
- mnd
text: "Magic number: 9"In the following example, all the reports from the linters (linters) that originated from the source (source) are excluded:
linters:
exclusions:
rules:
- linters:
- lll
source: "^//go:generate "In the following example, all the reports that contains the text (text) in the path (path) are excluded:
linters:
exclusions:
rules:
- path: path/to/a/file.go
text: "string `example` has (\\d+) occurrences, make it a constant"Exclude Issues by Path
Exclude issues in path by linters.exclusions.paths or linters.exclusions.rules config options.
In the following example, all the reports from the linters (linters) that concerns the path (path) are excluded:
linters:
exclusions:
rules:
- path: '(.+)_test\.go'
linters:
- funlen
- goconstThe opposite, excluding reports except for specific paths, is also possible. In the following example, only test files get checked:
linters:
exclusions:
rules:
- path-except: '(.+)_test\.go'
linters:
- funlen
- goconstIn the following example, all the reports related to the files (paths) are excluded:
linters:
exclusions:
paths:
- path/to/a/file.goIn the following example, all the reports related to the directories (paths) are excluded:
linters:
exclusions:
paths:
- path/to/a/dir/Nolint Directive
To exclude issues from all linters use //nolint:all.
For example, if it’s used inline (not from the beginning of the line) it excludes issues only for this line.
var bad_name int //nolint:allTo exclude issues from specific linters only:
var bad_name int //nolint:wsl,unusedTo exclude issues for the block of code, use this directive at the beginning of a line:
//nolint:all
func allIssuesInThisFunctionAreExcluded() *string {
// ...
}
//nolint:govet
var (
a int
b int
)Also, you can exclude all issues in a file by:
//nolint:unparam
package pkgYou may add a comment explaining or justifying why a nolint directive is being used on the same line as the flag itself:
//nolint:gocyclo // This legacy function is complex, but the team too busy to simplify it
func someLegacyFunction() *string {
// ...
}You can see more examples of using nolint directives in our tests for it.
Syntax
nolint is not regular comment but a directive.
A directive comment is a line matching the regular expression
//(line |extern |export |[a-z0-9]+:[a-z0-9]). https://go.dev/doc/comment#syntax
This means that no spaces are allowed between:
//andnolintnolintand::and the name of the linter.
Invalid syntax:
// nolint
// nolint:xxx
//nolint :xxx
//nolint: xxxValid syntax:
//nolint:xxxExclusion Presets
Some exclusions are considered common. To help golangci-lint users, those common exclusions are provided through presets.
linters:
exclusions:
presets:
- comments
- common-false-positives
- legacy
- std-error-handlingPreset comments
| Linter | Issue Text |
|---|---|
| staticcheck | (ST1000|ST1020|ST1021|ST1022) |
| revive | exported (.+) should have comment( \(or a comment on this block\))? or be unexported |
| revive | package comment should be of the form "(.+)..." |
| revive | comment on exported (.+) should be of the form "(.+)..." |
| revive | should have a package comment |
Preset common-false-positives
| Linter | Issue Text |
|---|---|
| gosec | G103: Use of unsafe calls should be audited |
| gosec | G204: Subprocess launched with variable |
| gosec | G304: Potential file inclusion via variable |
Preset legacy
| Linter | Issue Text |
|---|---|
| govet | (possible misuse of unsafe.Pointer|should have signature) |
| staticcheck | SA4011 |
| gosec | G104 |
| gosec | (G301|G302|G307): Expect (directory permissions to be 0750|file permissions to be 0600) or less |
Preset std-error-handling
| Linter | Issue Text |
|---|---|
| errcheck | (?i)Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked |