-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
nolintlint: allow to fix //nolint lines #1583
nolintlint: allow to fix //nolint lines #1583
Conversation
func nolintlint() { | ||
run() // nolint:bob // leading space should be dropped | ||
run() // nolint:bob // leading spaces should be dropped | ||
fmt.Println() // nolint:bob // leading space should be dropped |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just changed this to something that doesn't trigger other linter errors
83e14f8
to
ba47cb7
Compare
This comment has been minimized.
This comment has been minimized.
141b43f
to
7e85d7d
Compare
This comment has been minimized.
This comment has been minimized.
if issue.ExpectNoLint { | ||
if issue.ExpectedNoLintLinter != "" { | ||
return i.matchedIssueFromLinter[issue.ExpectedNoLintLinter] | ||
// only allow selective nolinting of nolintlint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been reorganized so that we can do //nolint:nolintlint
to disable nolintlint
checking, having it behave just like every other linter. The one exception is that a blanket //nolint
directive does not disable the nolintlint
linter.
This comment has been minimized.
This comment has been minimized.
496a54b
to
dcd5924
Compare
dcd5924
to
7c992af
Compare
@@ -141,19 +146,14 @@ func (p *Nolint) buildIgnoredRangesForFile(f *ast.File, fset *token.FileSet, fil | |||
|
|||
func (p *Nolint) shouldPassIssue(i *result.Issue) (bool, error) { | |||
nolintDebugf("got issue: %v", *i) | |||
if i.FromLinter == golinters.NolintlintName { | |||
// always pass nolintlint issues except ones trying find unused nolint directives | |||
if !i.ExpectNoLint { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we are allowing nolint directives to be excluded by //nolint:nolintlint
.
@ldez I would love it if you could take a look at this re-attempt to add a |
c133cbc
to
d0f91d0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I confirm that #1579, #1580, #1581 are fixed.
Trailing comma after the fix
one unused linter (begin) [OK]
// wantedErrors parses expected errors from comments in a file.
//nolint:gocyclo,nakedret
func wantedErrors(file, short string) (errs []wantedError) {
// wantedErrors parses expected errors from comments in a file.
//nolint:nakedret
func wantedErrors(file, short string) (errs []wantedError) {
one unused linter (end) [KO]
// wantedErrors parses expected errors from comments in a file.
//nolint:nakedret,gocyclo
func wantedErrors(file, short string) (errs []wantedError) {
// wantedErrors parses expected errors from comments in a file.
//nolint:nakedret,
func wantedErrors(file, short string) (errs []wantedError) {
two unused linter (begin) [OK]
// wantedErrors parses expected errors from comments in a file.
//nolint:gocyclo,dupl,nakedret
func wantedErrors(file, short string) (errs []wantedError) {
// wantedErrors parses expected errors from comments in a file.
//nolint:nakedret
func wantedErrors(file, short string) (errs []wantedError) {
two unused linter (end) [KO]
// wantedErrors parses expected errors from comments in a file.
//nolint:nakedret,gocyclo,dupl
func wantedErrors(file, short string) (errs []wantedError) {
// wantedErrors parses expected errors from comments in a file.
//nolint:nakedret,
func wantedErrors(file, short string) (errs []wantedError) {
Trailing two dot after the fix
only unused linters [KO]
// wantedErrors parses expected errors from comments in a file.
//nolint:gocyclo,dupl
func wantedErrors(file, short string) (errs []wantedError) {
// wantedErrors parses expected errors from comments in a file.
//nolint:
func wantedErrors(file, short string) (errs []wantedError) {
test/errchk.go:163:1: directive `//nolint:` should match `//nolint:[:<comma-separated-linters>] [// <explanation>]` (nolintlint)
//nolint:
^
The fact to not have a homogeneous behavior depending on the position of the unused linter is not expected for me.
In some cases (ex: only unused linters), the auto-fix produces an error
I think that the trailing commas must handle by this PR.
Also when the auto-fix will produce an empty //nolint:
, I think that the fix must not be applied, and Replacement{NeedOnlyDelete: true}
can be better here.
@ldez Thanks for looking at this more. I'm concerned trying to completely cleanly handle the cases with multiple linters specified with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ashanbrown could you clean a bit your PR? I think you made a wrong rebase 😉
6723032
to
eace6a1
Compare
6723032
to
c69dde5
Compare
@ldez I think I've fixed it up. The one change since last time is to just skip trying to fix unused linter issues for directives with multiple linters specified. |
@ldez Tests are passing now. I hadn't fixed them correctly after merging the latest code and needed to add another |
Repair whitespace issues and remove nolint statements that aren't being triggered
3c960f1
to
d7db174
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify the scope of the PR because the scope of the PR has changed since it was open: this PR only fixes the cases where only one linter is defined in the nolint
directive and this only one linter is unused.
This PR adds a fixing capability to nolintlint. It doesn't try to fix everything.
What it does (when --fix is enabled):
allow-leading-whitespace
is set tofalse
, in which case we assume no leading spaces (i.e.//nolint
).What it does not do:
Note that fixes may leave trailing spaces or empty lines behind. Presumably, this would be cleaned up by gofmt in most cases which could also fix these cases. I'm not sure whether it's really possible or necessary to try to remove such spaces. It would be nice to at least remove empty lines if there is a way to identify if a comment is the only thing on a line, but I'm not sure how to do that just given the AST. One possibility might be to give the fixer a hint to clean up trailing whitespace or remove blank lines. That could potentially be useful for other linters as well.
Fixes #1579
Fixes #1580
Fixes #1581
Related to #1573