github.com/serversong/goreporter@v0.0.0-20200325104552-3cfaf44fd178/linters/staticcheck/testdata/CheckEarlyDefer.go (about)

     1  package pkg
     2  
     3  import "io"
     4  
     5  func fn1() (io.ReadCloser, error) {
     6  	return nil, nil
     7  }
     8  
     9  type T struct {
    10  	rc io.ReadCloser
    11  }
    12  
    13  func fn3() (T, error) {
    14  	return T{}, nil
    15  }
    16  
    17  func fn2() {
    18  	rc, err := fn1()
    19  	defer rc.Close() // MATCH /should check returned error before deferring rc.Close/
    20  	if err != nil {
    21  		println()
    22  	}
    23  
    24  	rc, _ = fn1()
    25  	defer rc.Close()
    26  
    27  	rc, err = fn1()
    28  	if err != nil {
    29  		println()
    30  	}
    31  	defer rc.Close()
    32  
    33  	t, err := fn3()
    34  	defer t.rc.Close() // MATCH /should check returned error before deferring t.rc.Close/
    35  	if err != nil {
    36  		println()
    37  	}
    38  }