Skip to content

Expand timenowsub coverage to safe selector and index arguments - #46754

Merged
pelikhan merged 3 commits into
mainfrom
copilot/fix-timenowsub-coverage-gap
Jul 20, 2026
Merged

Expand timenowsub coverage to safe selector and index arguments#46754
pelikhan merged 3 commits into
mainfrom
copilot/fix-timenowsub-coverage-gap

Conversation

Copilot AI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

timenowsub only rewrote time.Now().Sub(t) when t was an ident, so it missed the common stored-start-time forms s.start and starts[i]. This change broadens the safety check to cover side-effect-free selector and index expressions while continuing to exclude call-based arguments.

  • Analyzer coverage

    • Extend isSafeSinceArg beyond bare idents to accept pure expression shapes that are evaluation-order invariant around time.Now().
    • Keep call expressions rejected, preserving the existing safety boundary for potentially observable argument evaluation.
  • Suggested-fix behavior

    • Allow autofixes for common elapsed-time patterns such as field reads and indexed time values.
    • Reuse the existing time qualifier/alias handling unchanged.
  • Regression coverage

    • Add bad cases for selector and index arguments and corresponding golden rewrites.
    • Add good cases proving selector/index expressions that contain calls are still not flagged.

Example:

type state struct {
	start time.Time
}

func elapsed(s state, starts []time.Time, i int) {
	_ = time.Now().Sub(s.start)   // -> time.Since(s.start)
	_ = time.Now().Sub(starts[i]) // -> time.Since(starts[i])
}

Copilot AI and others added 2 commits July 20, 2026 07:53
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix coverage gap in timenowsub for side-effect-free selectors Expand timenowsub coverage to safe selector and index arguments Jul 20, 2026
Copilot AI requested a review from pelikhan July 20, 2026 08:03
@pelikhan
pelikhan marked this pull request as ready for review July 20, 2026 08:07
Copilot AI review requested due to automatic review settings July 20, 2026 08:07
@pelikhan
pelikhan merged commit b7b52ac into main Jul 20, 2026
20 checks passed
@pelikhan
pelikhan deleted the copilot/fix-timenowsub-coverage-gap branch July 20, 2026 08:07
Copilot stopped reviewing on behalf of pelikhan due to an error July 20, 2026 08:08

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.

Pull request overview

Note

Copilot could not run the full agentic suite for this review because it was automatically requested on a bot-authored pull request. Request a review from Copilot under Reviewers to retry with the full agentic suite. Improved support for bot-authored pull requests is coming soon.

This PR broadens the timenowsub linter’s notion of “safe” arguments so it can suggest simplifying time.Now().Sub(x) to time.Since(x) for additional argument shapes (selectors and indexing), and updates the linter’s golden test fixtures accordingly. It also refreshes the pinned versions in the generated VulnHunter workflow lockfile.

Changes:

  • Extend isSafeSinceArg to treat selector, index, pointer dereference, and basic literal expressions as “safe”.
  • Add/adjust timenowsub test cases + golden output for selector/index arguments.
  • Bump versions/pins in .github/workflows/daily-vulnhunter-scan.lock.yml.
Show a summary per file
File Description
pkg/linters/timenowsub/timenowsub.go Expands “safe argument” AST patterns used to decide when to suggest time.Since(...).
pkg/linters/timenowsub/testdata/src/timenowsub/timenowsub.go Adds new test scenarios for selector/index arguments and ensures some call-based variants remain “good”.
pkg/linters/timenowsub/testdata/src/timenowsub/timenowsub.go.golden Updates expected diagnostics/fixes for the new scenarios.
.github/workflows/daily-vulnhunter-scan.lock.yml Updates generated lockfile pins (AWF + Claude Code versions, container digests).

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Low

Comment on lines +139 to +144
case *ast.SelectorExpr:
return isSafeSinceArg(e.X)
case *ast.IndexExpr:
return isSafeSinceArg(e.X) && isSafeSinceArg(e.Index)
case *ast.StarExpr:
return isSafeSinceArg(e.X)
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.82.14

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

Labels

None yet

Projects

None yet

3 participants