-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
111721b
commit 62db669
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// TimeEqualRule shows where "==" and "!=" used for equality check time.Time | ||
type TimeEqualRule struct{} | ||
|
||
// Apply applies the rule to given file. | ||
func (*TimeEqualRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { | ||
var failures []lint.Failure | ||
|
||
onFailure := func(failure lint.Failure) { | ||
failures = append(failures, failure) | ||
} | ||
|
||
w := &lintTimeEqual{file, onFailure} | ||
if w.file.Pkg.TypeCheck() != nil { | ||
return nil | ||
} | ||
|
||
ast.Walk(w, file.AST) | ||
return failures | ||
} | ||
|
||
// Name returns the rule name. | ||
func (*TimeEqualRule) Name() string { | ||
return "time-equal" | ||
} | ||
|
||
type lintTimeEqual struct { | ||
file *lint.File | ||
onFailure func(lint.Failure) | ||
} | ||
|
||
func (l *lintTimeEqual) Visit(node ast.Node) ast.Visitor { | ||
expr, ok := node.(*ast.BinaryExpr) | ||
if !ok { | ||
return l | ||
} | ||
|
||
switch expr.Op { | ||
case token.EQL, token.NEQ: | ||
default: | ||
return l | ||
} | ||
|
||
xtyp := l.file.Pkg.TypeOf(expr.X) | ||
ytyp := l.file.Pkg.TypeOf(expr.Y) | ||
|
||
if !isNamedType(xtyp, "time", "Time") || !isNamedType(ytyp, "time", "Time") { | ||
return l | ||
} | ||
|
||
var failure string | ||
switch expr.Op { | ||
case token.EQL: | ||
failure = fmt.Sprintf("use %s.Equal(%s) instead of %q operator", expr.X, expr.Y, expr.Op) | ||
case token.NEQ: | ||
failure = fmt.Sprintf("use !%s.Equal(%s) instead of %q operator", expr.X, expr.Y, expr.Op) | ||
} | ||
|
||
l.onFailure(lint.Failure{ | ||
Category: "time", | ||
Confidence: 1, | ||
Node: node, | ||
Failure: failure, | ||
}) | ||
|
||
return l | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
// TestTimeEqual rule. | ||
func TestTimeEqual(t *testing.T) { | ||
testRule(t, "time-equal", &rule.TimeEqualRule{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package pkg | ||
|
||
import "time" | ||
|
||
func t() bool { | ||
t := time.Now() | ||
u := t | ||
|
||
if !t.After(u) { | ||
return t == u // MATCH /use t.Equal(u) instead of "==" operator/ | ||
} | ||
|
||
return t != u // MATCH /use !t.Equal(u) instead of "!=" operator/ | ||
} |