10000 lint: remove the var decl check · golang/lint@414d861 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on May 9, 2021. It is now read-only.

Commit 414d861

Browse files
SamWhitedandybons
authored andcommitted
lint: remove the var decl check
Removes the var decl check which often fires when the type is required for documentation purposes. For example, when an enumerated list of pre-defined values of a certain type is created, it may be desirable to group those values under the type itself in the docs. If the type is infered, godoc will not group them, but if the type is manually listed they will be placed in the correct location. Since this lint is not an important stylistic consideration and frequently produces false positives, removing it is consistent with the scope of lints in this project. Fixes #429 Change-Id: I2afd928eae0bb11b39886c045556cda0771e3af5 Reviewed-on: https://go-review.googlesource.com/c/lint/+/191317 Run-TryBot: Sam Whited <sam@samwhited.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org>
1 parent 959b441 commit 414d861

File tree

2 files changed

+0
-165
lines changed

2 files changed

+0
-165
lines changed

lint.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ func (f *file) lint() {
198198
f.lintBlankImports()
199199
f.lintExported()
200200
f.lintNames()
201-
f.lintVarDecls()
202201
f.lintElses()
203202
f.lintRanges()
204203
f.lintErrorf()
@@ -977,84 +976,6 @@ var zeroLiteral = map[string]bool{
977976
"0i": true,
978977
}
979978

980-
// lintVarDecls examines variable declarations. It complains about declarations with
981-
// redundant LHS types that can be inferred from the RHS.
982-
func (f *file) lintVarDecls() {
983-
var lastGen *ast.GenDecl // last GenDecl entered.
984-
985-
f.walk(func(node ast.Node) bool {
986-
switch v := node.(type) {
987-
case *ast.GenDecl:
988-
if v.Tok != token.CONST && v.Tok != token.VAR {
989-
return false
990-
}
991-
lastGen = v
992-
return true
993-
case *ast.ValueSpec:
994-
if lastGen.Tok == token.CONST {
995-
return false
996-
}
997-
if len(v.Names) > 1 || v.Type == nil || len(v.Values) == 0 {
998-
return false
999-
}
1000-
rhs := v.Values[0]
1001-
// An underscore var appears in a common idiom for compile-time interface satisfaction,
1002-
// as in "var _ Interface = (*Concrete)(nil)".
1003-
if isIdent(v.Names[0], "_") {
1004-
return false
1005-
}
1006-
// If the RHS is a zero value, suggest dropping it.
1007-
zero := false
1008-
if lit, ok := rhs.(*ast.BasicLit); ok {
1009-
zero = zeroLiteral[lit.Value]
1010-
} else if isIdent(rhs, "nil") {
1011-
zero = true
1012-
}
1013-
if zero {
1014-
f.errorf(rhs, 0.9, category("zero-value"), "should drop = %s from declaration of var %s; it is the zero value", f.render(rhs), v.Names[0])
1015-
return false
1016-
}
1017-
lhsTyp := f.pkg.typeOf(v.Type)
1018-
rhsTyp := f.pkg.ty 10000 peOf(rhs)
1019-
1020-
if !validType(lhsTyp) || !validType(rhsTyp) {
1021-
// Type checking failed (often due to missing imports).
1022-
return false
1023-
}
1024-
1025-
if !types.Identical(lhsTyp, rhsTyp) {
1026-
// Assignment to a different type is not redundant.
1027-
return false
1028-
}
1029-
1030-
// The next three conditions are for suppressing the warning in situations
1031-
// where we were unable to typecheck.
1032-
1033-
// If the LHS type is an interface, don't warn, since it is probably a
1034-
// concrete type on the RHS. Note that our feeble lexical check here
1035-
// will only pick up interface{} and other literal interface types;
1036-
// that covers most of the cases we care to exclude right now.
1037-
if _, ok := v.Type.(*ast.InterfaceType); ok {
1038-
return false
1039-
}
1040-
// If the RHS is an untyped const, only warn if the LHS type is its default type.
1041-
if defType, ok := f.isUntypedConst(rhs); ok && !isIdent(v.Type, defType) {
1042-
return false
1043-
}
1044-
1045-
f.errorf(v.Type, 0.8, category("type-inference"), "should omit type %s from declaration of var %s; it will be inferred from the right-hand side", f.render(v.Type), v.Names[0])
1046-
return false
1047-
}
1048-
return true
1049-
})
1050-
}
1051-
1052-
func validType(T types.Type) bool {
1053-
return T != nil &&
1054-
T != types.Typ[types.Invalid] &&
1055-
!strings.Contains(T.String(), "invalid type") // good but not foolproof
1056-
}
1057-
1058979
// lintElses examines else blocks. It complains about any else block whose if block ends in a return.
1059980
func (f *file) lintElses() {
1060981
// We don't want to flag if { } else if { } else { } constructions.

testdata/var-decl.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0