@@ -7,22 +7,25 @@ Duration Check
7
7
8
8
A Go linter to detect cases where two ` time.Duration ` values are being multiplied in possibly erroneous ways.
9
9
10
- For example, consider the following (highly contrived) function :
10
+ Consider the following (highly contrived) code :
11
11
12
12
``` go
13
- func waitFor (someDuration time .Duration ) {
14
- timeToWait := someDuration * time.Second
15
- time.Sleep (timeToWait)
13
+ func waitForSeconds (someDuration time .Duration ) {
14
+ timeToWait := someDuration * time.Second
15
+ fmt.Printf (" Waiting for %s \n " , timeToWait)
16
+ }
17
+
18
+ func main () {
19
+ waitForSeconds (5 ) // waits for 5 seconds
20
+ waitForSeconds (5 * time.Second ) // waits for 1388888h 53m 20s
16
21
}
17
22
```
18
23
19
- Although the above code would compile without any errors, its runtime behaviour would almost certainly be incorrect.
20
- A caller would reasonably expect ` waitFor(5 * time.Seconds) ` to wait for ~ 5 seconds but they would actually end up
21
- waiting for ~ 1,388,889 hours.
24
+ Both invocations of the function are syntactically correct but the second one is probably not what most people want.
25
+ In this contrived example it is quite easy to spot the mistake. However, if the incorrect ` waitForSeconds ` invocation is
26
+ nested deep within a complex piece of code that runs in the background, the mistake could go unnoticed for months (which
27
+ is exactly what happened in a production backend system of fairly well-known software service).
22
28
23
- The above example is just for illustration purposes only. The problem is glaringly obvious in such a simple function
24
- and even the greenest Gopher would discover the issue immediately. However, imagine a much more complicated function
25
- with many more lines and it is not inconceivable that such logic errors could go unnoticed.
26
29
27
30
See the [ test cases] ( testdata/src/a/a.go ) for more examples of the types of errors detected by the linter.
28
31
0 commit comments