Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: charithe/durationcheck
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.8
Choose a base ref
...
head repository: charithe/durationcheck
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.0.9
Choose a head ref
  • 2 commits
  • 3 files changed
  • 2 contributors

Commits on Sep 21, 2021

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ec9cf88 View commit details

Commits on Oct 9, 2021

  1. Fix false positive (#21)

    bunyk authored Oct 9, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c941235 View commit details
Showing with 18 additions and 10 deletions.
  1. +13 −10 README.md
  2. +3 −0 durationcheck.go
  3. +2 −0 testdata/src/a/a.go
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -7,22 +7,25 @@ Duration Check

A Go linter to detect cases where two `time.Duration` values are being multiplied in possibly erroneous ways.

For example, consider the following (highly contrived) function:
Consider the following (highly contrived) code:

```go
func waitFor(someDuration time.Duration) {
timeToWait := someDuration * time.Second
time.Sleep(timeToWait)
func waitForSeconds(someDuration time.Duration) {
timeToWait := someDuration * time.Second
fmt.Printf("Waiting for %s\n", timeToWait)
}

func main() {
waitForSeconds(5) // waits for 5 seconds
waitForSeconds(5 * time.Second) // waits for 1388888h 53m 20s
}
```

Although the above code would compile without any errors, its runtime behaviour would almost certainly be incorrect.
A caller would reasonably expect `waitFor(5 * time.Seconds)` to wait for ~5 seconds but they would actually end up
waiting for ~1,388,889 hours.
Both invocations of the function are syntactically correct but the second one is probably not what most people want.
In this contrived example it is quite easy to spot the mistake. However, if the incorrect `waitForSeconds` invocation is
nested deep within a complex piece of code that runs in the background, the mistake could go unnoticed for months (which
is exactly what happened in a production backend system of fairly well-known software service).

The above example is just for illustration purposes only. The problem is glaringly obvious in such a simple function
and even the greenest Gopher would discover the issue immediately. However, imagine a much more complicated function
with many more lines and it is not inconceivable that such logic errors could go unnoticed.

See the [test cases](testdata/src/a/a.go) for more examples of the types of errors detected by the linter.

3 changes: 3 additions & 0 deletions durationcheck.go
Original file line number Diff line number Diff line change
@@ -150,6 +150,9 @@ func isAcceptableNestedExpr(pass *analysis.Pass, n ast.Expr) bool {
case *ast.Ident:
return isAcceptableIdent(pass, e)
case *ast.CallExpr:
if isAcceptableCast(pass, e) {
return true
}
t := pass.TypesInfo.TypeOf(e)
return !isDuration(t)
case *ast.SelectorExpr:
2 changes: 2 additions & 0 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -74,6 +74,8 @@ func validCases() {
_ = time.Second * b.SomeInt

_ = time.Duration(intArr[0]) * time.Second

_ = time.Duration(y) * 24 * time.Hour
}

func invalidCases() {