File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
codeql-custom-queries-java/queries/performance Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Finds code which first repeats a String using `String#repeat` and then appends it to
3
+ * a `StringBuilder` or `StringBuffer`.
4
+ *
5
+ * Since Java 21 `StringBuilder` and `StringBuffer` have new `repeat` methods, which can
6
+ * be used instead and likely provide better performance.
7
+ *
8
+ * @id TODO
9
+ * @kind problem
10
+ */
11
+
12
+ import java
13
+
14
+ from MethodAccess stringRepeatCall , Method stringRepeatMethod , MethodAccess stringBuilderAppendCall
15
+ where
16
+ stringRepeatCall .getMethod ( ) = stringRepeatMethod and
17
+ stringRepeatMethod .getDeclaringType ( ) instanceof TypeString and
18
+ stringRepeatMethod .hasStringSignature ( "repeat(int)" ) and
19
+ stringBuilderAppendCall .getReceiverType ( ) instanceof StringBuildingType and
20
+ stringBuilderAppendCall .getMethod ( ) .hasName ( "append" ) and
21
+ // For now only cover `repeat` result directly being used as argument for `append`; that already has
22
+ // a lot of findings. Could instead use local dataflow, but this causes false positives then if `repeat`
23
+ // result is used multiple times and cannot be replaced with `StringBuilder#repeat`.
24
+ stringRepeatCall = stringBuilderAppendCall .getAnArgument ( )
25
+ select stringRepeatCall ,
26
+ "Can instead use " + stringBuilderAppendCall .getReceiverType ( ) .getName ( ) + "#repeat"
You can’t perform that action at this time.
0 commit comments