Skip to content

s1024: don't suggest an invalid fix when Sub is not called on a time.Time - #1730

Open
Eljees wants to merge 1 commit into
dominikh:masterfrom
Eljees:fix/756-s1024-invalid-fix
Open

s1024: don't suggest an invalid fix when Sub is not called on a time.Time#1730
Eljees wants to merge 1 commit into
dominikh:masterfrom
Eljees:fix/756-s1024-invalid-fix

Conversation

@Eljees

@Eljees Eljees commented Aug 6, 2026

Copy link
Copy Markdown

Fixes #756.

S1024 rewrites x.Sub(time.Now()) into time.Until(x), which only compiles when x is itself a time.Time. Sub is reachable in two other shapes, and the fix produces code that does not build in either:

type embedTime struct{ time.Time }

func fnEmbedded(t embedTime) { t.Sub(time.Now()) } // fix: time.Until(t)
func fnPointer(t *time.Time) { t.Sub(time.Now()) } // fix: time.Until(t)

The pointer case is not in the issue, but it has the same cause.

Emitting time.Until(t.Time) for the embedded case would be nicer than dropping the fix, but the embedded field is not always nameable at the use site — it can be unexported and declared in another package — so this keeps the diagnostic and reports it without a fix instead.

The testdata gains both shapes. On the current tree the golden file fails with exactly the two invalid rewrites.

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.

S1024: incorrect suggested fix for embedded type

1 participant