A difference with time.Now() can be written with time.Since or time.Until

tx0/go:time-sub-nowstrictwarningstyle

What it finds

time.Now().Sub(t), which time.Since(t) says directly, and t.Sub(time.Now()), which is time.Until(t).

Since and Until are defined as these expressions. The argument of Sub is a time.Time, so time.Since(t) always fits; it evaluates t before reading the clock rather than after, so its fix is safe only when t calls nothing. For Until, the receiver must be a variable declared as a time.Time, since other types, such as those that embed one, have a Sub method too.

From the rule's source, time_sub_now.rs.

Example from the rule's tests
package p import (	"time"	clock "time") type stamp struct{ time.Time } func elapsed(start time.Time, deadline time.Time, s stamp, d time.Duration) time.Duration {	a := time.Now().Sub(start)tx0 reports: this can be written as time.Since(start)	b := deadline.Sub(clock.Now())tx0 reports: this can be written as clock.Until(deadline)	c := (time.Now()).Sub(start.Add(d))tx0 reports: this can be written as time.Since(start.Add(d))	e := s.Sub(time.Now())	f := start.Sub(deadline)	g := time.Now().Add(d).Sub(start)	return a + b + c + e + f + g} func shadowed(start time.Time) time.Duration {	time := fake{}	return time.Now().Sub(start)} type fake struct{} func (fake) Now() fake               { return fake{} }func (fake) Sub(time.Time) time.Duration { return 0 }
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:time-sub-now" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:time-sub-now] reason="..."

tx0 explain tx0/go:time-sub-now shows the same in the terminal. Found a false positive? Report it.