File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @name Use of regexp to match a set of constant string
3+ * @description Comparing against constant strings instead of a regexp can improve performance
4+ * @kind problem
5+ * @problem.severity recommendation
6+ * @id ql/use-string-compare
7+ * @tags maintainability
8+ * @precision high
9+ */
10+
11+ import ql
12+ import codeql_ql.ast.internal.Type
13+
14+ predicate problem ( MemberCall call ) {
15+ call .getBase ( ) .getType ( ) .getASuperType * ( ) .( PrimitiveType ) .getName ( ) = "string" and
16+ (
17+ call .getMemberName ( ) = "regexpMatch" and
18+ call .getNumberOfArguments ( ) = 1 and
19+ call .getArgument ( 0 ) .( String ) .getValue ( ) .regexpMatch ( "([a-zA-Z0-9]+\\|)*[a-zA-Z0-9]+" )
20+ or
21+ exists ( string reg | call .getMemberName ( ) = "matches" |
22+ call .getNumberOfArguments ( ) = 1 and
23+ reg = call .getArgument ( 0 ) .( String ) .getValue ( ) and
24+ not reg .regexpMatch ( ".*(%|_).*" )
25+ )
26+ )
27+ }
28+
29+ from AstNode node
30+ where problem ( node )
31+ select node , "Use string comparison instead of regexp to compare against a constant set of string."
You can’t perform that action at this time.
0 commit comments