Skip to content

Commit

Permalink
fixes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
fexolm committed May 14, 2018
1 parent c980edf commit 46c5539
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/kfulint/testdata/underef/checker_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func sampleCase2(k sampleInterface) {
func sampleCase3() {
var k *[5]int

//TODO: could simplify (*k)[5] to k[5]
///: could simplify (*k)[2] to k[2]
(*k)[2] = 3
}

Expand Down
38 changes: 36 additions & 2 deletions lint/underef_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,32 @@ func (c *UnderefChecker) Check(f *ast.File) {
}
if expr, ok := expr.X.(*ast.StarExpr); ok {
if c.checkStarExpr(expr) {
c.warn(n)
c.warnSelect(n)
return false
}
}
case *ast.IndexExpr:
expr, ok := n.X.(*ast.ParenExpr)
if !ok {
return true
}
if expr, ok := expr.X.(*ast.StarExpr); ok {
if !c.checkStarExpr(expr) {
return true
}
if c.checkArray(expr) {
c.warnArray(n)
}
}
}

return true
})
}
}

// TODO add () to function output.
func (c *UnderefChecker) warn(expr *ast.SelectorExpr) {
func (c *UnderefChecker) warnSelect(expr *ast.SelectorExpr) {
c.ctx.addWarning(Warning{
Kind: "underef",
Node: expr,
Expand All @@ -60,6 +74,17 @@ func (c *UnderefChecker) warn(expr *ast.SelectorExpr) {
})
}

func (c *UnderefChecker) warnArray(expr *ast.IndexExpr) {
c.ctx.addWarning(Warning{
Kind: "underef",
Node: expr,
Text: fmt.Sprintf("could simplify %s to %s[%s]",
nodeString(c.ctx.FileSet, expr),
nodeString(c.ctx.FileSet, expr.X.(*ast.ParenExpr).X.(*ast.StarExpr).X),
nodeString(c.ctx.FileSet, expr.Index)),
})
}

// checkStarExpr checks if ast.StarExpr could be simplified
func (c *UnderefChecker) checkStarExpr(expr *ast.StarExpr) bool {
// checks if expr is pointer type
Expand All @@ -75,3 +100,12 @@ func (c *UnderefChecker) checkStarExpr(expr *ast.StarExpr) bool {

return true
}

func (c *UnderefChecker) checkArray(expr *ast.StarExpr) bool {
typ, ok := c.ctx.TypesInfo.TypeOf(expr.X).(*types.Pointer)
if !ok {
return false
}
_, ok = typ.Elem().(*types.Array)
return ok
}

0 comments on commit 46c5539

Please sign in to comment.