-
Notifications
You must be signed in to change notification settings - Fork 373
Upgrade golangci-lint to v2 #8986
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
base: master
Are you sure you want to change the base?
Conversation
nopcoder
commented
Apr 27, 2025
•
edited
Loading
edited
- Fixed some of the lint issues - plan to enable lint on tests and more linters after this PR.
- The github-actions output format was removed - will consider to use their action to run the lint
handle some of the lint errors next PR will enable more on test code
no longer supported
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 is really neat! But some of the new(?) linter features are not so good. Please see comments inlines. I did not go over the second half, if it's OK with you I would rather finish handling these comments and then have another round.
@@ -97,15 +97,17 @@ var importCmd = &cobra.Command{ | |||
case <-ticker.C: | |||
statusResp, err = client.ImportStatusWithResponse(ctx, toURI.Repository, toURI.Ref, &apigen.ImportStatusParams{Id: importID}) | |||
DieOnErrorOrUnexpectedStatusCode(statusResp, err, http.StatusOK) | |||
status := statusResp.JSON200 | |||
if status == nil { | |||
if statusResp.JSON200 == nil { |
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 understand this "fix". It seems longer, more reasonable, and easier to get wrong when code is notified. (TBF, if this flags a "can be nil" warning, any future breakage will flag it again...)
@@ -80,7 +80,7 @@ func localInit(ctx context.Context, dir string, remote *uri.URI, force, updateIg | |||
ignoreFile, err := git.Ignore(dir, []string{dir}, []string{filepath.Join(dir, local.IndexFileName)}, local.IgnoreMarker) | |||
if err == nil { | |||
fmt.Println("Location added to", ignoreFile) | |||
} else if !(errors.Is(err, giterror.ErrNotARepository) || errors.Is(err, giterror.ErrNoGit)) { | |||
} else if !errors.Is(err, giterror.ErrNotARepository) && !errors.Is(err, giterror.ErrNoGit) { |
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.
Why is this better? Can we turn this longer off?
I definitely prefer the former (but not enough to require it). For instance, it is easier to refactor into a function isErrNotVersiomed. In general, negatives are confusing, best use as few of them as possible.
@@ -329,7 +329,7 @@ func getSyncArgs(args []string, requireRemote bool, considerGitRoot bool) (remot | |||
gitRoot, err := git.GetRepositoryPath(localPath) | |||
if err == nil { | |||
localPath = gitRoot | |||
} else if !(errors.Is(err, giterror.ErrNotARepository) || errors.Is(err, giterror.ErrNoGit)) { // allow support in environments with no git | |||
} else if !errors.Is(err, giterror.ErrNotARepository) && !errors.Is(err, giterror.ErrNoGit) { // allow support in environments with no git |
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.
Same here, don't like this lint
@@ -87,10 +87,12 @@ var setupCmd = &cobra.Command{ | |||
if err != nil { | |||
fmt.Printf("Setup failed: %s\n", err) | |||
os.Exit(1) | |||
return |
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.
Huh? Isn't os.Exit a noreturn func, like panic and a very few others?
@@ -668,7 +668,11 @@ func checkFileWasGarbageCollected(t *testing.T, presignedURLs map[string]string, | |||
if err != nil { | |||
t.Fatalf("%s, expected no error, got err=%s", "Http request to presigned url", err) | |||
} | |||
defer r.Body.Close() | |||
defer func() { | |||
if err := r.Body.Close(); err != nil { |
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 is silly for a read, because Close cannot fail. But if it did, I would want to fail the test.
Can we nolint this here, with some content?
@@ -1058,13 +1062,8 @@ func TestDeleteObjects(t *testing.T) { | |||
ctx, _, repo := setupTest(t) | |||
defer tearDownTest(repo) | |||
const numOfObjects = 10 | |||
identifiers := make([]types.ObjectIdentifier, 0, numOfObjects) |
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.
Oh wow, actual code analysis for Go. Neat!
@@ -36,7 +36,7 @@ func TestTaskResultsIterator(t *testing.T) { | |||
}, | |||
{ | |||
name: "after out of range", | |||
runID: rand.Intn(100), | |||
runID: rand.Intn(100), //nolint:gosec |
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.
Can we disable gosec in tests? Preferably just the "pseudorandom numbers" thing?
@@ -99,7 +99,7 @@ func writeFuseSymlink(c *GSClient) lua.Function { | |||
} | |||
|
|||
w := obj.NewWriter(c.ctx) | |||
w.ObjectAttrs.Metadata = map[string]string{ | |||
w.Metadata = map[string]string{ |
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.
Huh???
@@ -34,12 +34,12 @@ const ( | |||
) | |||
|
|||
type ActionStatsMockCollector struct { | |||
Hits map[string]int | |||
Hits map[string]uint64 |
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.
What? No, I thought the whole idea was to avoid unsigned arithmetic wherever possible.
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 quote some people who think they know Go:
When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.
(My emphasis)
For counters and sizes signed integers are almost always better, they avoid making silly errors such as
var (
counter, max uint64
)
max = max.MaxUint64 - 3
// ...
// Check if we have room for 5 more
if counter + 5 < max {
// Gotcha! If counter == max-1 then counter + 5 == 1 and we're in here.
}
@@ -317,7 +317,8 @@ func (c *Controller) UploadPart(w http.ResponseWriter, r *http.Request, body api | |||
|
|||
func (c *Controller) UploadPartCopy(w http.ResponseWriter, r *http.Request, | |||
body apigen.UploadPartCopyJSONRequestBody, dstRepository string, branch string, uploadID string, partNumber int, | |||
params apigen.UploadPartCopyParams) { | |||
params apigen.UploadPartCopyParams, |
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.
If different formatting is now required then gofmt should enforce it.