Repository navigation
fix(linters): unwrap parens in timenowsub, timeafterleak, errstringmatch, sprintfint - #75085
Conversation
|
🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅
|
|
✅ Design Decision Gate 🏗️ completed the design decision gate check. See the comment below for the result and any generated ADR draft. Design Decision Gate 🏗️Outcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
|
✅ Test Quality Sentinel completed test quality analysis. Test Quality SentinelOutcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
|
✅ PR Code Quality Reviewer completed the code quality review.
|
|
✅ Ponytail Reviewer completed successfully! Ponytail ReviewerOutcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
There was a problem hiding this comment.
Review summary
No actionable correctness, performance, or security issues found in the changed lines. The parenthesis-unwrapping updates are consistent with the added fixtures; please retain CI validation as noted in the PR description.
🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~3 AIC · ⌖ ~1 AIC · ⊞ 1.6K
Comment /review to run again
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the parenthesized-expression handling changes across the four Go linters.
No open issues 🎉
What changed in this PR
This PR closes parenthesis-related detection gaps in four CI-enforced Go linters, using shared AST helpers to recognize equivalent call forms consistently.
Changes:
- Unwrap parenthesized callees and relevant arguments in
timenowsub,timeafterleak,errstringmatch, andsprintfint. - Make
timeafterleakskip parenthesized ancestors when identifying select receives and reuse the shared package-selector check. - Add regression fixtures and expected automatic-fix outputs.
| File | Description |
|---|---|
pkg/linters/timenowsub/timenowsub.go |
Recognize parenthesized Now and Sub callees. |
pkg/linters/timenowsub/testdata/src/timenowsub/timenowsub.go.golden |
Record expected time.Since replacements. |
pkg/linters/timenowsub/testdata/src/timenowsub/timenowsub.go |
Add parenthesized callee regression cases. |
pkg/linters/timeafterleak/timeafterleak.go |
Handle parenthesized calls and receives; simplify package matching. |
pkg/linters/timeafterleak/testdata/src/timeafterleak/timeafterleak.go |
Cover three parenthesized select-receive forms. |
pkg/linters/sprintfint/testdata/src/sprintfint/sprintfint.go.golden |
Record the expected strconv.Itoa replacement. |
pkg/linters/sprintfint/testdata/src/sprintfint/sprintfint.go |
Add a parenthesized callee and format-literal case. |
pkg/linters/sprintfint/sprintfint.go |
Unwrap the callee and format literal before matching. |
pkg/linters/errstringmatch/testdata/src/errstringmatch/errstringmatch.go |
Cover parenthesized string functions and error calls. |
pkg/linters/errstringmatch/errstringmatch.go |
Unwrap string-function callees, error calls, and error-method callees. |
Review effort: Balanced (auto)
There was a problem hiding this comment.
Skills-Based Review 🧠
Applied /tdd, /diagnosing-bugs and /codebase-design. Approving: the paren-unwrap fixes are correct and tested.
📋 Key Themes & Highlights
Verification
go testpasses fortimenowsub,timeafterleak,errstringmatchandsprintfint. This covers the sandbox gap the author noted.gofmt -lis clean for all four changed packages (the one hit insprintfint/testdata/.../aliased_import.gois outside this diff).- The four changed analyzers, run with
-test=falseover./cmd/...and./pkg/..., report no findings. This mirrors the CI gate for these analyzers, not the full flag list.
Positive Highlights
- ✅
isTimeAfterCallnow reusesastutil.IsPkgSelector, removing the hand-rolledtypes.PkgNamecheck. - ✅ Each fix comes with a fixture that pins the new behaviour via
// wantannotations. - ✅ The
timeafterleakparent walk only skips*ast.ParenExpr, so it does not widen what gets flagged.
Non-blocking suggestions
- Add an ExprStmt paren case (
case (<-time.After(d)):) toBadParenthesized. Only the AssignStmt form is covered today. - Consider moving
parentSkippingParensintointernal/astutilonce the remaining ~35.Funbatches land.
@copilot please address the review comments above.
🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~5 AIC · ⌖ ~1 AIC · ⊞ 5.4K
Comment /matt to run again
| return | ||
| } | ||
| select { | ||
| case t := (<-time.After(time.Second)): // want `time\.After creates a new timer on each loop iteration that is not garbage collected until it fires; use time\.NewTimer with Reset and Stop instead` |
There was a problem hiding this comment.
[/tdd] Coverage gap: the parent-skipping path on the Comm statement is only exercised through the AssignStmt form case t := (<-time.After(time.Second)):. Add the ExprStmt form case (<-time.After(time.Second)): with the same // want annotation, so both statement kinds are covered.
💡 Suggested fixture
Add one more case to BadParenthesized, using the same // want text as line 81 and no assignment.
This is non-blocking. The rest of the diff is well covered.
@copilot please address this.
| // parentSkippingParens returns the nearest ancestor of cur that is not a | ||
| // *ast.ParenExpr, so that <-(time.After(d)) and (<-time.After(d)) are matched | ||
| // the same as <-time.After(d). | ||
| func parentSkippingParens(cur inspector.Cursor) inspector.Cursor { |
There was a problem hiding this comment.
[/codebase-design] parentSkippingParens is a generic cursor helper, and the PR notes about 35 bare .Fun assertions remain. The paren-unwrap logic already lives in astutil.UnwrapParenExpr. Consider moving this helper into internal/astutil next to it so the upcoming batches share one implementation. Keeping it here is fine for now, since only this linter uses it.
@copilot please address this.
|
🎉 This pull request is included in a new release. Release: |
Summary
Four linters that CI enforces (
.github/workflows/cgo.yml:1674and:1701) still used a barecall.Fun.(*ast.SelectorExpr)type assertion. Because of that, they silently skipped valid Go calls that put parentheses around the function being called:timenowsub(time.Now().Sub)(t),(time.Now)().Sub(t)timenowsub.go:42,:99timeafterleakcase <-(time.After)(d):,case <-(time.After(d)):,case t := (<-time.After(d)):timeafterleak.go:70,:96,:104errstringmatch(strings.Contains)(err.Error(), "x"),strings.HasPrefix((err.Error()), "x"),(err.Error)()errstringmatch.go:82,:98,:102sprintfint(fmt.Sprintf)(("%d"), n)sprintfint.go:52,:62This continues the paren-unwrap series. The last merged batch fixed
timesleepnocontext,ctxbackgroundandexeccommandwithoutcontext; I checked that each now callsastutil.UnwrapParenExpr(call.Fun).Changes
call.Fun: unwrap it withastutil.UnwrapParenExprin all four linters.errstringmatch'sisErrDotErrornow unwraps both the argument and the.Errorcallee.sprintfintnow unwraps the"%d"format literal.timeafterleakcursor walk (a separate gap):isInsideLoopSelectCommrequired the<-receive to be the direct parent of thetime.Aftercall, and the receive statement to be the direct parent of the<-. A new helper,parentSkippingParens, skips*ast.ParenExprancestors. The Go spec says a selectRecvExpr"must be a (possibly parenthesized) receive operation", socase t := (<-time.After(d)):is valid Go and leaks the same way.timeafterleakcleanup: replaced its hand-rolled 12-linetypes.PkgNamecheck with the sharedastutil.IsPkgSelector. Thego/typesimport is no longer needed and was removed.timenowsub(rewrites totime.Since(t)) andsprintfint(rewrites tostrconv.Itoa(n)).GoodTimeAfterInBodycase intimeafterleakstays unflagged, because thecc.Comm != commStmtguard is unchanged.Validation
get_diagnostics_for_fileon all four changed source files: no diagnostics.timeafterleak,errstringmatchandsprintfint: no type errors. The only findings are staticcheck or unused-function hints that were already there.timenowsub: the only diagnostic is the existing GOPATH-stylefaketime/timeimport, which analysistest resolves itself.TestTimeNowQualifierParenthesesinternal test callstimeNowQualifierwith the same signature and the sameNowmatching, so it is unaffected.go testorgofmtin this sandbox (permission denied). Please make sure CI'sgo test ./pkg/linters/{timenowsub,timeafterleak,errstringmatch,sprintfint}/...passes before merging.Remaining work (not in this PR)
About 35 bare
.Funtype assertions remain, for example insprintfbool,fprintlnsprintf,writebytestring,closeerrorunchecked,uncheckedflushreturnandblankassigncomma. They will be handled in later small batches.🤖 Generated with [Claude Code]((claude.com/redacted)