golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
14.85k stars 1.34k forks source link

feat: add new whyvarscope linter #4791

Closed itsamirhn closed 3 weeks ago

itsamirhn commented 1 month ago

Hi,

I'd like to add a new linter: whyvarscope

It looks for variable scoped if statements and if the variable can be removed, it suggest to do that.

For example:

if r := rand.Int(); r == 0 {
    println("Lucky")
}

Could be convert to:

if rand.Int() == 0 {
    println("Lucky")
}

It also cares for some edge cases like and do not report error:

if r1, r2 := multiReturnFunc(); r1 == r2 {
    println("Equal")
}
boring-cyborg[bot] commented 1 month ago

Hey, thank you for opening your first Pull Request !

CLAassistant commented 1 month ago

CLA assistant check
All committers have signed the CLA.

ldez commented 1 month ago

In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements.

Pull Request Description

Linter

The Linter Tests Inside Golangci-lint

.golangci.next.reference.yml

Others Requirements

Recommendations


The golangci-lint team will edit this comment to check the boxes before and during the review.

The code review will start after the completion of those checkboxes (except for the specific items that the team will help to verify).

The reviews should be addressed as commits (no squash).

If the author of the PR is a member of the golangci-lint team, he should not edit this message.

This checklist does not imply that we will accept the linter.

Antonboom commented 1 month ago

Why var scope?

Sometimes this is useful for debug in IDE, especially if function result cannot be executed at breakpoint time. JFYI

ldez commented 4 weeks ago

The reviews should be addressed as commits (no squash).

itsamirhn commented 4 weeks ago

Oh my bad. I was bumping up my linter version.

ldez commented 3 weeks ago

As long as the elements required by the checklist (except those related to maintainer actions) are not handled, the review will not start.

itsamirhn commented 3 weeks ago

I'm sorry but i think I make the changes done (such as adding to the list of available linters or .gitignore) but it's still uncheck. What should i do?

ldez commented 3 weeks ago

What should i do?

please read the checklist.

ldez commented 3 weeks ago

Empty commit

I'm not a bot: the checklist is updated by hand.

So, please read the checklist.

itsamirhn commented 3 weeks ago

The linter must be added to the list of available linters (alphabetical case-insensitive order)

Is't already added and sorted?

ldez commented 3 weeks ago

The full quote:

[ ] The linter must be added to the list of available linters (alphabetical case-insensitive order).

  • enable and disable options
ldez commented 3 weeks ago

Although the items required by the checklist are still not addressed, I will provide a review.

The following examples are illustrations of what we may call false positives:

All those cases are legit because the variables are used for readability.

IMO, this linter will lead to bad practices: readability is more important than removing a local variable that the compiler already removes during compilation.

So I will decline this linter.

You can still use it as a plugin: https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint

itsamirhn commented 3 weeks ago

Thanks for your review and time.