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

add new rule time equal #584

Merged
merged 6 commits into from
Oct 1, 2021
Merged

add new rule time equal #584

merged 6 commits into from
Oct 1, 2021

Conversation

sina-devel
Copy link
Contributor

Closes #583

Copy link
Collaborator

@chavacava chavacava left a comment

Choose a reason for hiding this comment

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

Hi @sina-devel thanks for the PR!
I've left some comments in the code.
Please add an entry for the new rule in the README.md, and one in the RULES_DESCRIPTIONS.md

xtyp := l.file.Pkg.TypeOf(expr.X)
ytyp := l.file.Pkg.TypeOf(expr.Y)

if !isNamedType(xtyp, "time", "Time") || !isNamedType(ytyp, "time", "Time") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we could add constants for "time" and "Time"


func (l *lintTimeEqual) Visit(node ast.Node) ast.Visitor {
expr, ok := node.(*ast.BinaryExpr)
if !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here we could check if the opperators are == or !=, if is not the case we can return early.

t := time.Now()
u := t
return t == u // MATCH /use t.Equal(u) instead of "==" operator/
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

tests for other operators ( !=, <, ...) can be done in a single function

}

w := &lintTimeEqual{file, onFailure}
w.file.Pkg.TypeCheck()
Copy link
Collaborator

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 it

Copy link
Contributor Author

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.

Copy link
Collaborator

@chavacava chavacava Sep 28, 2021

Choose a reason for hiding this comment

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

this error has not been checked in other files too.

This is something that need to be fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this error has not been checked in other files too.

This is something that need to be fixed

well, i thinks return nil is good , because the Apply method not returns any error.

@@ -0,0 +1,27 @@
package pkg
Copy link
Collaborator

Choose a reason for hiding this comment

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

After configuring revive to only apply time-equal rule, when doing

./revive -config defaults.toml -formatter friendly testdata/time-equal.go

No failure is found.
This might be due to (silent) type checking errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

interesting, it works for me 🤔 😬

Copy link
Owner

@mgechev mgechev left a comment

Choose a reason for hiding this comment

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

LGTM, but I'll let @chavacava give final approval.

@chavacava chavacava merged commit 62db669 into mgechev:master Oct 1, 2021
@sina-devel sina-devel deleted the time-equal-rule branch October 1, 2021 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add new rule to suggest using time.Time.Equal instead of == and !=
3 participants