2024-03-27 23:50:12 +01:00
|
|
|
# fatcontext
|
2024-03-27 19:53:02 +01:00
|
|
|
|
2024-03-27 23:50:12 +01:00
|
|
|
`fatcontext` is a Go linter which detects potential fat contexts in loops.
|
2024-03-27 19:53:02 +01:00
|
|
|
They can lead to performance issues, as documented here: https://gabnotes.org/fat-contexts/
|
|
|
|
|
2024-04-06 09:09:43 +02:00
|
|
|
## Installation / usage
|
|
|
|
|
|
|
|
`fatcontext` is available in `golangci-lint` since v1.58.0.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
go install github.com/Crocmagnon/fatcontext/cmd/fatcontext@latest
|
|
|
|
fatcontext ./...
|
|
|
|
```
|
|
|
|
|
|
|
|
There are no specific configuration options or custom command-line flags.
|
|
|
|
|
2024-03-27 19:53:02 +01:00
|
|
|
## Example
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
func ok() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
ctx := context.WithValue(ctx, "key", i)
|
|
|
|
_ = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func notOk() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2024-03-27 23:50:12 +01:00
|
|
|
ctx = context.WithValue(ctx, "key", i) // "nested context in loop"
|
2024-03-27 19:53:02 +01:00
|
|
|
_ = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2024-03-27 22:59:32 +01:00
|
|
|
|
|
|
|
## Development
|
|
|
|
|
|
|
|
Setup pre-commit locally:
|
|
|
|
```bash
|
|
|
|
pre-commit install
|
|
|
|
```
|
|
|
|
|
|
|
|
Run tests & linter:
|
|
|
|
```bash
|
|
|
|
make lint test
|
|
|
|
```
|
|
|
|
|
|
|
|
To release, just publish a git tag:
|
|
|
|
```bash
|
|
|
|
git tag -a v0.1.0 -m "v0.1.0"
|
|
|
|
git push --follow-tags
|
|
|
|
```
|