Skip to content

Conversation

@Alhanaqtah
Copy link
Contributor

@Alhanaqtah Alhanaqtah commented Nov 13, 2025

Description

I added a check to the parser for whether the interface is a constraint. Mocks will not be generated using such interfaces.

Examples of interfaces that will be skipped:

type Skip1 constraints.Ordered

type Skip2 interface {
	~int
}

type Skip3 interface {
	constraints.Float
}

type Skip4 interface {
	constraints.Float | constraints.Integer
}

type Skip5 Skip1

type Skip6 interface {
	Skip5
}

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Version of Go used when building/testing:

  • 1.25.1

How Has This Been Tested?

I wrote a test in fixtures with several constraint interfaces. If no mocks are generated for them, the test will be completed successfully.
Fixture path: internal/fixtures/constraint_ifaces

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Comment on lines 206 to 215
// Example:
// type I interface {
// constraints.Float
// }
if sel, ok := item.Type.(*ast.SelectorExpr); ok {
if id, ok := sel.X.(*ast.Ident); ok && id.Name == "constraints" {
return true
}
}
}
Copy link
Member

@LandonTClipp LandonTClipp Nov 13, 2025

Choose a reason for hiding this comment

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

I think this is fine but it's easily tricked. You can create an alias to a constraints.Float and this check would not catch it. You would need an additional check for alias types, then resolve the alias, then perform the same checks you added here.

That doesn't have to be included in this PR, but it's something to think about. You mention this in the description so I'm not telling you something new.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi. Thanks for the tip. Thanks to this, i also found a way to check aliases for constraint interfaces

@Alhanaqtah Alhanaqtah marked this pull request as ready for review November 15, 2025 12:22
@LandonTClipp
Copy link
Member

I haven't forgotten this, I will get to it soon!

@LandonTClipp LandonTClipp merged commit c94443b into vektra:v3 Dec 16, 2025
6 checks passed
@LandonTClipp
Copy link
Member

Merged, thanks again 🫡

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.

Mocks generated for constraint interfaces/anys

2 participants