github.com/nozzle/golangci-lint@v1.49.0-nz3/test/testdata/noctx.go (about)

     1  //golangcitest:args -Enoctx
     2  package testdata
     3  
     4  import (
     5  	"context"
     6  	"net/http"
     7  )
     8  
     9  var newRequestPkg = http.NewRequest
    10  
    11  func Noctx() {
    12  	const url = "http://example.com"
    13  	cli := &http.Client{}
    14  
    15  	ctx := context.Background()
    16  	http.Get(url) // want `net/http\.Get must not be called`
    17  	_ = http.Get  // OK
    18  	f := http.Get // OK
    19  	f(url)        // want `net/http\.Get must not be called`
    20  
    21  	http.Head(url)          // want `net/http\.Head must not be called`
    22  	http.Post(url, "", nil) // want `net/http\.Post must not be called`
    23  	http.PostForm(url, nil) // want `net/http\.PostForm must not be called`
    24  
    25  	cli.Get(url) // want `\(\*net/http\.Client\)\.Get must not be called`
    26  	_ = cli.Get  // OK
    27  	m := cli.Get // OK
    28  	m(url)       // want `\(\*net/http\.Client\)\.Get must not be called`
    29  
    30  	cli.Head(url)          // want `\(\*net/http\.Client\)\.Head must not be called`
    31  	cli.Post(url, "", nil) // want `\(\*net/http\.Client\)\.Post must not be called`
    32  	cli.PostForm(url, nil) // want `\(\*net/http\.Client\)\.PostForm must not be called`
    33  
    34  	req, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
    35  	cli.Do(req)
    36  
    37  	req2, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) // OK
    38  	cli.Do(req2)
    39  
    40  	req3, _ := http.NewRequest(http.MethodPost, url, nil) // OK
    41  	req3 = req3.WithContext(ctx)
    42  	cli.Do(req3)
    43  
    44  	f2 := func(req *http.Request, ctx context.Context) *http.Request {
    45  		return req
    46  	}
    47  	req4, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
    48  	req4 = f2(req4, ctx)
    49  
    50  	req41, _ := http.NewRequest(http.MethodPost, url, nil) // OK
    51  	req41 = req41.WithContext(ctx)
    52  	req41 = f2(req41, ctx)
    53  
    54  	newRequest := http.NewRequest
    55  	req5, _ := newRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
    56  	cli.Do(req5)
    57  
    58  	req51, _ := newRequest(http.MethodPost, url, nil) // OK
    59  	req51 = req51.WithContext(ctx)
    60  	cli.Do(req51)
    61  
    62  	req52, _ := newRequestPkg(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
    63  	cli.Do(req52)
    64  
    65  	type MyRequest = http.Request
    66  	f3 := func(req *MyRequest, ctx context.Context) *MyRequest {
    67  		return req
    68  	}
    69  	req6, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
    70  	req6 = f3(req6, ctx)
    71  
    72  	req61, _ := http.NewRequest(http.MethodPost, url, nil) // OK
    73  	req61 = req61.WithContext(ctx)
    74  	req61 = f3(req61, ctx)
    75  
    76  	type MyRequest2 http.Request
    77  	f4 := func(req *MyRequest2, ctx context.Context) *MyRequest2 {
    78  		return req
    79  	}
    80  	req7, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
    81  	req71 := MyRequest2(*req7)
    82  	f4(&req71, ctx)
    83  
    84  	req72, _ := http.NewRequest(http.MethodPost, url, nil) // OK
    85  	req72 = req72.WithContext(ctx)
    86  	req73 := MyRequest2(*req7)
    87  	f4(&req73, ctx)
    88  
    89  	req8, _ := func() (*http.Request, error) {
    90  		return http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
    91  	}()
    92  	cli.Do(req8)
    93  
    94  	req82, _ := func() (*http.Request, error) {
    95  		req82, _ := http.NewRequest(http.MethodPost, url, nil) // OK
    96  		req82 = req82.WithContext(ctx)
    97  		return req82, nil
    98  	}()
    99  	cli.Do(req82)
   100  
   101  	f5 := func(req, req2 *http.Request, ctx context.Context) (*http.Request, *http.Request) {
   102  		return req, req2
   103  	}
   104  	req9, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
   105  	req9, _ = f5(req9, req9, ctx)
   106  
   107  	req91, _ := http.NewRequest(http.MethodPost, url, nil) // OK
   108  	req91 = req91.WithContext(ctx)
   109  	req9, _ = f5(req91, req91, ctx)
   110  
   111  	req10, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
   112  	req11, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
   113  	req10, req11 = f5(req10, req11, ctx)
   114  
   115  	req101, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
   116  	req111, _ := http.NewRequest(http.MethodPost, url, nil) // OK
   117  	req111 = req111.WithContext(ctx)
   118  	req101, req111 = f5(req101, req111, ctx)
   119  
   120  	func() (*http.Request, *http.Request) {
   121  		req12, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
   122  		req13, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
   123  		return req12, req13
   124  	}()
   125  
   126  	func() (*http.Request, *http.Request) {
   127  		req14, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
   128  		req15, _ := http.NewRequest(http.MethodPost, url, nil) // OK
   129  		req15 = req15.WithContext(ctx)
   130  
   131  		return req14, req15
   132  	}()
   133  }
   134