Skip to content

feature: add redundant-var-decl rule - #1952

Open
mfederowicz wants to merge 1 commit into
revive-lint:masterfrom
mfederowicz:feature/redundant-var-decl
Open

mfederowicz wants to merge 1 commit into
revive-lint:masterfrom
mfederowicz:feature/redundant-var-decl

Conversation

@mfederowicz

@mfederowicz mfederowicz commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Related #1098

Adds the typed rule redundant-var-decl. It warns on a var x T declaration (no value) when a later := in the same block
redeclares x, so the var line can just be removed.

Following the comment in the issue, the rule checks variables of all types, not only err.
The only-types option limits it to selected types:

[rule.redundant-var-decl]
arguments = [{ only-types = ["error"] }]

Reported

func (s *RPCServer) Disassemble(args DisassembleRequest, answer *api.AsmInstructions) error {
	var err error // redundant declaration of "err": it is redeclared by := on line 3; remove the var declaration
	insts, err := s.debugger.Disassemble(args.Scope.GoroutineID, args.StartPC, args.EndPC)
	// ...
}

var foo int // redundant declaration of "foo"
foo, bar := 2, 3

Not reported

A declaration is only reported when removing it keeps the code compiling with the same types:

var err error
process(err) // used before the :=
insts, err := call()

var err error
if insts, err := call(); err != nil { // shadows err instead of reusing it
	// ...
}

var err error
insts, err := callMyErr() // returns *MyErr: err would become a concrete pointer

var r io.Reader
r, err := os.Open(name) // r would become *os.File

var n int64
n, ok := 2, true // n would become int

Functions containing a goto are skipped as well.

Results on real projects

No false positives. For 52 of the 55 reports, I removed the var line and the package still built and passed go vet
(one also needed its now-unused io import removed). The other 3 are in multi-name declarations and were checked by hand.

Project Reports
delve, before go-delve/delve#3855 6
gitea, before go-gitea/gitea#32381 1
mockery, before vektra/mockery#829 5
moby 23
kubernetes (pkg/... + staging/...) 19
go-github 1

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 from
another 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 Disassemble example above. On the test fixture, where the types
are known, it is reported.

Open questions

  • Is checking all types by default fine, or should the default be error only, as the issue title suggests?
  • Name: redundant-var-decl (all types) rather than redundant-err-decl from the issue?

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"]`).
@alexandear

Copy link
Copy Markdown
Member

The same as #1951 (comment)

@alexandear alexandear added the blocked Needs a direct action from a maintainer. label Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocked Needs a direct action from a maintainer.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants