proposal (ST1024): Add a check for redundant package aliases in import blocks #1497
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request contains a (draft) working implementation of a proposal. There's a section ("Notes") that identifies an issue with this branch and raises a question about my implementation.
Description
Suppose that:
Bar
of typefunc(string)
.Then
contains some unhelpful redundancy: the package alias, bar, adds nothing.
This branch adds a (proposed) stylistic check, ST1024, that should catch these kinds of cases.
An exception is carved out for those cases where the last component of the package path differs from the package name. An example of this is github.com/google/gofuzz whose package name is fuzz, not gofuzz.
A full working example
For example, the below triggers the proposed check.
Notes
Firstly, this is adapted from mvdan/gofumpt#296, where the impact on performance and the change of scope ruled it out.
Secondly, I created a failing test on this branch. After scratching my head for a little while I'm not sure how to resolve it. The
packages.Load
call ofexample.com/samename
returns a*package.Package
with anErrors
field that containsI'm guessing it's because of
go-tools/analysis/lint/testutil/util.go
Line 119 in a367d57
but I'm not sure what to do. The check works in my local testing with modules.
Finally, I don't know if the package data are already somehow accessible (that is, whether these calls to
packages.Load
are wasteful). I'm a little at-sea in this codebase—at least for now.