x/tools/go/analysis/passes/shadow/cmd/shadow: fixing shadowed errs results in code more likely to cause errors #40113
Labels
Analysis
Issues related to static analysis (vet, x/tools/go/analysis)
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Tools
This label describes issues relating to any tools in the x/tools repository.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Fixed code according to the shadow analyzer, I renamed a variable from
err
to be distinguishable from another variable also namederr
that was in a higher scope.What did you expect to see?
I expected using unique names for variables in nested scopes to be a best practice since the shadow analyzer recommends it.
What did you see instead?
I'm experiencing that renaming
err
variables in code is more likely to create more human errors than to use shadowederr
variables.In Go code my colleagues and I have seen we have definitely found value in renaming shadowed variables, it's actually caught some bugs for us on a couple occassions, but when it comes to errors the opposite has been true. On one occasion the error lived on for a while in production code. Code had been written, a shadow was detected, a variable was renamed, the scope was removed and then this was the bug and fix when we discovered the bug that had been created by the attempt to not shadow: stellar/go#2469. I've caught myself on another occasion making the same mistake. This might just be my weakness but I suspect others might make the same mistake as me.
This issue isn't about that specific issue but rather treating a shadowed
err
can easily create buggy code because developers may be accustomed to seeing the error namederr
, making it really easy to forget that in this one rare scope the error is nameddbErr
and to do things like return the error from the wrong scope.Proposal
Make
err
of typeerror
an exception to the shadow checker. The shadow checker already makes several exceptions on the basis of those patterns being common and idiomatic. This is another one of those patterns.cc @stamblerre
The text was updated successfully, but these errors were encountered: