Skip to content

Commit 300aff5

Browse files
committed
Add nolintlint directive
1 parent c427c61 commit 300aff5

File tree

91 files changed

+8391
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+8391
-160
lines changed

.golangci.example.yml

+4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ linters-settings:
204204
dogsled:
205205
# checks assignments with too many blank identifiers; default is 2
206206
max-blank-identifiers: 2
207+
nolintlint:
208+
explain: true
209+
machine: false
210+
specific: true
207211

208212
whitespace:
209213
multi-if: false

.golangci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ linters-settings:
4141
disabled-checks:
4242
- wrapperFunc
4343
- dupImport # https://github.com/go-critic/go-critic/issues/845
44+
nolintlint:
45+
explain: true
46+
machine: false
47+
specific: true
4448

4549
linters:
4650
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ lll: Reports long lines [fast: true, auto-fix: false]
213213
maligned: Tool to detect Go structs that would take less memory if their fields were sorted [fast: true, auto-fix: false]
214214
misspell: Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
215215
nakedret: Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
216+
nolintlint: Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
216217
prealloc: Finds slice declarations that could potentially be preallocated [fast: true, auto-fix: false]
217218
scopelint: Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
218219
stylecheck: Stylecheck is a replacement for golint [fast: false, auto-fix: false]
@@ -465,6 +466,7 @@ golangci-lint help linters
465466
- [godox](https://github.com/matoous/godox) - Tool for detection of FIXME, TODO and other comment keywords
466467
- [funlen](https://github.com/ultraware/funlen) - Tool for detection of long functions
467468
- [whitespace](https://github.com/ultraware/whitespace) - Tool for detection of leading and trailing whitespace
469+
- [nolintlint](https://github.com/ashanbrown/nolintlint) - Reports ill-formed or insufficient nolint directives
468470
469471
## Configuration
470472
@@ -789,6 +791,10 @@ linters-settings:
789791
dogsled:
790792
# checks assignments with too many blank identifiers; default is 2
791793
max-blank-identifiers: 2
794+
nolintlint:
795+
explain: true
796+
machine: false
797+
specific: true
792798
793799
whitespace:
794800
multi-if: false
@@ -917,6 +923,10 @@ linters-settings:
917923
disabled-checks:
918924
- wrapperFunc
919925
- dupImport # https://github.com/go-critic/go-critic/issues/845
926+
nolintlint:
927+
explain: true
928+
machine: false
929+
specific: true
920930
921931
linters:
922932
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
@@ -1112,6 +1122,7 @@ Thanks to developers and authors of used linters:
11121122
- [leighmcculloch](https://github.com/leighmcculloch)
11131123
- [matoous](https://github.com/matoous)
11141124
- [ultraware](https://github.com/ultraware)
1125+
- [ashanbrown](https://github.com/ashanbrown)
11151126
11161127
## Changelog
11171128

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.12
44

55
require (
66
github.com/OpenPeeDeeP/depguard v1.0.1
7+
github.com/ashanbrown/nolintlint/v2 v2.2.2
78
github.com/fatih/color v1.7.0
89
github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
910
github.com/go-lintpack/lintpack v0.5.2
@@ -38,7 +39,9 @@ require (
3839
github.com/ultraware/funlen v0.0.2
3940
github.com/ultraware/whitespace v0.0.3
4041
github.com/valyala/quicktemplate v1.2.0
41-
golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678
42+
golang.org/x/net v0.0.0-20190918130420-a8b05e9114ab // indirect
43+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 // indirect
44+
golang.org/x/tools v0.0.0-20190917215024-905c8ffbfa41
4245
gopkg.in/yaml.v2 v2.2.2
4346
honnef.co/go/tools v0.0.1-2019.2.3
4447
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrU
99
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
1010
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
1111
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
12+
github.com/ashanbrown/nolintlint/v2 v2.2.2 h1:vwqNclNDHOeE/KHK73fwNty3zVbmWSNk/SitRSzMG8A=
13+
github.com/ashanbrown/nolintlint/v2 v2.2.2/go.mod h1:eFzROBu49LDYj99qhj4UUTtNH0PVFI8P71+UM+Q41wA=
1214
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
1315
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
1416
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
@@ -28,6 +30,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
2830
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
2931
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
3032
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
33+
github.com/fatih/structtag v1.0.0/go.mod h1:IKitwq45uXL/yqi5mYghiD3w9H6eTOvI9vnk8tXMphA=
3134
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
3235
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3336
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
@@ -140,6 +143,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
140143
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
141144
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
142145
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
146+
github.com/launchdarkly/go-options v1.0.0/go.mod h1:9l3OopYPepd4jRq/QlCAUez94s4i+MaOlo7ToKqSzoA=
143147
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
144148
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
145149
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
@@ -164,8 +168,10 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d h1:AREM5mwr4u1
164168
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
165169
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
166170
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
171+
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
167172
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
168173
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
174+
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
169175
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
170176
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
171177
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
@@ -265,6 +271,8 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
265271
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
266272
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b h1:XfVGCX+0T4WOStkaOsJRllbsiImhB2jgVBGc9L0lPGc=
267273
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
274+
golang.org/x/net v0.0.0-20190918130420-a8b05e9114ab h1:h5tBRKZ1aY/bo6GNqe/4zWC8GkaLOFQ5wPKIOQ0i2sA=
275+
golang.org/x/net v0.0.0-20190918130420-a8b05e9114ab/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
268276
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
269277
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
270278
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -282,6 +290,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
282290
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
283291
golang.org/x/sys v0.0.0-20190911201528-7ad0cfa0b7b5 h1:SW/0nsKCUaozCUtZTakri5laocGx/5bkDSSLrFUsa5s=
284292
golang.org/x/sys v0.0.0-20190911201528-7ad0cfa0b7b5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
293+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 h1:7TYNF4UdlohbFwpNH04CoPMp1cHUZgO1Ebq5r2hIjfo=
294+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
285295
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
286296
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
287297
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

pkg/config/config.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ type LintersSettings struct {
175175
MultiIf bool `mapstructure:"multi-if"`
176176
}
177177

178-
Lll LllSettings
179-
Unparam UnparamSettings
180-
Nakedret NakedretSettings
181-
Prealloc PreallocSettings
182-
Errcheck ErrcheckSettings
183-
Gocritic GocriticSettings
184-
Godox GodoxSettings
185-
Dogsled DogsledSettings
178+
Lll LllSettings
179+
Unparam UnparamSettings
180+
Nakedret NakedretSettings
181+
Prealloc PreallocSettings
182+
Errcheck ErrcheckSettings
183+
Gocritic GocriticSettings
184+
Godox GodoxSettings
185+
Dogsled DogsledSettings
186+
NoLintLint NoLintLintSettings
186187
}
187188

188189
type GovetSettings struct {
@@ -243,6 +244,13 @@ type DogsledSettings struct {
243244
MaxBlankIdentifiers int `mapstructure:"max-blank-identifiers"`
244245
}
245246

247+
type NoLintLintSettings struct {
248+
Explain bool
249+
Machine bool
250+
Specific bool
251+
Exclude []string
252+
}
253+
246254
var defaultLintersSettings = LintersSettings{
247255
Lll: LllSettings{
248256
LineLength: 120,
@@ -268,6 +276,11 @@ var defaultLintersSettings = LintersSettings{
268276
Dogsled: DogsledSettings{
269277
MaxBlankIdentifiers: 2,
270278
},
279+
NoLintLint: NoLintLintSettings{
280+
Explain: false,
281+
Machine: false,
282+
Specific: true,
283+
},
271284
}
272285

273286
type Linters struct {

pkg/golinters/nolintlint.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package golinters
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"go/ast"
7+
8+
"github.com/ashanbrown/nolintlint/v2/nolintlint"
9+
10+
"github.com/golangci/golangci-lint/pkg/lint/linter"
11+
"github.com/golangci/golangci-lint/pkg/result"
12+
)
13+
14+
type NoLintLint struct{}
15+
16+
func (NoLintLint) Name() string {
17+
return "nolintlint"
18+
}
19+
20+
func (NoLintLint) Desc() string {
21+
return "Reports ill-formed or insufficient nolint directives"
22+
}
23+
24+
func (l NoLintLint) Run(ctx context.Context, lintCtx *linter.Context) (results []result.Issue, err error) {
25+
var needs nolintlint.Needs
26+
settings := lintCtx.Settings().NoLintLint
27+
if settings.Explain {
28+
needs |= nolintlint.NeedsExplanation
29+
}
30+
if settings.Machine {
31+
needs |= nolintlint.NeedsMachine
32+
}
33+
if settings.Specific {
34+
needs |= nolintlint.NeedsSpecific
35+
}
36+
lnt, err := nolintlint.NewLinter(
37+
nolintlint.OptionNeeds(needs),
38+
nolintlint.OptionExcludes(settings.Exclude),
39+
)
40+
if err != nil {
41+
return nil, err
42+
}
43+
for _, pkg := range lintCtx.Packages {
44+
files, fset, err := getASTFilesForGoPkg(lintCtx, pkg)
45+
if err != nil {
46+
return nil, fmt.Errorf("could not load files: %s", err)
47+
}
48+
nodes := make([]ast.Node, 0, len(files))
49+
for _, n := range files {
50+
nodes = append(nodes, n)
51+
}
52+
issues, err := lnt.Run(fset, nodes...)
53+
if err != nil {
54+
return nil, fmt.Errorf("linter failed to run: %s", err)
55+
}
56+
for _, i := range issues {
57+
results = append(results, result.Issue{
58+
FromLinter: l.Name(),
59+
Text: i.Details(),
60+
Pos: i.Position(),
61+
})
62+
}
63+
}
64+
return results, nil
65+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
257257
WithSpeed(10).
258258
WithAutoFix().
259259
WithURL("https://github.com/ultraware/whitespace"),
260+
linter.NewConfig(golinters.NoLintLint{}).
261+
WithLoadFiles().
262+
WithPresets(linter.PresetStyle).
263+
WithSpeed(10).
264+
WithURL("https://github.com/ashanbrown/nolintlint"),
260265
}
261266

262267
isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""

pkg/result/processors/nolint.go

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ func (p *Nolint) buildIgnoredRangesForFile(f *ast.File, fset *token.FileSet, fil
126126
}
127127

128128
func (p *Nolint) shouldPassIssue(i *result.Issue) (bool, error) {
129+
if i.FromLinter == "nolintlint" {
130+
return true, nil
131+
}
132+
129133
fd, err := p.getOrCreateFileData(i)
130134
if err != nil {
131135
return false, err

test/linters_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func testSourcesFromDir(t *testing.T, dir string) {
4949

5050
for _, s := range sources {
5151
s := s
52+
if s != "testdata/nolintlint.go" {
53+
continue
54+
}
5255
t.Run(filepath.Base(s), func(t *testing.T) {
5356
t.Parallel()
5457
testOneSource(t, s)

test/testdata/nolintlint.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//args: -Enolintlint
2+
//config: linters-settings.nolintlint.explain=true
3+
//config: linters-settings.nolintlint.specific=true
4+
//config: linters-settings.nolintlint.machine=true
5+
package testdata
6+
7+
import "fmt"
8+
9+
func Foo() {
10+
fmt.Println("not specific") //nolint // ERROR "directive `.*` should mention specific linter such as `//nolint:my-linter`"
11+
fmt.Println("not machine readable") // nolint // ERROR "directive `.*` should be written as `//nolint`"
12+
fmt.Println("bad syntax") //nolint: deadcode // ERROR "directive `.*` should match `//nolint\[:<comma-separated-linters>\] \[// <explanation>\]`"
13+
fmt.Println("bad syntax") //nolint:deadcode lll // ERROR "directive `.*` should match `//nolint\[:<comma-separated-linters>\] \[// <explanation>\]`"
14+
fmt.Println("extra spaces") // nolint:deadcode // because // ERROR "directive `.*` should not have more than one leading space"
15+
}

vendor/github.com/ashanbrown/nolintlint/v2/LICENSE

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/ashanbrown/nolintlint/v2/nolintlint/config_options.go

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)