| 1 | package a |
|---|---|
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "net/http" |
| 6 | ) |
| 7 | |
| 8 | func goodHTTPGet() { |
| 9 | res, err := http.Get("http://foo.com") |
| 10 | if err != nil { |
| 11 | log.Fatal(err) |
| 12 | } |
| 13 | defer res.Body.Close() |
| 14 | } |
| 15 | |
| 16 | func badHTTPGet() { |
| 17 | res, err := http.Get("http://foo.com") |
| 18 | defer res.Body.Close() // want "using res before checking for errors" |
| 19 | if err != nil { |
| 20 | log.Fatal(err) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func badHTTPHead() { |
| 25 | res, err := http.Head("http://foo.com") |
| 26 | defer res.Body.Close() // want "using res before checking for errors" |
| 27 | if err != nil { |
| 28 | log.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func goodClientGet() { |
| 33 | client := http.DefaultClient |
| 34 | res, err := client.Get("http://foo.com") |
| 35 | if err != nil { |
| 36 | log.Fatal(err) |
| 37 | } |
| 38 | defer res.Body.Close() |
| 39 | } |
| 40 | |
| 41 | func badClientPtrGet() { |
| 42 | client := http.DefaultClient |
| 43 | resp, err := client.Get("http://foo.com") |
| 44 | defer resp.Body.Close() // want "using resp before checking for errors" |
| 45 | if err != nil { |
| 46 | log.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func badClientGet() { |
| 51 | client := http.Client{} |
| 52 | resp, err := client.Get("http://foo.com") |
| 53 | defer resp.Body.Close() // want "using resp before checking for errors" |
| 54 | if err != nil { |
| 55 | log.Fatal(err) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func badClientPtrDo() { |
| 60 | client := http.DefaultClient |
| 61 | req, err := http.NewRequest("GET", "http://foo.com", nil) |
| 62 | if err != nil { |
| 63 | log.Fatal(err) |
| 64 | } |
| 65 | |
| 66 | resp, err := client.Do(req) |
| 67 | defer resp.Body.Close() // want "using resp before checking for errors" |
| 68 | if err != nil { |
| 69 | log.Fatal(err) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func badClientDo() { |
| 74 | var client http.Client |
| 75 | req, err := http.NewRequest("GET", "http://foo.com", nil) |
| 76 | if err != nil { |
| 77 | log.Fatal(err) |
| 78 | } |
| 79 | |
| 80 | resp, err := client.Do(req) |
| 81 | defer resp.Body.Close() // want "using resp before checking for errors" |
| 82 | if err != nil { |
| 83 | log.Fatal(err) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func goodUnwrapResp() { |
| 88 | unwrapResp := func(resp *http.Response, err error) *http.Response { |
| 89 | if err != nil { |
| 90 | panic(err) |
| 91 | } |
| 92 | return resp |
| 93 | } |
| 94 | resp := unwrapResp(http.Get("https://golang.org")) |
| 95 | // It is ok to call defer here immediately as err has |
| 96 | // been checked in unwrapResp (see #52661). |
| 97 | defer resp.Body.Close() |
| 98 | } |
| 99 | |
| 100 | func badUnwrapResp() { |
| 101 | unwrapResp := func(resp *http.Response, err error) string { |
| 102 | if err != nil { |
| 103 | panic(err) |
| 104 | } |
| 105 | return "https://golang.org/" + resp.Status |
| 106 | } |
| 107 | resp, err := http.Get(unwrapResp(http.Get("https://golang.org"))) |
| 108 | defer resp.Body.Close() // want "using resp before checking for errors" |
| 109 | if err != nil { |
| 110 | log.Fatal(err) |
| 111 | } |
| 112 | } |
| 113 |
Members