Skip to content

regexp: warn when rx argument is not string or *regexp.Regexp#292

Open
mmorel-35 wants to merge 4 commits into
Antonboom:masterfrom
mmorel-35:regexp
Open

regexp: warn when rx argument is not string or *regexp.Regexp#292
mmorel-35 wants to merge 4 commits into
Antonboom:masterfrom
mmorel-35:regexp

Conversation

@mmorel-35

@mmorel-35 mmorel-35 commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

assert.Regexp/assert.NotRegexp accept rx interface{}, but testify's matchRegexp only handles *regexp.Regexp and string correctly — anything else silently goes through regexp.MustCompile(fmt.Sprint(rx)), which produces unexpected results (e.g. []byte("pattern") compiles to its decimal byte representation).

Changes

  • internal/checkers/regexp.go: Extended Check() to emit "use string or *regexp.Regexp as the first argument" when rx is neither a string type (typed, untyped, or string-underlying alias) nor *regexp.Regexp. Added isStringOrRegexpType helper.
  • internal/testgen/gen_regexp.go: Added invalid test cases ([]byte arg) and valid test cases (pre-compiled *regexp.Regexp variable); updated template to declare compiledRegexp.
// Now flagged:
assert.Regexp(t, []byte(`\w+`), output) // regexp: use string or *regexp.Regexp as the first argument

// Valid — no warning:
assert.Regexp(t, `\w+`, output)
assert.Regexp(t, compiledRegexp, output)

Closes #81

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

@dolmen dolmen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We must only accept:

  • any *types.TypeParam
  • *types.Basic that is strict string (BasicKind is String or UntypedString) (but NOT anything that is string underlying)
  • any *types.Alias to above

The current implementation is too lax and doesn't handle TypeParam.

Comment thread internal/checkers/regexp.go Outdated
Comment thread internal/checkers/regexp.go Outdated
Comment thread internal/checkers/regexp.go Outdated
Comment thread internal/checkers/regexp.go Outdated
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Comment on lines +40 to 71
InvalidTypeAssertions: []Assertion{
{
Fn: "Regexp",
Argsf: "[]byte(`\\w+`), out",
ReportMsgf: reportInvalidArg,
},
{
Fn: "NotRegexp",
Argsf: "[]byte(`\\w+`), out",
ReportMsgf: reportInvalidArg,
},
{
Fn: "Regexp",
Argsf: "myDefinedString(`\\w+`), out",
ReportMsgf: reportInvalidArg,
},
{
Fn: "NotRegexp",
Argsf: "myDefinedString(`\\w+`), out",
ReportMsgf: reportInvalidArg,
},
{
Fn: "Regexp",
Argsf: "rxRegexpStructAlias, out",
ReportMsgf: reportInvalidArg,
},
{
Fn: "NotRegexp",
Argsf: "rxRegexpStructAlias, out",
ReportMsgf: reportInvalidArg,
},
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add the following to tests

  • using 42 as an integer (typed or untyped, signed or not
  • using float
  • stringer

All of these rely on the fmt.Sprint that is done with non string or *regexp.Regexp

type foo struct{}

func (foo) String() string {
	return "42"
}

func TestFoo(t *testing.T) {
  assert.NotRegexp(t, false, "false")
	assert.NotRegexp(t, 42, "42")
	assert.NotRegexp(t, 42.0, "42")
	assert.NotRegexp(t, int8(42), "42")
	assert.NotRegexp(t, foo{}, "42")
}

All of these relies on something that would be better to avoid.

https://goplay.tools/snippet/Mmo4V36BHD1

Maybe something specific to stringer could be considered as a stringer can be rewritten to use .String()

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Copilot AI added a commit to mmorel-35/testifylint that referenced this pull request May 28, 2026
mmorel-35 pushed a commit to mmorel-35/testifylint that referenced this pull request May 28, 2026
@Antonboom Antonboom added the llm-based LLM shit label Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-based LLM shit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

regexp: add type checks for assert.Regexp regexp argument

4 participants