-
Notifications
You must be signed in to change notification settings - Fork 424
fix(regexpcompileinfunction): resolve package identity via type checker instead of identifier name #39773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+63
−5
Merged
fix(regexpcompileinfunction): resolve package identity via type checker instead of identifier name #39773
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
01cad7e
Initial plan
Copilot 979edab
Plan: fix isRegexpCompileCall to use type-checker identity
Copilot 44be2f8
fix: resolve regexp package identity via type checker in regexpcompil…
Copilot e16e2a5
fix: address review feedback on regexpcompileinfunction linter
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
pkg/linters/regexpcompileinfunction/testdata/src/regexpcompileinfunction/alias_import.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package regexpcompileinfunction | ||
|
|
||
| import re "regexp" | ||
|
|
||
| // flagged: aliased import of regexp — alias must not cause a false negative. | ||
| func ProcessWithAlias(s string) bool { | ||
| r := re.MustCompile(`^[a-z]+$`) // want `regexp compilation inside function should be moved to package-level variable` | ||
| return r.MatchString(s) | ||
| } | ||
|
|
||
| func ValidateWithAlias(input string) (bool, error) { | ||
| r, err := re.Compile(`\d+`) // want `regexp compilation inside function should be moved to package-level variable` | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return r.MatchString(input), nil | ||
| } | ||
|
|
||
| // not flagged: aliased import at package level is fine. | ||
| var packageLevelAliasRegexp = re.MustCompile(`^[a-z]+$`) | ||
11 changes: 11 additions & 0 deletions
11
pkg/linters/regexpcompileinfunction/testdata/src/regexpcompileinfunction/dot_import.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package regexpcompileinfunction | ||
|
|
||
| import . "regexp" | ||
|
|
||
| // NOT flagged: dot imports produce bare identifier calls, not selector expressions, | ||
| // so isRegexpCompileCall exits early at the *ast.SelectorExpr guard. | ||
| // This is a known limitation of the linter. | ||
| func DotImportExample() bool { | ||
| r := MustCompile(`^[a-z]+$`) // not flagged: dot-import calls are not selector expressions | ||
| return r.MatchString("abc") | ||
| } |
14 changes: 14 additions & 0 deletions
14
...nters/regexpcompileinfunction/testdata/src/regexpcompileinfunction/shadowed_identifier.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package regexpcompileinfunction | ||
|
|
||
| // customRegexp is an unrelated type that happens to have Compile/MustCompile methods. | ||
| type customRegexp struct{} | ||
|
|
||
| func (customRegexp) Compile(_ string) (*customRegexp, error) { return &customRegexp{}, nil } | ||
| func (customRegexp) MustCompile(_ string) *customRegexp { return &customRegexp{} } | ||
|
|
||
| // not flagged: local variable named "regexp" is not the stdlib regexp package. | ||
| func GoodShadowedRegexpIdentifier() bool { | ||
| regexp := customRegexp{} | ||
| _ = regexp.MustCompile(`^[a-z]+$`) | ||
| return true | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undocumented false negative:
import . "regexp"is silently skipped. With a dot import, call sites are bare identifiers (MustCompile(...)) rather than selector expressions, soisRegexpCompileCallexits early at the*ast.SelectorExprguard and produces no diagnostic.💡 Details
This is a pre-existing limitation, not introduced by this PR, but the PR adds test fixtures specifically targeting import-aliasing edge cases without mentioning this gap. Either add a comment in the function doc noting the limitation, or add a
dot_import.gofixture that documents the known miss:Without this, a future refactor that adds dot-import detection would have no regression test to verify the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
dot_import.goin the latest commit to document this known limitation. The fixture confirms dot-import calls are silently skipped (no// wantannotation) and includes a comment explaining why: dot imports produce bare identifier calls, not selector expressions, soisRegexpCompileCallexits early at the*ast.SelectorExprguard.