Repository: mgechev/revive (go)

Issue:
`time-equal` garbled message when time returned from function
**Describe the bug**
The `time-equal` rule passes an `ast.Expr` to a `%s` placeholder in its message format, but not all `ast.Expr` implement `String()`, so junk is output for certain kinds of expressions (such as comparing a `time.Time` that's returned from a function call).

**To Reproduce**
Steps to reproduce the behavior:
1. I updated revive `go install github.com/mgechev/revive@latest`
2. I run it with the following flags & configuration file:

```shell
revive -config revive.toml time-equal-test.go
```

```toml
# config file
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.0
errorCode = 0
warningCode = 0

[rule.time-equal]
```

Source file `time-equal-test.go`:

```go
package test

import "time"

func isNow(t time.Time) bool { return t == time.Now() }

```

**Expected behavior**
Something more readable than:

```
time-equal-test.go:5:39: use t.Equal(&{%!s(*ast.SelectorExpr=&{0xc00007e7a0 0xc00007e7c0}) %!s(token.Pos=81) [] %!s(token.Pos=0) %!s(token.Pos=82)}) instead of "==" operator
```

Pull request description:
fix #846: time-equal garbled message when time returned from function
Closes #846 


Patch:
diff --git a/rule/time-equal.go b/rule/time-equal.go
index 72ecf26..3b85e18 100644
--- a/rule/time-equal.go
+++ b/rule/time-equal.go
@@ -60,9 +60,9 @@ func (l *lintTimeEqual) Visit(node ast.Node) ast.Visitor {
 	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)
+		failure = fmt.Sprintf("use %s.Equal(%s) instead of %q operator", gofmt(expr.X), gofmt(expr.Y), expr.Op)
 	case token.NEQ:
-		failure = fmt.Sprintf("use !%s.Equal(%s) instead of %q operator", expr.X, expr.Y, expr.Op)
+		failure = fmt.Sprintf("use !%s.Equal(%s) instead of %q operator", gofmt(expr.X), gofmt(expr.Y), expr.Op)
 	}
 
 	l.onFailure(lint.Failure{


Test patch:
diff --git a/testdata/time-equal.go b/testdata/time-equal.go
index be72c4e..f31a05a 100644
--- a/testdata/time-equal.go
+++ b/testdata/time-equal.go
@@ -12,3 +12,7 @@ func t() bool {
 
 	return t != u // MATCH /use !t.Equal(u) instead of "!=" operator/
 }
+
+// issue #846
+func isNow(t time.Time) bool    { return t == time.Now() } // MATCH /use t.Equal(time.Now()) instead of "==" operator/
+func isNotNow(t time.Time) bool { return time.Now() != t } // MATCH /use !time.Now().Equal(t) instead of "!=" operator/