Skip to content

Conversation

mmorel-35
Copy link
Contributor

@mmorel-35 mmorel-35 commented May 25, 2025

Description

This enable and fixes shadow rule from govet.

@mmorel-35 mmorel-35 force-pushed the govet/shadow branch 4 times, most recently from 1fb5fbb to 4abc98f Compare May 25, 2025 12:51
@mmorel-35 mmorel-35 requested a review from ccoVeille May 25, 2025 13:21
@mmorel-35 mmorel-35 force-pushed the govet/shadow branch 2 times, most recently from bb5cdd2 to cf19b6d Compare May 25, 2025 13:35
@mmorel-35 mmorel-35 force-pushed the govet/shadow branch 4 times, most recently from 0428808 to fe79a5c Compare May 26, 2025 18:18
@shirou
Copy link
Owner

shirou commented Jun 7, 2025

Sorry for the delayed review.

I’m in favor of adding linting for shadowing. However, after reading this PR twice, I still feel that applying it to err might be overkill.

For example, take the following code:

if err := common.Sleep(ctx, interval); err != nil {

Changing it to:

err := common.Sleep(ctx, interval)
if err != nil {

feels a bit much.

The former is a common idiom in Go and makes it clear that we're only dealing with the err returned by common.Sleep, which seems preferable.

Since err is such a commonly used variable name in Go, I feel like some amount of shadowing is unavoidable. Is there a way to configure the linter to ignore shadowing specifically for err?

@mmorel-35
Copy link
Contributor Author

mmorel-35 commented Jun 7, 2025

I'm wondering if using this form will work

if err := common.Sleep(ctx, interval); err != nil {

Let's give this a try

@ccoVeille
Copy link
Contributor

It works. The issue comes often from the very first err that is define in the scope, that implies everything else will be reported as shadowed.

The solution is to use the if err := whatever(); err != nil { everywhere

@mmorel-35 mmorel-35 force-pushed the govet/shadow branch 3 times, most recently from defd80f to d155442 Compare June 7, 2025 12:04
@ccoVeille
Copy link
Contributor

Since err is such a commonly used variable name in Go, I feel like some amount of shadowing is unavoidable. Is there a way to configure the linter to ignore shadowing specifically for err?

The solution is described here

@mmorel-35 mmorel-35 force-pushed the govet/shadow branch 13 times, most recently from 861306d to 2bc9ec4 Compare June 7, 2025 18:08
Copy link
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

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

Sorry for the very late reply. I've been thinking about this for a while. It's true that detecting variable shadowing has many advantages, however I still believe that universally disallowing shadowing of err hurts code readability.
We've had various discussions and you've made several changes to the PR, but I don't think my stance on this has changed. I truly appreciate your dedicated contributions.

@mmorel-35 mmorel-35 force-pushed the govet/shadow branch 2 times, most recently from e00867a to 13b3d0c Compare July 5, 2025 18:06
@mmorel-35 mmorel-35 force-pushed the govet/shadow branch 2 times, most recently from 72a3411 to 484de74 Compare July 5, 2025 18:14
Comment on lines +144 to +148
rules:
- text: 'shadow: declaration of "err" shadows declaration at line'
linters:
- govet
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed @ccoVeille advice and rolled-back err related changes. Shall I just close this PR or does it fits better your expectations @shirou ?

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Copy link
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

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

Thank you for the thoughtful discussion and updates. I've reviewed the changes, and I believe they are now clear and understandable. I will go ahead and merge the PR.

@shirou shirou merged commit e240180 into shirou:master Jul 10, 2025
50 of 52 checks passed
@mmorel-35 mmorel-35 deleted the govet/shadow branch July 10, 2025 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants