-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStringBuilder-append-String-concat.ql
More file actions
33 lines (30 loc) · 1.02 KB
/
Copy pathStringBuilder-append-String-concat.ql
File metadata and controls
33 lines (30 loc) · 1.02 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
27
28
29
30
31
32
33
/**
* Finds calls to the `append(...)` method of `StringBuilder` or `StringBuffer`
* with a String concatenation expression as argument.
* String concatenation joins the two Strings and then creates the result
* String. Since that result is then appended, it would be more efficient to
* separately append the arguments, which would avoid the intermediate String
* object.
* E.g.:
* ```
* // Potentially inefficient
* sb.append("key=" + value);
* ```
* Should be written as:
* ```
* sb.append("key=").append(value);
* ```
*/
import java
// TODO: Remove code duplication; already exists in manual-CharSequence-joining.ql
class StringAppendingMethod extends Method {
StringAppendingMethod() {
getDeclaringType() instanceof StringBuildingType
and hasName("append")
}
}
from AddExpr concatExpr
where
concatExpr.getType() instanceof TypeString
and concatExpr.getParent().(MethodAccess).getMethod() instanceof StringAppendingMethod
select concatExpr, "String concatenation as `append(...)` argument"