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

Remove redundant len check around loop #27464

Merged
merged 1 commit into from
Oct 6, 2023
Merged

Remove redundant len check around loop #27464

merged 1 commit into from
Oct 6, 2023

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Oct 6, 2023

This pull request is a minor code cleanup.

From the Go specification (https://go.dev/ref/spec#For_range):

"1. For a nil slice, the number of iterations is 0."
"3. If the map is nil, the number of iterations is 0."

len returns 0 if the slice or map is nil (https://pkg.go.dev/builtin#len). Therefore, checking len(v) > 0 before a loop is unnecessary.


At the time of writing this pull request, there wasn't a lint rule that catches these issues. The closest I could find is https://staticcheck.dev/docs/checks/#S103

From the Go specification [1]:

  "1. For a nil slice, the number of iterations is 0."
  "3. If the map is nil, the number of iterations is 0."

`len` returns 0 if the slice or map is nil [2]. Therefore, checking
`len(v) > 0` around a loop is unnecessary.

[1]: https://go.dev/ref/spec#For_range
[2]: https://pkg.go.dev/builtin#len

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 6, 2023
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 6, 2023
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Oct 6, 2023
@techknowlogick
Copy link
Member

for other reviewers: I found viewing with whitespace changes hidden helpful.

@Juneezee
Copy link
Contributor Author

Juneezee commented Oct 6, 2023

@techknowlogick Yup!

image

image

@techknowlogick techknowlogick added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Oct 6, 2023
Copy link
Member

@lng2020 lng2020 left a comment

Choose a reason for hiding this comment

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

Should remove the comment if present in search query? Or just delete the whole redundant sentence if the code is easy to understand.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Oct 6, 2023
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Oct 6, 2023
@lunny lunny merged commit 13d5d2e into go-gitea:main Oct 6, 2023
25 checks passed
@GiteaBot GiteaBot added this to the 1.22.0 milestone Oct 6, 2023
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Oct 6, 2023
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Jan 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants