-
Notifications
You must be signed in to change notification settings - Fork 284
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
add new rule time equal #584
Changes from 2 commits
7b8351a
13f3707
432ef62
7a1e2b8
aa7cf04
9278b98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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} | ||
w.file.Pkg.TypeCheck() | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we could check if the opperators are |
||
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") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could add constants for |
||
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) | ||
default: | ||
return l | ||
} | ||
|
||
l.onFailure(lint.Failure{ | ||
Category: "time", | ||
Confidence: 1, | ||
Node: node, | ||
Failure: failure, | ||
}) | ||
|
||
return l | ||
} |
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{}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pkg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After configuring
No failure is found. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting, it works for me 🤔 😬 |
||
|
||
import "time" | ||
|
||
func eqlOp() bool { | ||
t := time.Now() | ||
u := t | ||
return t == u // MATCH /use t.Equal(u) instead of "==" operator/ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tests for other operators ( |
||
|
||
func eqlFun() bool { | ||
t := time.Now() | ||
u := t | ||
return t.Equal(t) | ||
} | ||
|
||
func neqlOp() bool { | ||
t := time.Now() | ||
u := t.Add(2 * time.Second) | ||
return t != u // MATCH /use !t.Equal(u) instead of "!=" operator/ | ||
} | ||
|
||
func neqlFun() bool { | ||
t := time.Now() | ||
u := t.Sub(2 * time.Second) | ||
return !t.Equal(t) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeCheck
might return an error, need to check for itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for the comments, I agree but saw that this error has not been checked in other files too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something that need to be fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, i thinks
return nil
is good , because theApply
method not returns any error.