Skip to content
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

Allow custom linters to auto-fix suggested fixes #2300

Closed
wants to merge 1 commit into from

Conversation

StevenACoffman
Copy link
Contributor

@StevenACoffman StevenACoffman commented Oct 19, 2021

This allows custom linters hook into the --fix functionality to resolve #1728
Custom linters specify the fixes using the Go analysis structures,
which allow for arbitrary char offsets for fixes; they get converted
into golangci structures, which are line-based. If the conversion is
not possible, the fix is dropped on the floor.

This also potentially partially addresses #1779 for those fixes that can be converted to line-based. For those that cannot, a modification to golangci structures is still necessary.

Current linters that support SuggestedFixes:

  • goerr113
  • ruleguard
  • staticcheck
  • nlreturn
  • exportloopref
  • exhaustive
  • govet

This work is a cherry-pick of Khan@b48d220 by @dbraley and @csilvers
Signed-off-by: Steve Coffman steve@khanacademy.org

@StevenACoffman StevenACoffman changed the title Allow custom linters to auto-fix Allow custom linters to auto-fix suggested fixes Oct 19, 2021
@StevenACoffman
Copy link
Contributor Author

The lint failure for is for pkg/golinters/gocritic.go which was untouched by this and has this line:

//nolint:exhaustive // only 3 types (int, bool, and string) are supported by CheckerParam.Value

@StevenACoffman StevenACoffman added the enhancement New feature or improvement label Oct 19, 2021
@csilvers
Copy link
Contributor

csilvers commented Oct 20, 2021

This commit is against an old version of golangci-lint. I'm working on updating it to golangci-lint-main -- the code is the same, just in different files now. oops my mistake, I see @StevenACoffman already did that updating for me!

@ldez ldez added the blocked Need's direct action from maintainer label Oct 20, 2021
This allows custom linters hook into the `--fix` functionality.
Custom linters specify the fixes using the Go analysis structures,
which allow for arbitrary char offsets for fixes; they get converted
into golangci structures, which are line-based. If the conversion is
not possible, the fix is dropped on the floor.

Signed-off-by: Steve Coffman <steve@khanacademy.org>
@@ -116,7 +116,7 @@ func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string
// but the file parsers (TOML, YAML, JSON) don't create the same representation for raw type.
// then we have to convert value types into the expected value types.
// Maybe in the future, this kind of conversion will be done in go-critic itself.
//nolint:exhaustive // only 3 types (int, bool, and string) are supported by CheckerParam.Value
//nolint:exhaustive,nolintlint // only 3 types (int, bool, and string) are supported by CheckerParam.Value
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without nolintlint this is incorrectly identified as unused, but removing the entire comment correctly causes exhaustive linter to fail. This seems unrelated to my changes, so I added this to unblock.

@StevenACoffman StevenACoffman removed the blocked Need's direct action from maintainer label Oct 21, 2021
@ldez ldez added the blocked Need's direct action from maintainer label Oct 22, 2021
@ldez
Copy link
Member

ldez commented Oct 22, 2021

I don't have the time now to explain why, but this PR is blocked.
I will explain the reason as soon as possible.
Please do not remove the label and do not merge the PR, for now.

@StevenACoffman
Copy link
Contributor Author

Thanks for letting me know. I know you are busy so I can wait until you have more time. I saw your ldez@28b5be3 so I assume you have some other unpushed commits and have something else in mind.

@dbraley
Copy link
Contributor

dbraley commented Oct 27, 2021

@ldez @StevenACoffman If there's anything I can do here to help out, please let me know. I'm only working half time for the next month or so, but glad to put in some time where I can find it to clean this PR up if needed, or adjust it to an alternate design if desired.

@StevenACoffman
Copy link
Contributor Author

@ldez @SVilgelm Can you please explain why this was marked blocked? Please describe a path forward or when one might become available?

@ldez
Copy link
Member

ldez commented Nov 5, 2021

I think that the real solution is to add support for SuggestedFixes (#1779) to replace the current/old system.

I'm still working on it but, for me, it's a part of the v2.

I'll need to take time to think about a v2 plan.
I hope I will have time this week to work on a proposal for this plan.

@StevenACoffman
Copy link
Contributor Author

Thanks for your update. While this PR works well as is, it will not be merged at this time, as a more sweeping redesign is being planned by a maintainer who has a track record of past excellent contributions.

I'll close the PR, and we'll continue to carry it in our fork, so there's no need to further rush your design or add stress to your volunteer commitment. Again, sorry to have troubled you. Feel free to ping me or anyone at Khan if you reconsider.

@StevenACoffman
Copy link
Contributor Author

For other people's future reference, an alternative implementation for applying suggested fixes is here: https://github.com/golang/tools/blob/54dc8c5edb561003330ec7e649333181bbd0a9bd/go/analysis/internal/checker/checker.go#L316

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Need's direct action from maintainer enhancement New feature or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom Linter vs --fix
4 participants