mirror of
https://github.com/Crocmagnon/fatcontext.git
synced 2025-04-09 11:06:33 +02:00
Compare commits
109 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bc6bc802c2 | ||
ecae7266e2 | |||
![]() |
134c7a8397 | ||
e5c81ff0dd | |||
![]() |
a3efb8903a | ||
![]() |
96ab752057 | ||
![]() |
2cfdadfd85 | ||
![]() |
5a001f2fbe | ||
![]() |
6f8b43553d | ||
![]() |
3947c6f8d5 | ||
![]() |
b568e8acf1 | ||
![]() |
ed135bb8e7 | ||
![]() |
12e5409dea | ||
![]() |
2046ce80db | ||
![]() |
7b0afb1f92 | ||
f887074f5d | |||
ef9d47d1f0 | |||
939d65bc16 | |||
54e593c1c6 | |||
529e088561 | |||
![]() |
52f7fb588c | ||
![]() |
6130ad946e | ||
![]() |
48ee2433d6 | ||
![]() |
4c828f7302 | ||
![]() |
632a706303 | ||
![]() |
98578576b8 | ||
![]() |
4ad817c8f3 | ||
![]() |
fae7a27f40 | ||
![]() |
fbf73fbd4d | ||
![]() |
ed98e56f00 | ||
![]() |
7f2b12beab | ||
![]() |
9b1b0c8986 | ||
![]() |
1c05d23bb3 | ||
![]() |
e1e94fa7d4 | ||
![]() |
db40be2dee | ||
![]() |
0c76b071d6 | ||
![]() |
cddb074802 | ||
![]() |
548d1beeac | ||
![]() |
7e1e279ad5 | ||
![]() |
88173f8b3f | ||
![]() |
4dc5fc8817 | ||
![]() |
25eb4fa74b | ||
![]() |
4526c31d1d | ||
![]() |
8782199988 | ||
![]() |
4410b65005 | ||
90afb8d3fa | |||
021f1b4ed6 | |||
![]() |
be0aa70f23 | ||
0d2c4019d4 | |||
77afd24616 | |||
2e1ec44b79 | |||
0be9888cea | |||
![]() |
ab55c271f9 | ||
46a445090d | |||
![]() |
3df83da91f | ||
a80e8ddef6 | |||
30606c7931 | |||
![]() |
091030580e | ||
f35e8a2263 | |||
40f87bfc57 | |||
bf0cfec130 | |||
c6325891c1 | |||
![]() |
9a9e950a91 | ||
![]() |
602eb047a9 | ||
a2ca606f30 | |||
![]() |
802ac1311c | ||
e8053df661 | |||
![]() |
69e9ae12fc | ||
![]() |
0e13d068ad | ||
![]() |
3e9e29f41c | ||
6be4ab74b8 | |||
![]() |
db036b18d4 | ||
9beb365772 | |||
![]() |
954c9b64c6 | ||
acb08d4731 | |||
![]() |
66526fe956 | ||
d5171e1d54 | |||
![]() |
de95617005 | ||
e7f3a75701 | |||
ec963af14f | |||
da706a0193 | |||
![]() |
0b0c70a0a6 | ||
6a0a70a6a3 | |||
![]() |
b6bd36f685 | ||
5cfbe16927 | |||
4c2ad68b8d | |||
![]() |
a0fa7dbcdc | ||
a2187f17e7 | |||
95e882dd7d | |||
25cb4f05b9 | |||
781d685146 | |||
a7ef75e6e9 | |||
da0fa3f168 | |||
0ce2055c51 | |||
384f8008b9 | |||
03bce0f06d | |||
48329d1b05 | |||
8f5da5c4f4 | |||
ad16182dc5 | |||
92a12682a1 | |||
0f93f08dfd | |||
01653ec5e6 | |||
31fa0cfa64 | |||
fe11a610f8 | |||
08021fa72a | |||
31dcab6d70 | |||
3aa8e86695 | |||
89787d3472 | |||
e294cd822f |
23 changed files with 1203 additions and 122 deletions
.github
.gitignore.golangci.yml.goreleaser.yaml.pre-commit-config.yamlLICENSEMakefileREADME.mdcmd
contrib
go.modgo.sumpkg/analyzer
16
.github/dependabot.yml
vendored
Normal file
16
.github/dependabot.yml
vendored
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# Doc: https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "github-actions"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "monthly"
|
||||||
|
groups:
|
||||||
|
github-actions:
|
||||||
|
patterns:
|
||||||
|
- "*" # Group all updates into a single larger pull request.
|
||||||
|
- package-ecosystem: "gomod"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "monthly"
|
49
.github/workflows/go.yml
vendored
Normal file
49
.github/workflows/go.yml
vendored
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
name: Go
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
tags: [ "*" ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ "master" ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
pull-requests: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
go: [stable, oldstable]
|
||||||
|
os: [macos-latest, windows-latest, ubuntu-latest]
|
||||||
|
name: build
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go }}
|
||||||
|
- name: Build
|
||||||
|
run: go build -v ./...
|
||||||
|
- name: Test
|
||||||
|
run: make test
|
||||||
|
coverage:
|
||||||
|
name: coverage
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
concurrency:
|
||||||
|
group: coverage
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: [build]
|
||||||
|
steps:
|
||||||
|
- name: Update coverage report
|
||||||
|
uses: ncruces/go-coverage-report@v0
|
||||||
|
with:
|
||||||
|
report: true
|
||||||
|
chart: true
|
||||||
|
amend: true
|
||||||
|
if: |
|
||||||
|
github.event_name == 'push'
|
||||||
|
continue-on-error: true
|
30
.github/workflows/golangci-lint.yml
vendored
Normal file
30
.github/workflows/golangci-lint.yml
vendored
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
name: golangci-lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
tags: [ "*" ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ "master" ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
pull-requests: read
|
||||||
|
checks: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
golangci:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go: [stable, oldstable]
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go }}
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v7
|
||||||
|
with:
|
||||||
|
version: latest
|
29
.github/workflows/goreleaser.yml
vendored
Normal file
29
.github/workflows/goreleaser.yml
vendored
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
name: goreleaser
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags: [ "*" ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
goreleaser:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: stable
|
||||||
|
- name: Run GoReleaser
|
||||||
|
uses: goreleaser/goreleaser-action@v6
|
||||||
|
with:
|
||||||
|
distribution: goreleaser
|
||||||
|
version: latest
|
||||||
|
args: release --clean
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,4 @@
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
|
/fatcontext
|
||||||
|
dist/
|
||||||
|
|
18
.golangci.yml
Normal file
18
.golangci.yml
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
version: "2"
|
||||||
|
issues:
|
||||||
|
fix: true
|
||||||
|
linters:
|
||||||
|
default: all
|
||||||
|
disable:
|
||||||
|
- depguard
|
||||||
|
- exhaustruct
|
||||||
|
formatters:
|
||||||
|
enable:
|
||||||
|
- goimports
|
||||||
|
- gofmt
|
||||||
|
- gofumpt
|
||||||
|
- golines
|
||||||
|
settings:
|
||||||
|
goimports:
|
||||||
|
local-prefixes:
|
||||||
|
- github.com/Crocmagnon/fatcontext
|
44
.goreleaser.yaml
Normal file
44
.goreleaser.yaml
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
||||||
|
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||||
|
version: 2
|
||||||
|
|
||||||
|
force_token: github
|
||||||
|
|
||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- go mod tidy
|
||||||
|
- go generate ./...
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- windows
|
||||||
|
- darwin
|
||||||
|
main: ./cmd/fatcontext
|
||||||
|
|
||||||
|
archives:
|
||||||
|
- format: tar.gz
|
||||||
|
# this name template makes the OS and Arch compatible with the results of `uname`.
|
||||||
|
name_template: >-
|
||||||
|
{{ .ProjectName }}_
|
||||||
|
{{- title .Os }}_
|
||||||
|
{{- if eq .Arch "amd64" }}x86_64
|
||||||
|
{{- else if eq .Arch "386" }}i386
|
||||||
|
{{- else }}{{ .Arch }}{{ end }}
|
||||||
|
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
||||||
|
# use zip for windows archives
|
||||||
|
format_overrides:
|
||||||
|
- goos: windows
|
||||||
|
format: zip
|
||||||
|
|
||||||
|
changelog:
|
||||||
|
sort: asc
|
||||||
|
filters:
|
||||||
|
exclude:
|
||||||
|
- "^docs:"
|
||||||
|
- "^test:"
|
||||||
|
|
||||||
|
release:
|
||||||
|
draft: true
|
21
.pre-commit-config.yaml
Normal file
21
.pre-commit-config.yaml
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
ci:
|
||||||
|
skip: [golangci-lint-full, go-test]
|
||||||
|
|
||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v5.0.0
|
||||||
|
hooks:
|
||||||
|
- id: check-yaml
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- id: trailing-whitespace
|
||||||
|
- repo: https://github.com/golangci/golangci-lint
|
||||||
|
rev: v2.0.2
|
||||||
|
hooks:
|
||||||
|
- id: golangci-lint-full
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: go-test
|
||||||
|
name: go test ./...
|
||||||
|
language: golang
|
||||||
|
types_or: [go]
|
||||||
|
entry: make test
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 Gabriel Augendre
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
7
Makefile
Normal file
7
Makefile
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
.PHONY: lint
|
||||||
|
lint:
|
||||||
|
pre-commit run --all-files
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
go test -race -v ./...
|
37
README.md
37
README.md
|
@ -1,8 +1,21 @@
|
||||||
# foreshadow
|
# fatcontext
|
||||||
|
|
||||||
`foreshadow` is a Go linter which detects un-shadowed contexts in loops.
|
[](https://pkg.go.dev/github.com/Crocmagnon/fatcontext)
|
||||||
|
[](https://goreportcard.com/report/github.com/Crocmagnon/fatcontext)
|
||||||
|
[](https://github.com/Crocmagnon/fatcontext/wiki/Coverage)
|
||||||
|
|
||||||
|
`fatcontext` is a Go linter which detects potential fat contexts in loops or function literals.
|
||||||
They can lead to performance issues, as documented here: https://gabnotes.org/fat-contexts/
|
They can lead to performance issues, as documented here: https://gabnotes.org/fat-contexts/
|
||||||
|
|
||||||
|
## Installation / usage
|
||||||
|
|
||||||
|
`fatcontext` is available in `golangci-lint` since v1.58.0.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go install github.com/Crocmagnon/fatcontext/cmd/fatcontext@latest
|
||||||
|
fatcontext ./...
|
||||||
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -23,8 +36,26 @@ func notOk() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
ctx = context.WithValue(ctx, "key", i) // "context not shadowed in loop"
|
ctx = context.WithValue(ctx, "key", i) // "nested context in loop"
|
||||||
_ = ctx
|
_ = ctx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 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
|
||||||
|
```
|
||||||
|
|
12
cmd/fatcontext/main.go
Normal file
12
cmd/fatcontext/main.go
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
// Package main runs the analyzer. It's the CLI entrypoint.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/tools/go/analysis/singlechecker"
|
||||||
|
|
||||||
|
"github.com/Crocmagnon/fatcontext/pkg/analyzer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
singlechecker.Main(analyzer.NewAnalyzer())
|
||||||
|
}
|
|
@ -1,10 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/Crocmagnon/foreshadow/pkg/analyzer"
|
|
||||||
"golang.org/x/tools/go/analysis/singlechecker"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
singlechecker.Main(analyzer.Analyzer)
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
package contrib
|
|
||||||
|
|
||||||
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++ {
|
|
||||||
ctx = context.WithValue(ctx, "key", i) // "context not shadowed in loop"
|
|
||||||
_ = ctx
|
|
||||||
}
|
|
||||||
}
|
|
11
go.mod
11
go.mod
|
@ -1,7 +1,10 @@
|
||||||
module github.com/Crocmagnon/foreshadow
|
module github.com/Crocmagnon/fatcontext
|
||||||
|
|
||||||
go 1.21
|
go 1.23.0
|
||||||
|
|
||||||
require golang.org/x/tools v0.19.0
|
require golang.org/x/tools v0.31.0
|
||||||
|
|
||||||
require golang.org/x/mod v0.16.0 // indirect
|
require (
|
||||||
|
golang.org/x/mod v0.24.0 // indirect
|
||||||
|
golang.org/x/sync v0.12.0 // indirect
|
||||||
|
)
|
||||||
|
|
14
go.sum
14
go.sum
|
@ -1,6 +1,8 @@
|
||||||
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
||||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||||
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
|
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
|
||||||
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
|
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
|
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
|
||||||
|
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=
|
||||||
|
|
|
@ -1,32 +1,69 @@
|
||||||
|
// Package analyzer contains everything related to the linter analysis.
|
||||||
package analyzer
|
package analyzer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/printer"
|
"go/printer"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"golang.org/x/tools/go/analysis"
|
"golang.org/x/tools/go/analysis"
|
||||||
"golang.org/x/tools/go/analysis/passes/inspect"
|
"golang.org/x/tools/go/analysis/passes/inspect"
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
// FlagCheckStructPointers is a possible flag for the analyzer.
|
||||||
Name: "foreshadow",
|
// Exported to make it usable in golangci-lint.
|
||||||
Doc: "enforce context shadowing inside loops",
|
const FlagCheckStructPointers = "check-struct-pointers"
|
||||||
Run: run,
|
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
// NewAnalyzer returns a fatcontext analyzer.
|
||||||
|
func NewAnalyzer() *analysis.Analyzer {
|
||||||
|
rnnr := &runner{}
|
||||||
|
|
||||||
|
flags := flag.NewFlagSet("fatcontext", flag.ExitOnError)
|
||||||
|
flags.BoolVar(&rnnr.DetectInStructPointers, FlagCheckStructPointers, false,
|
||||||
|
"set to true to detect potential fat contexts in struct pointers")
|
||||||
|
|
||||||
|
return &analysis.Analyzer{
|
||||||
|
Name: "fatcontext",
|
||||||
|
Doc: "detects nested contexts in loops and function literals",
|
||||||
|
Run: rnnr.run,
|
||||||
|
Flags: *flags,
|
||||||
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var errUnknown = errors.New("unknown node type")
|
var (
|
||||||
|
errUnknown = errors.New("unknown node type")
|
||||||
|
errInvalidAnalysis = errors.New("invalid analysis")
|
||||||
|
)
|
||||||
|
|
||||||
func run(pass *analysis.Pass) (interface{}, error) {
|
const (
|
||||||
inspctr := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
categoryInLoop = "nested context in loop"
|
||||||
|
categoryInFuncLit = "nested context in function literal"
|
||||||
|
categoryInStructPointer = "potential nested context in struct pointer"
|
||||||
|
categoryUnsupported = "unsupported nested context type"
|
||||||
|
)
|
||||||
|
|
||||||
|
type runner struct {
|
||||||
|
DetectInStructPointers bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
|
||||||
|
inspctr, typeValid := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
||||||
|
if !typeValid {
|
||||||
|
return nil, errInvalidAnalysis
|
||||||
|
}
|
||||||
|
|
||||||
nodeFilter := []ast.Node{
|
nodeFilter := []ast.Node{
|
||||||
(*ast.ForStmt)(nil),
|
(*ast.ForStmt)(nil),
|
||||||
(*ast.RangeStmt)(nil),
|
(*ast.RangeStmt)(nil),
|
||||||
|
(*ast.FuncLit)(nil),
|
||||||
|
(*ast.FuncDecl)(nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
inspctr.Preorder(nodeFilter, func(node ast.Node) {
|
inspctr.Preorder(nodeFilter, func(node ast.Node) {
|
||||||
|
@ -35,71 +72,245 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, stmt := range body.List {
|
if body == nil {
|
||||||
assignStmt, ok := stmt.(*ast.AssignStmt)
|
return
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
t := pass.TypesInfo.TypeOf(assignStmt.Lhs[0])
|
|
||||||
if t == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if t.String() != "context.Context" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if assignStmt.Tok == token.DEFINE {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
assignStmt.Tok = token.DEFINE
|
|
||||||
suggested := render(pass.Fset, assignStmt)
|
|
||||||
|
|
||||||
pass.Report(analysis.Diagnostic{
|
|
||||||
Pos: assignStmt.Pos(),
|
|
||||||
Message: "context not shadowed in loop",
|
|
||||||
SuggestedFixes: []analysis.SuggestedFix{
|
|
||||||
{
|
|
||||||
Message: fmt.Sprintf("replace `=` with `:=`"),
|
|
||||||
TextEdits: []analysis.TextEdit{
|
|
||||||
{
|
|
||||||
Pos: assignStmt.Pos(),
|
|
||||||
End: assignStmt.End(),
|
|
||||||
NewText: []byte(suggested),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assignStmt := findNestedContext(pass, node, body.List)
|
||||||
|
if assignStmt == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
category := getCategory(pass, node, assignStmt)
|
||||||
|
|
||||||
|
if r.shouldIgnoreReport(category) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fixes := r.getSuggestedFixes(pass, assignStmt, category)
|
||||||
|
|
||||||
|
pass.Report(analysis.Diagnostic{
|
||||||
|
Pos: assignStmt.Pos(),
|
||||||
|
Message: category,
|
||||||
|
SuggestedFixes: fixes,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil //nolint:nilnil // we have no result to send to other analyzers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *runner) shouldIgnoreReport(category string) bool {
|
||||||
|
return category == categoryInStructPointer && !r.DetectInStructPointers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *runner) getSuggestedFixes(
|
||||||
|
pass *analysis.Pass,
|
||||||
|
assignStmt *ast.AssignStmt,
|
||||||
|
category string,
|
||||||
|
) []analysis.SuggestedFix {
|
||||||
|
switch category {
|
||||||
|
case categoryInStructPointer, categoryUnsupported:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
suggestedStmt := ast.AssignStmt{
|
||||||
|
Lhs: assignStmt.Lhs,
|
||||||
|
TokPos: assignStmt.TokPos,
|
||||||
|
Tok: token.DEFINE,
|
||||||
|
Rhs: assignStmt.Rhs,
|
||||||
|
}
|
||||||
|
suggested, err := render(pass.Fset, &suggestedStmt)
|
||||||
|
|
||||||
|
var fixes []analysis.SuggestedFix
|
||||||
|
if err == nil {
|
||||||
|
fixes = append(fixes, analysis.SuggestedFix{
|
||||||
|
Message: "replace `=` with `:=`",
|
||||||
|
TextEdits: []analysis.TextEdit{
|
||||||
|
{
|
||||||
|
Pos: assignStmt.Pos(),
|
||||||
|
End: assignStmt.End(),
|
||||||
|
NewText: suggested,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return fixes
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCategory(pass *analysis.Pass, node ast.Node, assignStmt *ast.AssignStmt) string {
|
||||||
|
switch node.(type) {
|
||||||
|
case *ast.ForStmt, *ast.RangeStmt:
|
||||||
|
return categoryInLoop
|
||||||
|
}
|
||||||
|
|
||||||
|
if isPointer(pass, assignStmt.Lhs[0]) {
|
||||||
|
return categoryInStructPointer
|
||||||
|
}
|
||||||
|
|
||||||
|
switch node.(type) {
|
||||||
|
case *ast.FuncLit, *ast.FuncDecl:
|
||||||
|
return categoryInFuncLit
|
||||||
|
default:
|
||||||
|
return categoryUnsupported
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBody(node ast.Node) (*ast.BlockStmt, error) {
|
func getBody(node ast.Node) (*ast.BlockStmt, error) {
|
||||||
forStmt, ok := node.(*ast.ForStmt)
|
switch typedNode := node.(type) {
|
||||||
if ok {
|
case *ast.ForStmt:
|
||||||
return forStmt.Body, nil
|
return typedNode.Body, nil
|
||||||
}
|
case *ast.RangeStmt:
|
||||||
|
return typedNode.Body, nil
|
||||||
rangeStmt, ok := node.(*ast.RangeStmt)
|
case *ast.FuncLit:
|
||||||
if ok {
|
return typedNode.Body, nil
|
||||||
return rangeStmt.Body, nil
|
case *ast.FuncDecl:
|
||||||
|
return typedNode.Body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errUnknown
|
return nil, errUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
// render returns the pretty-print of the given node
|
func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *ast.AssignStmt {
|
||||||
func render(fset *token.FileSet, x interface{}) string {
|
for _, stmt := range stmts {
|
||||||
|
// Recurse if necessary
|
||||||
|
stmtList := getStmtList(stmt)
|
||||||
|
if found := findNestedContext(pass, node, stmtList); found != nil {
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actually check for nested context
|
||||||
|
assignStmt, ok := stmt.(*ast.AssignStmt)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
t := pass.TypesInfo.TypeOf(assignStmt.Lhs[0])
|
||||||
|
if t == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.String() != "context.Context" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if assignStmt.Tok == token.DEFINE {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore [context.Background] & [context.TODO].
|
||||||
|
if isContextFunction(assignStmt.Rhs[0], "Background", "TODO") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if isPointer(pass, assignStmt.Lhs[0]) {
|
||||||
|
return assignStmt
|
||||||
|
}
|
||||||
|
|
||||||
|
// allow assignment to non-pointer children of values defined within the loop
|
||||||
|
if isWithinLoop(assignStmt.Lhs[0], node, pass) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return assignStmt
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStmtList(stmt ast.Stmt) []ast.Stmt {
|
||||||
|
switch typedStmt := stmt.(type) {
|
||||||
|
case *ast.BlockStmt:
|
||||||
|
return typedStmt.List
|
||||||
|
case *ast.IfStmt:
|
||||||
|
return typedStmt.Body.List
|
||||||
|
case *ast.SwitchStmt:
|
||||||
|
return typedStmt.Body.List
|
||||||
|
case *ast.CaseClause:
|
||||||
|
return typedStmt.Body
|
||||||
|
case *ast.SelectStmt:
|
||||||
|
return typedStmt.Body.List
|
||||||
|
case *ast.CommClause:
|
||||||
|
return typedStmt.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// render returns the pretty-print of the given node.
|
||||||
|
func render(fset *token.FileSet, x interface{}) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := printer.Fprint(&buf, fset, x); err != nil {
|
if err := printer.Fprint(&buf, fset, x); err != nil {
|
||||||
panic(err)
|
return nil, fmt.Errorf("printing node: %w", err)
|
||||||
}
|
}
|
||||||
return buf.String()
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isContextFunction(exp ast.Expr, fnName ...string) bool {
|
||||||
|
call, typeValid := exp.(*ast.CallExpr)
|
||||||
|
if !typeValid {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
selector, typeValid := call.Fun.(*ast.SelectorExpr)
|
||||||
|
if !typeValid {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
ident, typeValid := selector.X.(*ast.Ident)
|
||||||
|
if !typeValid {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return ident.Name == "context" && slices.Contains(fnName, selector.Sel.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isWithinLoop(exp ast.Expr, node ast.Node, pass *analysis.Pass) bool {
|
||||||
|
lhs := getRootIdent(pass, exp)
|
||||||
|
if lhs == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
obj := pass.TypesInfo.ObjectOf(lhs)
|
||||||
|
if obj == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
scope := obj.Parent()
|
||||||
|
if scope == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return scope.Pos() >= node.Pos() && scope.End() <= node.End()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
|
||||||
|
for {
|
||||||
|
switch typedNode := node.(type) {
|
||||||
|
case *ast.Ident:
|
||||||
|
return typedNode
|
||||||
|
case *ast.IndexExpr:
|
||||||
|
node = typedNode.X
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
if sel, ok := pass.TypesInfo.Selections[typedNode]; ok && sel.Indirect() {
|
||||||
|
return nil // indirected (pointer) roots don't imply a (safe) copy
|
||||||
|
}
|
||||||
|
|
||||||
|
node = typedNode.X
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPointer(pass *analysis.Pass, exp ast.Node) bool {
|
||||||
|
switch n := exp.(type) { //nolint:gocritic // Future-proofing with switch instead of if.
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
sel, ok := pass.TypesInfo.Selections[n]
|
||||||
|
|
||||||
|
return ok && sel.Indirect()
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,67 @@
|
||||||
package analyzer_test
|
package analyzer_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Crocmagnon/foreshadow/pkg/analyzer"
|
|
||||||
"golang.org/x/tools/go/analysis/analysistest"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/analysis/analysistest"
|
||||||
|
|
||||||
|
"github.com/Crocmagnon/fatcontext/pkg/analyzer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAll(t *testing.T) {
|
func TestAnalyzer(t *testing.T) {
|
||||||
wd, err := os.Getwd()
|
t.Parallel()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to get wd: %s", err)
|
|
||||||
}
|
|
||||||
testdata := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata")
|
|
||||||
|
|
||||||
analysistest.Run(t, testdata, analyzer.Analyzer, "./...")
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
dir string
|
||||||
|
options map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "no func decl",
|
||||||
|
dir: "common",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "no func decl",
|
||||||
|
dir: "no_structpointer",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "func decl",
|
||||||
|
dir: "common",
|
||||||
|
options: map[string]string{
|
||||||
|
analyzer.FlagCheckStructPointers: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "func decl",
|
||||||
|
dir: "structpointer",
|
||||||
|
options: map[string]string{
|
||||||
|
analyzer.FlagCheckStructPointers: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range testCases {
|
||||||
|
t.Run(test.desc+"_"+test.dir, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
anlzr := analyzer.NewAnalyzer()
|
||||||
|
|
||||||
|
for k, v := range test.options {
|
||||||
|
err := anlzr.Flags.Set(k, v)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), anlzr, test.dir)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAnalyzer_cgo(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
a := analyzer.NewAnalyzer()
|
||||||
|
|
||||||
|
analysistest.Run(t, analysistest.TestData(), a, "cgo")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,27 @@
|
||||||
package src
|
package cgo
|
||||||
|
|
||||||
import "context"
|
/*
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
func example() {
|
void myprint(char* s) {
|
||||||
|
printf("%d\n", s);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
cs := C.CString("Hello from stdio\n")
|
||||||
|
C.myprint(cs)
|
||||||
|
C.free(unsafe.Pointer(cs))
|
||||||
|
}
|
||||||
|
|
||||||
|
func _() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
|
@ -11,18 +30,18 @@ func example() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
ctx = context.WithValue(ctx, "key", i) // want "context not shadowed in loop"
|
ctx = context.WithValue(ctx, "key", i) // want "nested context in loop"
|
||||||
ctx = context.WithValue(ctx, "other", "val")
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
}
|
}
|
||||||
|
|
||||||
for item := range []string{"one", "two", "three"} {
|
for item := range []string{"one", "two", "three"} {
|
||||||
ctx = wrapContext(ctx) // want "context not shadowed in loop"
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
ctx := context.WithValue(ctx, "key", item)
|
ctx := context.WithValue(ctx, "key", item)
|
||||||
ctx = wrapContext(ctx)
|
ctx = wrapContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
ctx = wrapContext(ctx) // want "context not shadowed in loop"
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
251
pkg/analyzer/testdata/src/common/example.go
vendored
Normal file
251
pkg/analyzer/testdata/src/common/example.go
vendored
Normal file
|
@ -0,0 +1,251 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func example() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx := context.WithValue(ctx, "key", i)
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx = context.WithValue(ctx, "key", i) // want "nested context in loop"
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for item := range []string{"one", "two", "three"} {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
ctx := context.WithValue(ctx, "key", item)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// not fooled by shadowing in nested blocks
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
default:
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// detects contexts wrapped in function literals (this is risky as function literals can be called multiple times)
|
||||||
|
_ = func() {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in function literal"
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is fine because the context is created in the loop
|
||||||
|
for {
|
||||||
|
if ctx := context.Background(); doSomething() != nil {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx2 := context.Background()
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
if doSomething() != nil {
|
||||||
|
ctx2 = wrapContext(ctx2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapContext(ctx context.Context) context.Context {
|
||||||
|
return context.WithoutCancel(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doSomething() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// storing contexts in a struct isn't recommended, but local copies of a non-pointer struct should act like local copies of a context.
|
||||||
|
func inStructs(ctx context.Context) {
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := struct{ Ctx context.Context }{ctx}
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "key", i)
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := []struct{ Ctx context.Context }{{ctx}}
|
||||||
|
c[0].Ctx = context.WithValue(c[0].Ctx, "key", i)
|
||||||
|
c[0].Ctx = context.WithValue(c[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
c := struct{ Ctx context.Context }{ctx}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := c
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "key", i)
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
pc := &struct{ Ctx context.Context }{ctx}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := pc
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "key", i) // want "nested context in loop"
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
r := []struct{ Ctx context.Context }{{ctx}}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
r[0].Ctx = context.WithValue(r[0].Ctx, "key", i) // want "nested context in loop"
|
||||||
|
r[0].Ctx = context.WithValue(r[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
rp := []*struct{ Ctx context.Context }{{ctx}}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
rp[0].Ctx = context.WithValue(rp[0].Ctx, "key", i) // want "nested context in loop"
|
||||||
|
rp[0].Ctx = context.WithValue(rp[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func inVariousNestedBlocks(ctx context.Context) {
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
if true {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
switch err {
|
||||||
|
default:
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
{
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in loop"
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware could run on every request, bloating the request parameter level context and causing a memory leak
|
||||||
|
func badMiddleware(ctx context.Context) func() error {
|
||||||
|
return func() error {
|
||||||
|
ctx = wrapContext(ctx) // want "nested context in function literal"
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware is fine, as it doesn't modify the context of parent function
|
||||||
|
func okMiddleware(ctx context.Context) func() error {
|
||||||
|
return func() error {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware is fine, as it only modifies the context passed to it
|
||||||
|
func okMiddleware2(ctx context.Context) func(ctx context.Context) error {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func doSomethingWithCtx(ctx context.Context) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCasesInit(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
ctx context.Context
|
||||||
|
}{
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
ctx: context.WithValue(context.Background(), "key", "value"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run("some test", func(t *testing.T) {
|
||||||
|
if tc.ctx == nil {
|
||||||
|
tc.ctx = context.Background()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
251
pkg/analyzer/testdata/src/common/example.go.golden
vendored
Normal file
251
pkg/analyzer/testdata/src/common/example.go.golden
vendored
Normal file
|
@ -0,0 +1,251 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func example() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx := context.WithValue(ctx, "key", i)
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
ctx := context.WithValue(ctx, "key", i) // want "nested context in loop"
|
||||||
|
ctx = context.WithValue(ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for item := range []string{"one", "two", "three"} {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
ctx := context.WithValue(ctx, "key", item)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// not fooled by shadowing in nested blocks
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
default:
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// detects contexts wrapped in function literals (this is risky as function literals can be called multiple times)
|
||||||
|
_ = func() {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in function literal"
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is fine because the context is created in the loop
|
||||||
|
for {
|
||||||
|
if ctx := context.Background(); doSomething() != nil {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx2 := context.Background()
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
if doSomething() != nil {
|
||||||
|
ctx2 = wrapContext(ctx2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapContext(ctx context.Context) context.Context {
|
||||||
|
return context.WithoutCancel(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doSomething() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// storing contexts in a struct isn't recommended, but local copies of a non-pointer struct should act like local copies of a context.
|
||||||
|
func inStructs(ctx context.Context) {
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := struct{ Ctx context.Context }{ctx}
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "key", i)
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := []struct{ Ctx context.Context }{{ctx}}
|
||||||
|
c[0].Ctx = context.WithValue(c[0].Ctx, "key", i)
|
||||||
|
c[0].Ctx = context.WithValue(c[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
c := struct{ Ctx context.Context }{ctx}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := c
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "key", i)
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
pc := &struct{ Ctx context.Context }{ctx}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
c := pc
|
||||||
|
c.Ctx := context.WithValue(c.Ctx, "key", i) // want "nested context in loop"
|
||||||
|
c.Ctx = context.WithValue(c.Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
r := []struct{ Ctx context.Context }{{ctx}}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
r[0].Ctx := context.WithValue(r[0].Ctx, "key", i) // want "nested context in loop"
|
||||||
|
r[0].Ctx = context.WithValue(r[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
|
||||||
|
rp := []*struct{ Ctx context.Context }{{ctx}}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
rp[0].Ctx := context.WithValue(rp[0].Ctx, "key", i) // want "nested context in loop"
|
||||||
|
rp[0].Ctx = context.WithValue(rp[0].Ctx, "other", "val")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func inVariousNestedBlocks(ctx context.Context) {
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
if true {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := doSomething()
|
||||||
|
switch err {
|
||||||
|
default:
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
|
||||||
|
err := doSomething()
|
||||||
|
if err != nil {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
{
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in loop"
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware could run on every request, bloating the request parameter level context and causing a memory leak
|
||||||
|
func badMiddleware(ctx context.Context) func() error {
|
||||||
|
return func() error {
|
||||||
|
ctx := wrapContext(ctx) // want "nested context in function literal"
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware is fine, as it doesn't modify the context of parent function
|
||||||
|
func okMiddleware(ctx context.Context) func() error {
|
||||||
|
return func() error {
|
||||||
|
ctx := wrapContext(ctx)
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this middleware is fine, as it only modifies the context passed to it
|
||||||
|
func okMiddleware2(ctx context.Context) func(ctx context.Context) error {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
ctx = wrapContext(ctx)
|
||||||
|
return doSomethingWithCtx(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func doSomethingWithCtx(ctx context.Context) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCasesInit(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
ctx context.Context
|
||||||
|
}{
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
ctx: context.WithValue(context.Background(), "key", "value"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run("some test", func(t *testing.T) {
|
||||||
|
if tc.ctx == nil {
|
||||||
|
tc.ctx = context.Background()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
23
pkg/analyzer/testdata/src/no_structpointer/example.go
vendored
Normal file
23
pkg/analyzer/testdata/src/no_structpointer/example.go
vendored
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Container struct {
|
||||||
|
Ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func something() func(*Container) {
|
||||||
|
return func(r *Container) {
|
||||||
|
ctx := r.Ctx
|
||||||
|
ctx = context.WithValue(ctx, "key", "val")
|
||||||
|
r.Ctx = ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func blah(r *Container) {
|
||||||
|
ctx := r.Ctx
|
||||||
|
ctx = context.WithValue(ctx, "key", "val")
|
||||||
|
r.Ctx = ctx
|
||||||
|
}
|
23
pkg/analyzer/testdata/src/structpointer/example.go
vendored
Normal file
23
pkg/analyzer/testdata/src/structpointer/example.go
vendored
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Container struct {
|
||||||
|
Ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func something() func(*Container) {
|
||||||
|
return func(r *Container) {
|
||||||
|
ctx := r.Ctx
|
||||||
|
ctx = context.WithValue(ctx, "key", "val")
|
||||||
|
r.Ctx = ctx // want "potential nested context in struct pointer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func blah(r *Container) {
|
||||||
|
ctx := r.Ctx
|
||||||
|
ctx = context.WithValue(ctx, "key", "val")
|
||||||
|
r.Ctx = ctx // want "potential nested context in struct pointer"
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue