compares: cast untyped constants to contextual type in auto-fix - #300
Open
mmorel-35 wants to merge 2 commits into
Open
compares: cast untyped constants to contextual type in auto-fix#300mmorel-35 wants to merge 2 commits into
mmorel-35 wants to merge 2 commits into
Conversation
mmorel-35
force-pushed
the
compares-types
branch
from
March 15, 2026 18:25
24be52c to
4089b53
Compare
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
mmorel-35
force-pushed
the
compares-types
branch
from
March 15, 2026 18:28
4089b53 to
01da74c
Compare
mmorel-35
pushed a commit
to mmorel-35/testifylint
that referenced
this pull request
May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an untyped constant (e.g.
math.MaxInt16) appears in a binary expression alongside a typed variable (e.g.int32), Go contextually types the constant to match. The auto-fix blindly lifted both operands into a testify call, where the constant reverts to its default type (int), causing a runtime"Elements should be the same type"failure from testify'scompareTwoValues.Root cause
TypesInfo.Typesstores the contextual type — somath.MaxInt16appears asint32insideval <= math.MaxInt16. The revert tointonly happens when the constant is passed asinterface{}to a function, which the checker never saw.Changes
helpers_basic_type.go—untypedConstObjectType(): detects untyped constants by looking up their declared type viaTypesInfo.Uses(not the contextual form inTypesInfo.Types). Handles named constants, basic literals, and unary negation.helpers_format.go—formatAsCast(): formats aT(expr)cast string for use in text-edit auto-fixes.compares.go— before emitting the fix, compares each operand's default object type against its contextual type; when they differ, wraps the constant in an explicit cast.analyzer/testdata/src/compares-issue135/— regression tests covering both the cast-needed path and the normal same-type path, registered inanalyzer_test.go.Example
Closes #272
Closes #205
Closes #135