File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
codeql-custom-queries-java/queries Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Finds calls to `String.join` with no or only one argument to join. In that case no joining
3+ * is actually performed.
4+ *
5+ * For example:
6+ * ```java
7+ * // is equivalent to just `a` (respectively `String.valueOf(a)`)
8+ * String.join("-", a)
9+ * ```
10+ *
11+ * @id todo
12+ * @kind problem
13+ */
14+
15+ import java
16+
17+ from MethodAccess joinCall , Method joinMethod
18+ where
19+ joinCall .getMethod ( ) = joinMethod and
20+ joinMethod .getDeclaringType ( ) instanceof TypeString and
21+ joinMethod .hasName ( "join" ) and
22+ joinMethod .isStatic ( ) and
23+ // Only cover the varargs overload `String.join(CharSequence, CharSequence...)`
24+ joinMethod .isVarargs ( ) and
25+ (
26+ joinCall .getNumArgument ( ) = 1
27+ or
28+ joinCall .getNumArgument ( ) = 2 and
29+ not joinCall .getArgument ( 1 ) .getType ( ) instanceof Array
30+ )
31+ select joinCall , "Does not perform any joining"
Original file line number Diff line number Diff line change 1+ /**
2+ * Finds calls to `String.join` with only two arguments to join, instead of performing
3+ * string concatenation.
4+ *
5+ * For example:
6+ * ```java
7+ * String.join("-", a, b)
8+ * // can be simplified to
9+ * a + "-" + b
10+ * ```
11+ *
12+ * @id todo
13+ * @kind problem
14+ */
15+
16+ import java
17+
18+ from MethodAccess joinCall , Method joinMethod
19+ where
20+ joinCall .getMethod ( ) = joinMethod and
21+ joinMethod .getDeclaringType ( ) instanceof TypeString and
22+ joinMethod .hasName ( "join" ) and
23+ joinMethod .isStatic ( ) and
24+ joinMethod .isVarargs ( ) and
25+ // Separator + 2 varargs args
26+ joinCall .getNumArgument ( ) = 3
27+ select joinCall , "Can be replaced with string concatenation"
You can’t perform that action at this time.
0 commit comments