Skip to content

fix: replace intPtr with generic ptr[T any] to resolve modernize/newexpr lint failures#17

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-lint-failures-in-unit-tests
Open

fix: replace intPtr with generic ptr[T any] to resolve modernize/newexpr lint failures#17
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-lint-failures-in-unit-tests

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 4, 2026

golangci-lint (modernize/newexpr) flagged the type-specific intPtr helper in reconcile_unit_test.go as a thin wrapper around new(int), blocking CI.

Changes

  • internal/controller/reconcile_unit_test.go: Replace func intPtr(v int) *int with a generic func ptr[T any](v T) *T; update all call sites (intPtr(120)ptr(120), etc.)
// before
func intPtr(v int) *int { return &v }

// after
func ptr[T any](v T) *T { return &v }

The generic form is not flagged by newexpr since it is not a single-type wrapper around new(T). No logic changes.

Original prompt

A GitHub Actions lint job is failing due to golangci-lint (modernize/newexpr) findings in unit tests.

Failing job/run:

Logs show these errors:

  • internal/controller/reconcile_unit_test.go:40:6: newexpr: intPtr can be an inlinable wrapper around new(expr) (modernize)
    func intPtr(v int) *int { return &v }
  • internal/controller/reconcile_unit_test.go:398:21: newexpr: call of intPtr(x) can be simplified to new(x) (modernize)
    IntervalSeconds: intPtr(120),
  • internal/controller/reconcile_unit_test.go:474:18: intPtr(5)
  • internal/controller/reconcile_unit_test.go:475:18: intPtr(30)

Task:

  1. Fix the lint failures in internal/controller/reconcile_unit_test.go by removing/avoiding the intPtr helper that triggers modernize/newexpr.
  2. Prefer a code-only fix (no linter config changes) unless necessary. A good approach is to replace intPtr with a generic helper like func ptr[T any](v T) *T { return &v } and update all call sites accordingly.
  3. Ensure make lint passes.
  4. Open a PR with the changes.

Acceptance criteria:

  • golangci-lint run passes in CI.
  • Tests compile; no behavior changes beyond pointer helper refactor.
  • PR includes clear title and description referencing the failing lint rule and affected file/lines.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Please find a solution for this failing job. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.

Assistant: The job is failing in make lint because golangci-lint (the modernize linter’s newexpr check) rejects a helper in your unit test and the call sites that use it.

Failing lines (ref: 95959b506f6d143861fd6487251a33baf873f1ab):

  • internal/controller/reconcile_unit_test.go:40
    func intPtr(v int) *int { return &v }
  • internal/controller/reconcile_unit_test.go:398, 474, 475
    uses like IntervalSeconds: intPtr(120) and intPtr(5) / intPtr(30).

Fix option A (recommended): remove intPtr and use an inline helper that modernize accepts

Because new(int) returns *int but does not let you set a value in one expression, the simplest lint-friendly approach is to replace intPtr with a generic “ptr” helper (modernize won’t flag this as “wrapper around new(expr)”).

Edit internal/controller/reconcile_unit_test.go (ref: 95959b506f6d143861fd6487251a33baf873f1ab):

  1. Replace the int-only helper:
// delete this
func intPtr(v int) *int { return &v }

with:

func ptr[T any](v T) *T { return &v }
  1. Update the call sites:
IntervalSeconds: ptr(120),

and in the table tests:

{"5s minimum", ptr(5), 5 * time.Second},
{"custom 30s", ptr(30), 30 * time.Second},
{"below minimum falls back to 60s", ptr(4), 60 * time.Second},
{"zero falls back to 60s", ptr(0), 60 * time.Second},
{"negative falls back to 60s", ptr(-1), 60 * time.Second},
{"large value accepted", ptr(3600), 3600 * time.Second},

This keeps the test intent identical and satisfies the linter.

Fix option B: keep code as-is, but exclude this rule for _test.go files

If you prefer to keep intPtr in tests, update your golangci-lint config to ignore modernize/newexpr for test files (e.g., via an exclude-rules entry scoped to *_test.go). This is valid, but option A is cleaner and avoids adding linter exceptions.

Why this is the right place to fix

The workflow .github/workflows/lint.yml runs make lint, and your Makefile target is simply:

lint:
	"$(GOLANGCI_LINT)" run

So the job will keep failing until the reported modernize findings in internal/controller/reconcile_unit_test.go are resolved (or configured to be ignored).

User: create PR to fix this

Assistant: I’ve kicked off the pull request creation to apply the lint-fix (replacing the intPtr helper in internal/controller/reconcile_unit_test.go with a lint-friendly pointer helper and updating its call sites), and I’m waiting for your confirmation/approval in the prompted dialog to proceed and open the PR.

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Fix golangci-lint failures in unit tests fix: replace intPtr with generic ptr[T any] to resolve modernize/newexpr lint failures May 4, 2026
Copilot finished work on behalf of rtoma May 4, 2026 14:29
Copilot AI requested a review from rtoma May 4, 2026 14:29
@rtoma rtoma marked this pull request as ready for review May 4, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants