Skip to content

simple: suggest replacing "t1.UnixNano() >= t2.UnixNano()" with "!t1.Before(t2)" and vice-versa #995

Open

Description

@ainar-g

(Perhaps more of a quickfix, I'm not sure.)

Apart from a couple of nanoseconds of difference in benchmarks (on my machine), they are equivalent, but one is shorter and simpler than the other.

Activity

  1. dominikh commented on May 14, 2021

    @dominikh
    Owner

    I don't believe that they are the same in the presence of monotonic time.

  2. added and removed
    needs-triageNewly filed issue that needs triage
    on May 14, 2021
  3. ainar-g commented on May 14, 2021

    @ainar-g
    Author

    Well, in the code where I've seen this pattern, either one or both of t1 and t2 were decoded from somewhere, which means that the monotonic clock isn't really an issue, as monotonic clock comparison is only used when both values have monotonic clock readings.

    In fact, I'm pretty sure that that makes the UnixNano form worse, because if you compare two values with monotonic clock readings by stripping those readings away with UnixNano, you're up for trouble eventually. That's why the monotonic clock stuff is even in there in the first place.

    Although we would need a more advanced tracing of value origins to make it 100 % accurate (pta?), I'd say that this is a simple check in case of non-monotonic comparison and a staticcheck one when both are monotonic.

  4. dominikh commented on May 14, 2021

    @dominikh
    Owner

    In fact, I'm pretty sure that that makes the UnixNano form worse, because if you compare two values with monotonic clock readings by stripping those readings away with UnixNano, you're up for trouble eventually

    Unless your intention is to explicitly do that (for example to check if the system clock went backwards.) We can't tell intentional stripping from accidental stripping.

    I'd say that this is a simple check in case of non-monotonic comparison

    To avoid false positives, we could only flag values that we are certain have no monotonic time. Doing so the naive way will rarely ever produce true positives, and doing it the complex way doesn't seem worth it to me.

    I don't think this is the right place to enforce this. You might want to write your own rule using semgrep or ruleguard if you want to ban this pattern.

  5. added
    aggressiveA set of checks that is more prone to false positives but is helpful during code review
    needs-decisionWe have to decide if this check is feasible and desirable
    on Oct 17, 2021
  6. dominikh commented on Oct 17, 2021

    @dominikh
    Owner

    Reopening under the aggressive label, for a future set of checks specific to code review.

  7. added this to the Staticcheck 2022.2 milestone on Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    aggressiveA set of checks that is more prone to false positives but is helpful during code reviewneeds-decisionWe have to decide if this check is feasible and desirablenew-check

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions