golang.org/x/tools@v0.21.0/go/analysis/passes/defers/testdata/src/a/a.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package a 6 7 import ( 8 "fmt" 9 "time" 10 ) 11 12 func Since() (t time.Duration) { 13 return 14 } 15 16 func x(time.Duration) {} 17 func x2(float64) {} 18 19 func good() { 20 // The following are OK because func is not evaluated in defer invocation. 21 now := time.Now() 22 defer func() { 23 fmt.Println(time.Since(now)) // OK because time.Since is not evaluated in defer 24 }() 25 evalBefore := time.Since(now) 26 defer fmt.Println(evalBefore) 27 do := func(f func()) {} 28 defer do(func() { time.Since(now) }) 29 defer fmt.Println(Since()) // OK because Since function is not in module time 30 defer copy([]int(nil), []int{1}) // check that a builtin doesn't cause a panic 31 } 32 33 type y struct{} 34 35 func (y) A(float64) {} 36 func (*y) B(float64) {} 37 func (y) C(time.Duration) {} 38 func (*y) D(time.Duration) {} 39 40 func bad() { 41 var zero time.Time 42 now := time.Now() 43 defer time.Since(zero) // want "call to time.Since is not deferred" 44 defer time.Since(now) // want "call to time.Since is not deferred" 45 defer fmt.Println(time.Since(now)) // want "call to time.Since is not deferred" 46 defer fmt.Println(time.Since(time.Now())) // want "call to time.Since is not deferred" 47 defer x(time.Since(now)) // want "call to time.Since is not deferred" 48 defer x2(time.Since(now).Seconds()) // want "call to time.Since is not deferred" 49 defer y{}.A(time.Since(now).Seconds()) // want "call to time.Since is not deferred" 50 defer (&y{}).B(time.Since(now).Seconds()) // want "call to time.Since is not deferred" 51 defer y{}.C(time.Since(now)) // want "call to time.Since is not deferred" 52 defer (&y{}).D(time.Since(now)) // want "call to time.Since is not deferred" 53 } 54 55 func ugly() { 56 // The following is ok even though time.Since is evaluated. We don't 57 // walk into function literals or check what function definitions are doing. 58 defer x((func() time.Duration { return time.Since(time.Now()) })()) 59 }