Skip to content

Add nolintlint linter #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ linters-settings:
dogsled:
# checks assignments with too many blank identifiers; default is 2
max-blank-identifiers: 2
nolintlint:
explain: true # require an explanation for nolint directives
machine: false # don't require machine-readable nolint directives (i.e. with no leading space)
specific: true # require nolint directives to be specific about which linter is being skipped

whitespace:
multi-if: false
Expand Down
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ linters-settings:
disabled-checks:
- wrapperFunc
- dupImport # https://github.com/go-critic/go-critic/issues/845
nolintlint:
explain: false # don't require an explanation for nolint directives
machine: false # don't require machine-readable nolint directives (i.e. with no leading space)
specific: false # don't require nolint directives to be specific about which linter is being skipped

linters:
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ lll: Reports long lines [fast: true, auto-fix: false]
maligned: Tool to detect Go structs that would take less memory if their fields were sorted [fast: true, auto-fix: false]
misspell: Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
nakedret: Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
nolintlint: Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make these changes to README.tmpl.md and regenerate the README.md from that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that this is automatically generated already from somewhere else. I just ran make README.md to create it, so I think this is right but please let me know if there is some other file I should be changing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, @ashanbrown, it is expected for README.md to be re-generated after the README.tmpl.md change. The CI checks specifically for that, forbidding outdated README.md.

So it should be OK.

