Repository navigation
feature: add redundant-var-decl rule - #1952
Open
mfederowicz wants to merge 1 commit into
Open
mfederowicz wants to merge 1 commit into
mfederowicz wants to merge 1 commit into
Conversation
Warns on `var x T` declarations made redundant by a later `:=` in the same block that redeclares the variable, e.g. var err error insts, err := call() The rule is typed: it only reports when removing the declaration is safe, i.e. the variable is not used before the `:=` (nor on its right side), the `:=` does not shadow it, the function has no goto, and the type the `:=` would infer is identical to the declared one. Variables of all types are checked by default; the `onlyTypes` option restricts the rule to the listed types (e.g. `["error"]`).
Member
|
The same as #1951 (comment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related #1098
Adds the typed rule
redundant-var-decl. It warns on avar x Tdeclaration (no value) when a later:=in the same blockredeclares
x, so thevarline can just be removed.Following the comment in the issue, the rule checks variables of all types, not only
err.The
only-typesoption limits it to selected types:Reported
Not reported
A declaration is only reported when removing it keeps the code compiling with the same types:
Functions containing a
gotoare skipped as well.Results on real projects
No false positives. For 52 of the 55 reports, I removed the
varline and the package still built and passedgo vet(one also needed its now-unused
ioimport removed). The other 3 are in multi-name declarations and were checked by hand.pkg/...+staging/...)The fixes from the three linked PRs are covered by
testdata/redundant_var_decl_real_world.go.Known limitation
revive resolves types only for standard library imports, so the rule can't check the types when the
:=calls a function fromanother package (in the same project or a dependency), and it stays silent. For that reason, part of the declarations removed
in the linked PRs aren't reported on those repos, including the
Disassembleexample above. On the test fixture, where the typesare known, it is reported.
Open questions
erroronly, as the issue title suggests?redundant-var-decl(all types) rather thanredundant-err-declfrom the issue?