aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/kkHAIKE/contextcheck/README.md
blob: 105b2de5a1d5096ce900cfd16bc9aa6d0dd3b757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
[![CircleCI](https://circleci.com/gh/sylvia7788/contextcheck.svg?style=svg)](https://circleci.com/gh/sylvia7788/contextcheck)


# contextcheck

`contextcheck` is a static analysis tool used to check whether a function uses a non-inherited context that could result in a broken call link.

For example:

```go
func call1(ctx context.Context) {
    ...

    ctx = getNewCtx(ctx)
    call2(ctx) // OK

    call2(context.Background()) // Non-inherited new context, use function like `context.WithXXX` instead

    call3() // Function `call3` should pass the context parameter
    call4() // Function `call4->call3` should pass the context parameter
    ...
}

func call2(ctx context.Context) {
    ...
}

func call3() {
    ctx := context.TODO()
    call2(ctx)
}

func call4() {
    call3()
}


// if you want none-inherit ctx, use this function
func getNewCtx(ctx context.Context) (newCtx context.Context) {
    ...
    return
}

/* ---------- check net/http.HandleFunc ---------- */

func call5(ctx context.Context, w http.ResponseWriter, r *http.Request) {
}

func call6(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	call5(ctx, w, r)
	call5(context.Background(), w, r) // Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead
}

func call7(in bool, w http.ResponseWriter, r *http.Request) {
	call5(r.Context(), w, r)
	call5(context.Background(), w, r)
}

func call8() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		call5(r.Context(), w, r)
		call5(context.Background(), w, r) // Non-inherited new context, use function like `context.WithXXX` or `r.Context` instead

		call6(w, r)

		// call7 should be like `func call7(ctx context.Context, in bool, w http.ResponseWriter, r *http.Request)`
		call7(true, w, r) // Function `call7` should pass the context parameter
	})
}
```

## Tips
### need break ctx inheritance
eg: [issue](https://github.com/kkHAIKE/contextcheck/issues/2).

```go
func call1(ctx context.Context) {
    ...

    newCtx, cancel := NoInheritCancel(ctx)
    defer cancel()

    call2(newCtx)
    ...
}

func call2(ctx context.Context) {
    ...
}

func NoInheritCancel(_ context.Context) (context.Context,context.CancelFunc) {
  return context.WithCancel(context.Background())
}
```

### skip the check for the specified function
To skip this linter in some false-positive cases, you can add // nolint: contextcheck to the function declaration's comment.

```go
// nolint: contextcheck
func call1() {
    doSomeThing(context.Background()) // add nolint will no issuss for that
}

func call2(ctx context.Context) {
    call1()
}

func call3() {
    call2(context.Background())
}
```

### force the marking of a specified function as having a server-side http.Request parameter
The default behavior is to mark `http.HandlerFunc` or any function that uses `r.Context()`.

```go
// @contextcheck(req_has_ctx)
func writeErr(w http.ResponseWriter, r *http.Request, err error) {
    doSomeThing(r.Context())
}

func handler(w http.ResponseWriter, r *http.Request) {
    ...
    if err != nil {
        writeErr(w, r, err)
        return
    }
    ...
}
```

## Installation

You can get `contextcheck` by `go get` command.

```bash
$ go get -u github.com/kkHAIKE/contextcheck
```

or build yourself.

```bash
$ make build
$ make install
```

## Usage

Invoke `contextcheck` with your package name

```bash
$ contextcheck ./...
$ # or
$ go vet -vettool=`which contextcheck` ./...
```