-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update nolintlint to fix nolint formatting and remove unused nolint statements #1573
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
Update nolintlint to fix nolint formatting and remove unused nolint statements #1573
Conversation
e593eef
to
7489c5a
Compare
pkg/result/processors/nolint.go
Outdated
ir.matchedIssueFromLinter[i.FromLinter] = true | ||
return false, nil | ||
matchesAtLeastOneRange = true |
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 don't have test support for this. It seemed like it would be pretty complicated to construct but I'm open to suggestions.
This was needed because when I converted golangci lint to use machine-readable style directives (no leading space), the interfacer
linter created a problem at https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/scopelint.go#L166 (although this line itself did not change).
Running with env GL_DEBUG=nolint ./golangci-lint run -E interfacer --disable-all
, you can see the nolint range getting expanded to include the entire function:
DEBU [nolint] file pkg/golinters/scopelint.go: inline nolint ranges are [{linters:[gocyclo gocritic] matchedIssueFromLinter:map[] Range:{From:73 To:76} col:1} {linters:[interfacer] matchedIssueFromLinter:map[] Range:{From:163 To:166} col:1}]
DEBU [nolint] found range is {[gocyclo gocritic] map[] {73 76} 1} for node &ast.FuncDecl{Doc:(*ast.CommentGroup)(0xc002b978c0), Recv:(*ast.FieldList)(0xc002b87ef0), Name:(*ast.Ident)(0xc002b97960), Type:(*ast.FuncType)(0xc0007776e0), Body:(*ast.BlockStmt)(0xc001e18b10)} [77;161], expanded range is {[gocyclo gocritic] map[] {73 161} 1}
DEBU [nolint] found range is {[gocyclo gocritic] map[] {73 76} 1} for node &ast.FuncType{Func:1966, Params:(*ast.FieldList)(0xc002b87f20), Results:(*ast.FieldList)(0xc002b87f50)} [77;77], expanded range is {[gocyclo gocritic] map[] {73 77} 1}
DEBU [nolint] found range is {[interfacer] map[] {163 166} 1} for node &ast.FuncDecl{Doc:(*ast.CommentGroup)(0xc0007776c0), Recv:(*ast.FieldList)(0xc001e18ba0), Name:(*ast.Ident)(0xc000777760), Type:(*ast.FuncType)(0xc000777ae0), Body:(*ast.BlockStmt)(0xc001e18c60)} [167;170], expanded range is {[interfacer] map[] {163 170} 1}
DEBU [nolint] found range is {[interfacer] map[] {163 166} 1} for node &ast.FuncType{Func:4213, Params:(*ast.FieldList)(0xc001e18c00), Results:(*ast.FieldList)(nil)} [167;167], expanded range is {[interfacer] map[] {163 167} 1}
DEBU [nolint] file pkg/golinters/scopelint.go: built nolint ranges are [{linters:[gocyclo gocritic] matchedIssueFromLinter:map[] Range:{From:73 To:76} col:1} {linters:[interfacer] matchedIssueFromLinter:map[] Range:{From:163 To:166} col:1} {linters:[gocyclo gocritic] matchedIssueFromLinter:map[] Range:{From:73 To:161} col:1} {linters:[gocyclo gocritic] matchedIssueFromLinter:map[] Range:{From:73 To:77} col:1} {linters:[interfacer] matchedIssueFromLinter:map[] Range:{From:163 To:170} col:1} {linters:[interfacer] matchedIssueFromLinter:map[] Range:{From:163 To:167} col:1}]
I'm still not completely clear on what changed but I think it makes sense that the expanded ranges would be treated as a group for the purpose of determining unused nolint directives.
@@ -1,7 +1,7 @@ | |||
//args: -Enolintlint | |||
//config: linters-settings.nolintlint.require-explanation=true | |||
//config: linters-settings.nolintlint.require-specific=true | |||
//config: linters-settings.nolintlint.allowing-leading-space=false | |||
//config: linters-settings.nolintlint.allow-leading-space=false |
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.
noticed this typo although it didn't affect this test
7489c5a
to
6ee5247
Compare
IMO it is better to extract changing I'm even not sure that latter is better, because I'm not familiar with reasoning behind using non-spaced annotation. |
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.
LGTM
eb467e1
to
e9e8f38
Compare
I was testing and reviewing this PR, but it was merged before I finished. |
@ldez Totally my fault. I failed to notice the additional reviewer listed. Please do let me know if you find anything and I'm more than happy to make additional changes. As mentioned, I'd still like better coverage for the situation where the nolintlint issue somehow doesn't overlap the range matched to a linter, as appears to happen with the |
before the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:nakedret, gocyclo
func wantedErrors(file, short string) (errs []wantedError) { after the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:nakedreto
func wantedErrors(file, short string) (errs []wantedError) { KO before the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:nakedret,gocyclo
func wantedErrors(file, short string) (errs []wantedError) { after the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:nakedreto
func wantedErrors(file, short string) (errs []wantedError) { KO
|
before the fix: // wantedErrors parses expected errors from comments in a file.
// nolint:nakedret
func wantedErrors(file, short string) (errs []wantedError) { after the fix: // wantedErrors parses expected errors from comments in a file.
// nolint:nakedret
func wantedErrors(file, short string) (errs []wantedError) { OK before the fix: // wantedErrors parses expected errors from comments in a file.
// nolint:nakedret
func wantedErrors(file, short string) (errs []wantedError) { after the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:nakedret
func wantedErrors(file, short string) (errs []wantedError) { KO The fix removes all whitespaces, the choice to remove all the whitespaces, in this case, seems not right. |
before the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:gocyclo, nakedret
func wantedErrors(file, short string) (errs []wantedError) { after the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:nakedrett
func wantedErrors(file, short string) (errs []wantedError) { KO before the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:gocyclo, nakedret
func wantedErrors(file, short string) (errs []wantedError) { after the fix: // wantedErrors parses expected errors from comments in a file.
//nolint:nakedrett
func wantedErrors(file, short string) (errs []wantedError) { KO
|
before the fix: // wantedErrors parses expected errors from comments in a file.
// nolint:gocyclo,nakedret,whitespace
func wantedErrors(file, short string) (errs []wantedError) { or // wantedErrors parses expected errors from comments in a file.
// nolint:nakedret,gocyclo,whitespace
func wantedErrors(file, short string) (errs []wantedError) { or // wantedErrors parses expected errors from comments in a file.
// nolint:gocyclo,whitespace,nakedret
func wantedErrors(file, short string) (errs []wantedError) { results:
no fix applied. KO |
I will open an issue with all of these elements, I think that will be easier to track. |
@ldez I'll look into the mangled linter names. That's clearly a bug. Regarding multiple linters, I didn't attempt to reconcile multiple extra linters in this change. Does that seem like what's happening? Regarding leading whitespace, we could add try to preserve the user's original whitespace if we are not enforcing re-formatting. |
@ashanbrown I think that the PR has been merged too quickly, it's not the end of the world, but in our context, that means that it's impossible to create a release. Since anyone can create a release, this can be problematic. I think the best way to handle that is to revert the commit and create a new PR. WDYT? |
FYI I tested the PR with golangci-lint code: Lines 162 to 164 in aeb9830
|
Oh, sorry, missed that on review. Probably it is OK to revert it so @ashanbrown can fix issues without time pressure. |
…nolint statements (golangci#1573)" This reverts commit aeb9830.
…tatements (golangci#1573) Also allow multiple ranges to satisfy a nolint statement as having been used.
…tatements (golangci#1573) Also allow multiple ranges to satisfy a nolint statement as having been used.
This PR adds a fixing capability to
nolintlint
. It doesn't try to fix everything.What it does (when --fix is enabled):
//nolint
directives that are not used.//nolint
directive if that linter is not used for that line.//. nolint
directives for any violations on the number of spaces.What it does not do:
nolint
are required but not provided. I suppose it could add a placeholder that the user could replace but that doesn't seem really in the spirit of how the fix option is supposed to work (since the user might check in that placeholder).nolint
directive and I'd argue it's best for the user to make this determination on their own.Note that fies 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.
This PR also runs the fix on golangci-lint itself to enforce machine-readable nolint styling. (I can back this out if anyone things this isn't a good idea but I just wanted to see it in action).