forked from Marcono1234/codeql-java-queries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBuilder-append-String-repeat.ql
More file actions
26 lines (24 loc) · 1.2 KB
/
Copy pathStringBuilder-append-String-repeat.ql
File metadata and controls
26 lines (24 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Finds code which first repeats a String using `String#repeat` and then appends it to
* a `StringBuilder` or `StringBuffer`.
*
* Since Java 21 `StringBuilder` and `StringBuffer` have new `repeat` methods, which can
* be used instead and likely provide better performance.
*
* @id TODO
* @kind problem
*/
import java
from MethodAccess stringRepeatCall, Method stringRepeatMethod, MethodAccess stringBuilderAppendCall
where
stringRepeatCall.getMethod() = stringRepeatMethod and
stringRepeatMethod.getDeclaringType() instanceof TypeString and
stringRepeatMethod.hasStringSignature("repeat(int)") and
stringBuilderAppendCall.getReceiverType() instanceof StringBuildingType and
stringBuilderAppendCall.getMethod().hasName("append") and
// For now only cover `repeat` result directly being used as argument for `append`; that already has
// a lot of findings. Could instead use local dataflow, but this causes false positives then if `repeat`
// result is used multiple times and cannot be replaced with `StringBuilder#repeat`.
stringRepeatCall = stringBuilderAppendCall.getAnArgument()
select stringRepeatCall,
"Can instead use " + stringBuilderAppendCall.getReceiverType().getName() + "#repeat"