Skip to content

Conversation

@NathanBaulch
Copy link
Contributor

I went looking for a more maintainable unit test pattern for linters (as mentioned in #36) and quickly discovered the golang.org/x/tools/go/analysis package which is used by just about every other linter out there. This popular package was introduce a year after prealloc's inception.

The switch touches a lot of code but was pretty straightforward overall.

  • All code dealing with discovering and parsing .go files is gone, including all of import.go.
  • Unit tests use a comment convention to declare expected lint suggestions inline. Much easier.
  • pkg.Hint is replaced by analysis.Diagnostic.
  • Additional standardized linter CLI options are added (go/analysis is quite opinionated) for things like json output and test file exclusion.
  • Non-zero exit code 3 is always returned when suggestions are found.

I've confirmed that the same results are returned as before, and the unit tests are passing, however there are some differences in how the tool is run. This would be hidden from most users who use prealloc via golangci-lint though.

Copy link
Owner

@alexkohler alexkohler left a comment

Choose a reason for hiding this comment

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

This looks reasonable, thanks again for the PRs/cleanup! 🤝

@NathanBaulch
Copy link
Contributor Author

I've refactored the current testdata suite into one scenario per func spread across multiple files, and increased test coverage from 71.7% to 85.5% along the way. This makes each scenario self contained, no need to worry about clashing variable names, etc.

@alexkohler alexkohler merged commit 467d46b into alexkohler:master Nov 30, 2025
1 check passed
@NathanBaulch NathanBaulch deleted the xtools branch November 30, 2025 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants