forbidigo icon indicating copy to clipboard operation
forbidigo copied to clipboard

analyze_types: better support for type aliases

Open pohly opened this issue 2 years ago • 2 comments

The recently merged semantic analysis has one drawback when dealing with type aliases: they always get replaced with the underlying type. Example:

package somepkg

import "example.com/anotherpkg"

type SomeType = anotherpkg.AnotherType

A pattern somepkg.SomeType.ForbiddenMethod does not match because SomeType is an alias. What works is anotherpkg.AnotherType.ForbiddenMethod.

This is a) unexpected and b) can cause a pattern that works with a version of a package where the type is not an alias to stop working when the packages switches to a type alias.

It would be better if forbidigo matched against both the underlying type (anotherpkg.AnotherType) and the alias (somepkg.SomeType).

pohly avatar Jan 29 '23 16:01 pohly

The problem is that in many cases, we get the underlying type from static code analysis:

https://github.com/ashanbrown/forbidigo/blob/1396000fa337b0f684b38a5206b36964d9573b39/forbidigo/forbidigo.go#L273-L285

I'm not sure whether it is possible to detect that the type was reached through an alias.

pohly avatar Jan 29 '23 17:01 pohly