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

Suggest io.SeekXXX instead of os.SEEK_XXX #302

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func (f *file) lint() {
f.lintTimeNames()
f.lintContextKeyTypes()
f.lintContextArgs()
f.lintDeprecatedConstants()
}

type link string
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/suggest/suggests

Also comments should have a period at the end (was lint run on the code? ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also merge conflicts need to be resolved.

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 {
Expand Down