prealloc: Finds slice declarations that could potentially be preallocated [fast: true, auto-fix: false]
scopelint: Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
stylecheck: Stylecheck is a replacement for golint [fast: false, auto-fix: false]
Expand Down Expand Up @@ -465,6 +466,7 @@ golangci-lint help linters
- [godox](https://github.com/matoous/godox) - Tool for detection of FIXME, TODO and other comment keywords
- [funlen](https://github.com/ultraware/funlen) - Tool for detection of long functions
- [whitespace](https://github.com/ultraware/whitespace) - Tool for detection of leading and trailing whitespace
- [nolintlint](https://github.com/ashanbrown/nolintlint) - Reports ill-formed or insufficient nolint directives

## Configuration

Expand Down Expand Up @@ -789,6 +791,10 @@ linters-settings:
dogsled:
# checks assignments with too many blank identifiers; default is 2
max-blank-identifiers: 2
nolintlint:
explain: true # require an explanation for nolint directives
machine: false # don't require machine-readable nolint directives (i.e. with no leading space)
specific: true # require nolint directives to be specific about which linter is being skipped

whitespace:
multi-if: false
Expand Down Expand Up @@ -917,6 +923,10 @@ linters-settings:
disabled-checks:
- wrapperFunc
- dupImport # https://github.com/go-critic/go-critic/issues/845
nolintlint:
explain: false # don't require an explanation for nolint directives
machine: false # don't require machine-readable nolint directives (i.e. with no leading space)
specific: false # don't require nolint directives to be specific about which linter is being skipped

linters:
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
Expand Down Expand Up @@ -1112,6 +1122,7 @@ Thanks to developers and authors of used linters:
- [leighmcculloch](https://github.com/leighmcculloch)
- [matoous](https://github.com/matoous)
- [ultraware](https://github.com/ultraware)
- [ashanbrown](https://github.com/ashanbrown)

## Changelog

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/OpenPeeDeeP/depguard v1.0.1
github.com/ashanbrown/nolintlint/v2 v2.2.2
github.com/fatih/color v1.7.0
github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
github.com/go-lintpack/lintpack v0.5.2
Expand Down Expand Up @@ -38,7 +39,7 @@ require (
github.com/ultraware/funlen v0.0.2
github.com/ultraware/whitespace v0.0.3
github.com/valyala/quicktemplate v1.2.0
golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678
golang.org/x/tools v0.0.0-20190917215024-905c8ffbfa41
gopkg.in/yaml.v2 v2.2.2
honnef.co/go/tools v0.0.1-2019.2.3
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrU
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ashanbrown/nolintlint/v2 v2.2.2 h1:vwqNclNDHOeE/KHK73fwNty3zVbmWSNk/SitRSzMG8A=
github.com/ashanbrown/nolintlint/v2 v2.2.2/go.mod h1:eFzROBu49LDYj99qhj4UUTtNH0PVFI8P71+UM+Q41wA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand All @@ -28,6 +30,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structtag v1.0.0/go.mod h1:IKitwq45uXL/yqi5mYghiD3w9H6eTOvI9vnk8tXMphA=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -140,6 +143,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/launchdarkly/go-options v1.0.0/go.mod h1:9l3OopYPepd4jRq/QlCAUez94s4i+MaOlo7ToKqSzoA=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
Expand All @@ -164,8 +168,10 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d h1:AREM5mwr4u1
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
Expand Down
29 changes: 21 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,15 @@ type LintersSettings struct {
MultiIf bool `mapstructure:"multi-if"`
}

Lll LllSettings
Unparam UnparamSettings
Nakedret NakedretSettings
Prealloc PreallocSettings
Errcheck ErrcheckSettings
Gocritic GocriticSettings
Godox GodoxSettings
Dogsled DogsledSettings
Lll LllSettings
Unparam UnparamSettings
Nakedret NakedretSettings
Prealloc PreallocSettings
Errcheck ErrcheckSettings
Gocritic GocriticSettings
Godox GodoxSettings
Dogsled DogsledSettings
NoLintLint NoLintLintSettings
}

type GovetSettings struct {
Expand Down Expand Up @@ -243,6 +244,13 @@ type DogsledSettings struct {
MaxBlankIdentifiers int `mapstructure:"max-blank-identifiers"`
}

type NoLintLintSettings struct {
Explain bool
Machine bool
Specific bool
Exclude []string
}

var defaultLintersSettings = LintersSettings{
Lll: LllSettings{
LineLength: 120,
Expand All @@ -268,6 +276,11 @@ var defaultLintersSettings = LintersSettings{
Dogsled: DogsledSettings{
MaxBlankIdentifiers: 2,
},
NoLintLint: NoLintLintSettings{
Explain: false,
Machine: false,
Specific: false,
},
}

type Linters struct {
Expand Down
65 changes: 65 additions & 0 deletions pkg/golinters/nolintlint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package golinters

import (
"context"
"fmt"
"go/ast"

"github.com/ashanbrown/nolintlint/v2/nolintlint"

"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

type NoLintLint struct{}

func (NoLintLint) Name() string {
return "nolintlint"
}

func (NoLintLint) Desc() string {
return "Reports ill-formed or insufficient nolint directives"
}

func (l NoLintLint) Run(ctx context.Context, lintCtx *linter.Context) (results []result.Issue, err error) {
var needs nolintlint.Needs
settings := lintCtx.Settings().NoLintLint
if settings.Explain {
needs |= nolintlint.NeedsExplanation
}
if settings.Machine {
needs |= nolintlint.NeedsMachine
}
if settings.Specific {
needs |= nolintlint.NeedsSpecific
}
lnt, err := nolintlint.NewLinter(
nolintlint.OptionNeeds(needs),
nolintlint.OptionExcludes(settings.Exclude),
)
if err != nil {
return nil, err
}
for _, pkg := range lintCtx.Packages {
files, fset, err := getASTFilesForGoPkg(lintCtx, pkg)
if err != nil {
return nil, fmt.Errorf("could not load files: %s", err)
}
nodes := make([]ast.Node, 0, len(files))
for _, n := range files {
nodes = append(nodes, n)
}
issues, err := lnt.Run(fset, nodes...)
if err != nil {
return nil, fmt.Errorf("linter failed to run: %s", err)
}
for _, i := range issues {
results = append(results, result.Issue{
FromLinter: l.Name(),
Text: i.Details(),
Pos: i.Position(),
})
}
}
return results, nil
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSpeed(10).
WithAutoFix().
WithURL("https://github.com/ultraware/whitespace"),
linter.NewConfig(golinters.NoLintLint{}).
WithLoadFiles().
WithPresets(linter.PresetStyle).
WithSpeed(10).
WithURL("https://github.com/ashanbrown/nolintlint"),
}

isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""
Expand Down
4 changes: 4 additions & 0 deletions pkg/result/processors/nolint.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (p *Nolint) buildIgnoredRangesForFile(f *ast.File, fset *token.FileSet, fil
}

func (p *Nolint) shouldPassIssue(i *result.Issue) (bool, error) {
if i.FromLinter == "nolintlint" {
return true, nil
}

fd, err := p.getOrCreateFileData(i)
if err != nil {
return false, err
Expand Down
15 changes: 15 additions & 0 deletions test/testdata/nolintlint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//args: -Enolintlint
//config: linters-settings.nolintlint.explain=true
//config: linters-settings.nolintlint.specific=true
//config: linters-settings.nolintlint.machine=true
package testdata

import "fmt"

func Foo() {
fmt.Println("not specific") //nolint // ERROR "directive `.*` should mention specific linter such as `//nolint:my-linter`"
fmt.Println("not machine readable") // nolint // ERROR "directive `.*` should be written as `//nolint`"
fmt.Println("bad syntax") //nolint: deadcode // ERROR "directive `.*` should match `//nolint\[:<comma-separated-linters>\] \[// <explanation>\]`"
fmt.Println("bad syntax") //nolint:deadcode lll // ERROR "directive `.*` should match `//nolint\[:<comma-separated-linters>\] \[// <explanation>\]`"
fmt.Println("extra spaces") // nolint:deadcode // because // ERROR "directive `.*` should not have more than one leading space"
}
13 changes: 13 additions & 0 deletions vendor/github.com/ashanbrown/nolintlint/v2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading