Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkers: add filepath.Join checker #930

Merged
merged 1 commit into from May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions checkers/filepathJoin_checker.go
@@ -0,0 +1,50 @@
package checkers

import (
"go/ast"
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astcast"
)

func init() {
var info lintpack.CheckerInfo
info.Name = "filepathJoin"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects problems in filepath.Join() function calls"
info.Before = `filepath.Join("dir/", filename)`
info.After = `filepath.Join("dir", filename)`

collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForExpr(&filepathJoinChecker{ctx: ctx})
})
}

type filepathJoinChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}

func (c *filepathJoinChecker) VisitExpr(expr ast.Expr) {
call := astcast.ToCallExpr(expr)
if qualifiedName(call.Fun) != "filepath.Join" {
return
}

for _, arg := range call.Args {
arg, ok := arg.(*ast.BasicLit)
if ok && c.hasSeparator(arg) {
c.warnSeparator(arg)
}
}
}

func (c *filepathJoinChecker) hasSeparator(v *ast.BasicLit) bool {
return strings.ContainsAny(v.Value, `/\`)
}

func (c *filepathJoinChecker) warnSeparator(sep ast.Expr) {
c.ctx.Warn(sep, "%s contains a path separator", sep)
}
16 changes: 16 additions & 0 deletions checkers/testdata/filepathJoin/negative_tests.go
@@ -0,0 +1,16 @@
package checker_test

import (
"path/filepath"
)

func badArgs() {
Copy link
Member

Choose a reason for hiding this comment

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

are they really bad?

Copy link
Member Author

Choose a reason for hiding this comment

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

They could be better. 😅

filename := "file.go"
dir := "testdata"

_ = filepath.Join("testdata", filename)

_ = filepath.Join(dir, "file.go")

_ = filepath.Join(`testdata`, `a`, `b.txt`)
}
20 changes: 20 additions & 0 deletions checkers/testdata/filepathJoin/positive_tests.go
@@ -0,0 +1,20 @@
package checker_test

import (
"path/filepath"
)

func badArgs() {
filename := "file.go"
dir := "testdata"

/*! "testdata/" contains a path separator */
_ = filepath.Join("testdata/", filename)

/*! "/file.go" contains a path separator */
_ = filepath.Join(dir, "/file.go")

/*! `\a\b.txt` contains a path separator */
/*! `testdata\` contains a path separator */
_ = filepath.Join(`testdata\`, `\a\b.txt`)
}