regexp: warn when rx argument is not string or *regexp.Regexp#292
Open
mmorel-35 wants to merge 4 commits into
Open
regexp: warn when rx argument is not string or *regexp.Regexp#292mmorel-35 wants to merge 4 commits into
mmorel-35 wants to merge 4 commits into
Conversation
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
ccoVeille
approved these changes
Mar 14, 2026
dolmen
suggested changes
May 24, 2026
dolmen
left a comment
There was a problem hiding this comment.
We must only accept:
- any
*types.TypeParam *types.Basicthat is strictstring(BasicKind isStringorUntypedString) (but NOT anything that isstringunderlying)- any
*types.Aliasto above
The current implementation is too lax and doesn't handle TypeParam.
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
ccoVeille
reviewed
May 26, 2026
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, | ||
| }, | ||
| }, |
Contributor
There was a problem hiding this comment.
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>
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.
assert.Regexp/assert.NotRegexpacceptrx interface{}, but testify'smatchRegexponly handles*regexp.Regexpandstringcorrectly — anything else silently goes throughregexp.MustCompile(fmt.Sprint(rx)), which produces unexpected results (e.g.[]byte("pattern")compiles to its decimal byte representation).Changes
internal/checkers/regexp.go: ExtendedCheck()to emit"use string or *regexp.Regexp as the first argument"whenrxis neither a string type (typed, untyped, or string-underlying alias) nor*regexp.Regexp. AddedisStringOrRegexpTypehelper.internal/testgen/gen_regexp.go: Added invalid test cases ([]bytearg) and valid test cases (pre-compiled*regexp.Regexpvariable); updated template to declarecompiledRegexp.Closes #81