Skip to content

fix(linters): unwrap parens in timenowsub, timeafterleak, errstringmatch, sprintfint - #75085

Merged
pelikhan merged 1 commit into
mainfrom
sergo/paren-unwrap-time-err-sprintf-r120-bab28dcc833544f4
Nov 5, 2026
Merged

pelikhan merged 1 commit into
mainfrom
sergo/paren-unwrap-time-err-sprintf-r120-bab28dcc833544f4

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Four linters that CI enforces (.github/workflows/cgo.yml:1674 and :1701) still used a bare call.Fun.(*ast.SelectorExpr) type assertion. Because of that, they silently skipped valid Go calls that put parentheses around the function being called:

Linter Missed form (before) Location
timenowsub (time.Now().Sub)(t), (time.Now)().Sub(t) timenowsub.go:42, :99
timeafterleak case <-(time.After)(d):, case <-(time.After(d)):, case t := (<-time.After(d)): timeafterleak.go:70, :96, :104
errstringmatch (strings.Contains)(err.Error(), "x"), strings.HasPrefix((err.Error()), "x"), (err.Error)() errstringmatch.go:82, :98, :102
sprintfint (fmt.Sprintf)(("%d"), n) sprintfint.go:52, :62

This continues the paren-unwrap series. The last merged batch fixed timesleepnocontext, ctxbackground and execcommandwithoutcontext; I checked that each now calls astutil.UnwrapParenExpr(call.Fun).

Changes

  • call.Fun: unwrap it with astutil.UnwrapParenExpr in all four linters.
  • Argument positions:
    • errstringmatch's isErrDotError now unwraps both the argument and the .Error callee.
    • sprintfint now unwraps the "%d" format literal.
  • timeafterleak cursor walk (a separate gap): isInsideLoopSelectComm required the <- receive to be the direct parent of the time.After call, and the receive statement to be the direct parent of the <-. A new helper, parentSkippingParens, skips *ast.ParenExpr ancestors. The Go spec says a select RecvExpr "must be a (possibly parenthesized) receive operation", so case t := (<-time.After(d)): is valid Go and leaks the same way.
  • timeafterleak cleanup: replaced its hand-rolled 12-line types.PkgName check with the shared astutil.IsPkgSelector. The go/types import is no longer needed and was removed.
  • Fixtures:
    • New paren cases in all four linters.
    • New goldens for timenowsub (rewrites to time.Since(t)) and sprintfint (rewrites to strconv.Itoa(n)).
    • The GoodTimeAfterInBody case in timeafterleak stays unflagged, because the cc.Comm != commStmt guard is unchanged.

Validation

  • Ran Serena / gopls get_diagnostics_for_file on all four changed source files: no diagnostics.
  • Fixture diagnostics:
    • timeafterleak, errstringmatch and sprintfint: no type errors. The only findings are staticcheck or unused-function hints that were already there.
    • timenowsub: the only diagnostic is the existing GOPATH-style faketime/time import, which analysistest resolves itself.
  • The existing TestTimeNowQualifierParentheses internal test calls timeNowQualifier with the same signature and the same Now matching, so it is unaffected.
  • ⚠️ I could not run go test or gofmt in this sandbox (permission denied). Please make sure CI's go test ./pkg/linters/{timenowsub,timeafterleak,errstringmatch,sprintfint}/... passes before merging.

Remaining work (not in this PR)

About 35 bare .Fun type assertions remain, for example in sprintfbool, fprintlnsprintf, writebytestring, closeerrorunchecked, uncheckedflushreturn and blankassigncomma. They will be handled in later small batches.

🤖 Generated with [Claude Code]((claude.com/redacted)

Generated by 🤖 Sergo - Serena Go Expert · claude · routed: opus55 high · ~141 AIC · ⌖ ~2 AIC · ⊞ 3.8K · ◷

  • expires on Nov 11, 2026, 7:53 PM UTC-08:00

@github-actions github-actions Bot added cookie Issue Monster Loves Cookies! sergo labels Nov 5, 2026
@pelikhan
pelikhan marked this pull request as ready for review November 5, 2026 04:06
Copilot AI adaptive review requested due to automatic review settings November 5, 2026 04:06
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ 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: reported_no_action

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.

Generated from Design Decision Gate 🏗️ · routed: opus55 medium · ~12 AIC · ⌖ ~2 AIC · ⊞ 7.7K · ◷

🏗️ ADR gate enforced by Design Decision Gate 🏗️

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ Test Quality Sentinel completed test quality analysis.

Test Quality Sentinel

Outcome: reported_no_action

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.

Generated from Test Quality Sentinel · routed: gpt56 max · ~2 AIC · ⌖ ~1 AIC · ⊞ 6.3K · ◷

🧪 Test quality analysis by Test Quality Sentinel

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ PR Code Quality Reviewer completed the code quality review.

🔎 Code quality review by PR Code Quality Reviewer

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

✅ Ponytail Reviewer completed successfully!

Ponytail Reviewer

Outcome: reported_no_action

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.

Generated from Ponytail Reviewer · routed: gpt56 max · ~2 AIC · ⌖ ~1 AIC · ⊞ 118 · ◷

Generated by Ponytail Reviewer for #75085

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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, and sprintfint.
  • Make timeafterleak skip 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)

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skills-Based Review 🧠

Applied /tdd, /diagnosing-bugs and /codebase-design. Approving: the paren-unwrap fixes are correct and tested.

📋 Key Themes & Highlights

Verification

  • go test passes for timenowsub, timeafterleak, errstringmatch and sprintfint. This covers the sandbox gap the author noted.
  • gofmt -l is clean for all four changed packages (the one hit in sprintfint/testdata/.../aliased_import.go is outside this diff).
  • The four changed analyzers, run with -test=false over ./cmd/... and ./pkg/..., report no findings. This mirrors the CI gate for these analyzers, not the full flag list.

Positive Highlights

  • ✅ isTimeAfterCall now reuses astutil.IsPkgSelector, removing the hand-rolled types.PkgName check.
  • ✅ Each fix comes with a fixture that pins the new behaviour via // want annotations.
  • ✅ The timeafterleak parent 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)):) to BadParenthesized. Only the AssignStmt form is covered today.
  • Consider moving parentSkippingParens into internal/astutil once the remaining ~35 .Fun batches 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`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/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 {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/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.

@pelikhan
pelikhan merged commit 63a4393 into main Nov 5, 2026
128 checks passed
@pelikhan
pelikhan deleted the sergo/paren-unwrap-time-err-sprintf-r120-bab28dcc833544f4 branch November 5, 2026 06:12
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

🎉 This pull request is included in a new release.

Release: v1.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cookie Issue Monster Loves Cookies! sergo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants