-
Notifications
You must be signed in to change notification settings - Fork 2
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
Only flag regexp calls that use values known at compile time #1
Only flag regexp calls that use values known at compile time #1
Conversation
Do not flag function calls that have variables or function calls in their arguments, as such code cannot be refactored to only be called once.
Hi, came across your project and wanted to make a suggestion. It makes sense for a linter to flag cases where a regular expression is being compiled when the content is known at program compile time, but if a program compiles a regular expression based on variables or function calls (based on user input, file content, etc.), then it probably doesn't make sense to flag those occurrences, since they are valid and can't be compiled in a way that satisfies this check. Although the check provides a comment suppression mechanism, it probably makes more sense to handle this directly. This PR contains an example of how this modification could be made by doing an extra AST check after the initial "called" check. |
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.
I appreciate the great PR!
@@ -17,7 +20,7 @@ const doc = `Below functions should be called at once for performance. | |||
- regexp.CompilePOSIX | |||
- regexp.MustCompilePOSIX | |||
|
|||
Allow call in init, and main(except for in for loop) functions because each function is called only once. | |||
Allow call in init and main functions (unless call is in a for loop) because these functions are only called once. |
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.
👍
|
||
regexp.MustCompile(constVal) // want `regexp.MustCompile must be called only once at initialize` | ||
regexp.MustCompile(constVal + constVal) // want `regexp.MustCompile must be called only once at initialize` | ||
} |
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.
excellent test cases!
@@ -73,6 +111,30 @@ func run(pass *analysis.Pass) (interface{}, error) { | |||
return nil, nil | |||
} | |||
|
|||
// variablesOrCallInCallExpr returns true if the provided *ast.CallExpr has | |||
// at least one argument and if the first argument contains a reference to a | |||
// variable or a function call. |
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.
These comment are easy for me to understand. Thank you for explaining in comments.
and, sorry for review so late... |
I released new version included this PR! |
Do not flag function calls that have variables or function calls
in their arguments, as such code cannot be refactored to only be
called once.