diff --git a/lint.go b/lint.go index fb47da00..63d96be3 100644 --- a/lint.go +++ b/lint.go @@ -210,6 +210,7 @@ func (f *file) lint() { f.lintTimeNames() f.lintContextKeyTypes() f.lintContextArgs() + f.lintDeprecatedConstants() } type link string @@ -1466,6 +1467,32 @@ func (f *file) lintContextArgs() { }) } +// deprecatedConstants keeps the mapping from a deprecated constants +// to its replacement +var deprecatedConstants = map[string]string{ + "os.SEEK_SET": "io.SeekStart", + "os.SEEK_CUR": "io.SeekCurrent", + "os.SEEK_END": "io.SeekEnd", +} + +// lintDeprecatedConstants checks for the use of a deprecated constant +// and suggest a different constant in replacement +func (f *file) lintDeprecatedConstants() { + f.walk(func(node ast.Node) bool { + switch v := node.(type) { + case *ast.SelectorExpr: + constant := fmt.Sprintf("%v.%v", v.X, v.Sel) + suggestion := deprecatedConstants[constant] + + if suggestion != "" { + f.errorf(node, 1.0, fmt.Sprintf("don't use deprecated constant %v; use %v", constant, suggestion)) + } + } + + return true + }) +} + // receiverType returns the named type of the method receiver, sans "*", // or "invalid-type" if fn.Recv is ill formed. func receiverType(fn *ast.FuncDecl) string {