-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathPrintLnArray.ql
More file actions
40 lines (37 loc) · 1.11 KB
/
PrintLnArray.ql
File metadata and controls
40 lines (37 loc) · 1.11 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
34
35
36
37
38
39
40
/**
* @name Implicit conversion from array to string
* @description Directly printing an array, without first converting the array to a string,
* produces unreadable results.
* @kind problem
* @problem.severity recommendation
* @precision very-high
* @id java/print-array
* @tags quality
* reliability
* correctness
*/
import java
import semmle.code.java.StringFormat
/**
* Holds if `e` is an argument of `Arrays.toString(..)`.
*/
predicate arraysToStringArgument(Expr e) {
exists(MethodCall ma, Method m |
ma.getAnArgument() = e and
ma.getMethod() = m and
m.getDeclaringType().hasQualifiedName("java.util", "Arrays") and
m.hasName("toString")
)
}
from Expr arr
where
arr.getType() instanceof Array and
implicitToStringCall(arr) and
not exists(FormattingCall fmtcall |
// exclude slf4j formatting as it supports array formatting
fmtcall.getAnArgumentToBeFormatted() = arr and fmtcall.getSyntax().isLogger()
)
or
arr.getType().(Array).getComponentType() instanceof Array and
arraysToStringArgument(arr)
select arr, "Implicit conversion from Array to String."