|
2 | 2 | title: False Positives
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -False positives are inevitable, but we did our best to reduce their count. For example, we have a default enabled set of [exclude patterns](/usage/configuration#command-line-options). If a false positive occurred you have the following choices: |
| 5 | +False positives are inevitable, but we did our best to reduce their count. |
| 6 | +For example, we have a default enabled set of [exclude patterns](/usage/configuration#command-line-options). |
6 | 7 |
|
7 |
| -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. |
8 |
| -2. Exclude this one issue by using special comment `//nolint` (see [the section](#nolint) below). |
9 |
| -3. Exclude issues in path by `run.skip-dirs`, `run.skip-files` or `issues.exclude-rules` config options. |
| 8 | +If a false positive occurred, you have the several choices. |
10 | 9 |
|
11 |
| -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. |
| 10 | +## Specific Linter Excludes |
12 | 11 |
|
13 |
| -## Nolint |
| 12 | +Most of the linters has a configuration, sometimes false-positives can be related to a bad configuration of a linter. |
| 13 | +So it's recommended to check the linters configuration. |
14 | 14 |
|
15 |
| -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. |
| 15 | +Otherwise, some linters have dedicated configuration to exclude or disable rules. |
| 16 | + |
| 17 | +An example with `staticcheck`: |
| 18 | + |
| 19 | +```yml |
| 20 | +linters-settings: |
| 21 | + staticcheck: |
| 22 | + checks: |
| 23 | + - all |
| 24 | + - '-SA1000' # disable the rule SA1000 |
| 25 | + - '-SA1004' # disable the rule SA1004 |
| 26 | +``` |
| 27 | +
|
| 28 | +## Exclude or Skip |
| 29 | +
|
| 30 | +### Exclude Issue by Text |
| 31 | +
|
| 32 | +Exclude issue by text using command-line option `-e` or config option `issues.exclude`. |
| 33 | +It's helpful when you decided to ignore all issues of this type. |
| 34 | +Also, you can use `issues.exclude-rules` config option for per-path or per-linter configuration. |
| 35 | + |
| 36 | +In the following example, all the reports that contains the sentences defined in `exclude` are excluded: |
| 37 | + |
| 38 | +```yml |
| 39 | +issues: |
| 40 | + exclude: |
| 41 | + - "Error return value of .((os\\.)?std(out|err)\\..*|.*Close|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked" |
| 42 | + - "exported (type|method|function) (.+) should have comment or be unexported" |
| 43 | + - "ST1000: at least one file in a package should have a package comment" |
| 44 | +``` |
| 45 | + |
| 46 | +In the following example, all the reports from the linters (`linters`) that contains the text (`text`) are excluded: |
| 47 | + |
| 48 | +```yml |
| 49 | +issues: |
| 50 | + exclude-rules: |
| 51 | + - linters: |
| 52 | + - gomnd |
| 53 | + text: "mnd: Magic number: 9" |
| 54 | +``` |
| 55 | + |
| 56 | +In the following example, all the reports that contains the text (`text`) in the path (`path`) are excluded: |
| 57 | + |
| 58 | +```yml |
| 59 | +issues: |
| 60 | + exclude-rules: |
| 61 | + - path: path/to/a/file.go |
| 62 | + text: "string `example` has (\\d+) occurrences, make it a constant" |
| 63 | +``` |
| 64 | + |
| 65 | +### Exclude Issues by Path |
| 66 | + |
| 67 | +Exclude issues in path by `run.skip-dirs`, `run.skip-files` or `issues.exclude-rules` config options. |
| 68 | + |
| 69 | +In the following example, all the reports from the linters (`linters`) that concerns the path (`path`) are excluded: |
| 70 | + |
| 71 | +```yml |
| 72 | +issues: |
| 73 | + exclude-rules: |
| 74 | + - path: '(.+)_test\.go' |
| 75 | + linters: |
| 76 | + - funlen |
| 77 | + - goconst |
| 78 | +``` |
| 79 | +
|
| 80 | +In the following example, all the reports related to the files (`skip-files`) are excluded: |
| 81 | + |
| 82 | +```yml |
| 83 | +run: |
| 84 | + skip-files: |
| 85 | + - path/to/a/file.go |
| 86 | +``` |
| 87 | + |
| 88 | +In the following example, all the reports related to the directories (`skip-dirs`) are excluded: |
| 89 | + |
| 90 | +```yml |
| 91 | +run: |
| 92 | + skip-dirs: |
| 93 | + - path/to/a/dir/ |
| 94 | +``` |
| 95 | + |
| 96 | +## Nolint Directive |
| 97 | + |
| 98 | +To exclude issues from all linters use `//nolint`. |
| 99 | +For example, if it's used inline (not from the beginning of the line) it excludes issues only for this line. |
16 | 100 |
|
17 | 101 | ```go
|
18 | 102 | var bad_name int //nolint
|
|
0 commit comments