refactor: rename foreshadow to fatcontext

This commit is contained in:
Gabriel Augendre 2024-03-27 23:50:12 +01:00
parent a7ef75e6e9
commit 781d685146
8 changed files with 14 additions and 14 deletions

View file

@ -20,7 +20,7 @@ builds:
- linux - linux
- windows - windows
- darwin - darwin
main: ./cmd/foreshadow main: ./cmd/fatcontext
archives: archives:
- format: tar.gz - format: tar.gz

View file

@ -1,6 +1,6 @@
# foreshadow # fatcontext
`foreshadow` is a Go linter which detects un-shadowed contexts in loops. `fatcontext` is a Go linter which detects potential fat contexts in loops.
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/
## Example ## Example
@ -23,7 +23,7 @@ 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
} }
} }

View file

@ -1,7 +1,7 @@
package main package main
import ( import (
"github.com/Crocmagnon/foreshadow/pkg/analyzer" "github.com/Crocmagnon/fatcontext/pkg/analyzer"
"golang.org/x/tools/go/analysis/singlechecker" "golang.org/x/tools/go/analysis/singlechecker"
) )

View file

@ -15,7 +15,7 @@ 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
} }
} }

2
go.mod
View file

@ -1,4 +1,4 @@
module github.com/Crocmagnon/foreshadow module github.com/Crocmagnon/fatcontext
go 1.21 go 1.21

View file

@ -12,8 +12,8 @@ import (
) )
var Analyzer = &analysis.Analyzer{ var Analyzer = &analysis.Analyzer{
Name: "foreshadow", Name: "fatcontext",
Doc: "enforce context shadowing inside loops", Doc: "detects nested contexts in loops",
Run: run, Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer}, Requires: []*analysis.Analyzer{inspect.Analyzer},
} }
@ -63,7 +63,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
pass.Report(analysis.Diagnostic{ pass.Report(analysis.Diagnostic{
Pos: assignStmt.Pos(), Pos: assignStmt.Pos(),
Message: "context not shadowed in loop", Message: "nested context in loop",
SuggestedFixes: []analysis.SuggestedFix{ SuggestedFixes: []analysis.SuggestedFix{
{ {
Message: "replace `=` with `:=`", Message: "replace `=` with `:=`",

View file

@ -1,7 +1,7 @@
package analyzer_test package analyzer_test
import ( import (
"github.com/Crocmagnon/foreshadow/pkg/analyzer" "github.com/Crocmagnon/fatcontext/pkg/analyzer"
"golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/analysistest"
"os" "os"
"path/filepath" "path/filepath"

View file

@ -11,18 +11,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
} }
} }