Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[linter-miner] feat(linters): add ctxbackground linter — flag context.Background() when a ctx param exists #32865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
[linter-miner] feat(linters): add ctxbackground linter — flag context.Background() when a ctx param exists #32865
Changes from all commits
631413dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd] The
nodeFilteronly includes*ast.FuncDecl, so anonymous functions (*ast.FuncLit) that themselves receive acontext.Contextparameter are never checked. For example:If this is an intentional limitation (keeping the linter simple), add a comment to
nodeFilterdocumenting thatFuncLitis excluded on purpose. If the intent is to catch all cases, extendnodeFilterto also include(*ast.FuncLit)(nil)and applyhasContextParamtoFuncLit.Typeas well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd]
ast.Inspectrecursively descends into nested*ast.FuncLitnodes. This meanscontext.Background()inside a closure that capturesctxfrom the outer scope will be flagged — even when the closure deliberately detaches from the parent context (e.g. a background goroutine that must outlive the request). You should skip nested func literals:Without this guard, patterns like
go func() { ctx := context.Background(); ... }()inside a function that has actxparam produce false positives.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd] The testdata doesn't cover the anonymous-function/closure scenario, which is where the
ast.Inspectdescent issue above would surface. Consider adding:Having an explicit fixture for this case makes the intended behaviour clear and prevents regressions when the linter is later modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd]
context.TODO()is a closely related anti-pattern ("I'll wire up a real context later") but isn't flagged by this linter. Either add a test fixture that explicitly marks it as not flagged (to document the intentional scope boundary), or extend the linter to also catchcontext.TODO(). Leaving it undocumented means the next person to maintain this linter won't know whether the omission was deliberate.Uh oh!
There was an error while loading. Please reload this page.