Skip to content

Commit 8ef64a8

Browse files
committed
Java: Improve UselessTypeTest.ql
1 parent 5c2bf68 commit 8ef64a8

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

java/ql/src/Language Abuse/UselessTypeTest.ql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212

1313
import java
1414

15+
// This matches more than just `getAnAcestor()` or `getASourceSupertype*()`
16+
RefType getSelfOrSupertype(RefType t) {
17+
result = t or
18+
result = getSelfOrSupertype([t.getSourceDeclaration(), t.getASupertype()])
19+
}
20+
1521
from InstanceOfExpr ioe, RefType t, RefType ct
1622
where
1723
t = ioe.getExpr().getType() and
1824
ct = ioe.getTypeName().getType() and
19-
ct = t.getASupertype+()
25+
ct = getSelfOrSupertype(t)
2026
select ioe,
2127
"There is no need to test whether an instance of $@ is also an instance of $@ - it always is.", t,
2228
t.getName(), ct, ct.getName()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
| UselessTypeTest.java:11:9:11:28 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/lang/String.class:0:0:0:0 | String | String | file:///modules/java.base/java/lang/String.class:0:0:0:0 | String | String |
2+
| UselessTypeTest.java:12:9:12:28 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/lang/String.class:0:0:0:0 | String | String | file:///modules/java.base/java/lang/Object.class:0:0:0:0 | Object | Object |
3+
| UselessTypeTest.java:13:9:13:60 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> |
4+
| UselessTypeTest.java:14:9:14:55 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<?> | ArrayList<?> |
5+
| UselessTypeTest.java:15:9:15:68 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<? super String> | ArrayList<? super String> |
6+
| UselessTypeTest.java:16:9:16:70 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<? extends String> | ArrayList<? extends String> |
7+
| UselessTypeTest.java:17:9:17:63 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<? super String> | List<? super String> |
8+
| UselessTypeTest.java:18:9:18:65 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<? extends String> | List<? extends String> |
9+
| UselessTypeTest.java:19:9:19:44 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<?> | ArrayList<?> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<> | List<> |
10+
| UselessTypeTest.java:20:9:20:60 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<? super String> | ArrayList<? super String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<?> | List<?> |
11+
| UselessTypeTest.java:21:9:21:57 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<? super String> | ArrayList<? super String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<> | List<> |
12+
| UselessTypeTest.java:22:9:22:62 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<? extends String> | ArrayList<? extends String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<?> | List<?> |
13+
| UselessTypeTest.java:23:9:23:59 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<? extends String> | ArrayList<? extends String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<> | List<> |
14+
| UselessTypeTest.java:24:9:24:39 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<?> | List<?> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<> | List<> |
15+
| UselessTypeTest.java:25:9:25:55 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<? super String> | List<? super String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<?> | List<?> |
16+
| UselessTypeTest.java:26:9:26:52 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<? super String> | List<? super String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<> | List<> |
17+
| UselessTypeTest.java:27:9:27:57 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<? extends String> | List<? extends String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<?> | List<?> |
18+
| UselessTypeTest.java:28:9:28:54 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<? extends String> | List<? extends String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<> | List<> |
19+
| UselessTypeTest.java:29:9:29:52 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<> | ArrayList<> |
20+
| UselessTypeTest.java:30:9:30:47 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<> | ArrayList<> | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<?> | ArrayList<?> |
21+
| UselessTypeTest.java:31:9:31:55 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<String> | List<String> |
22+
| UselessTypeTest.java:32:9:32:50 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<?> | List<?> |
23+
| UselessTypeTest.java:33:9:33:47 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<String> | ArrayList<String> | file:///modules/java.base/java/util/List.class:0:0:0:0 | List<> | List<> |
24+
| UselessTypeTest.java:34:9:34:47 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | UselessTypeTest.java:6:18:6:29 | ExtendingRaw | ExtendingRaw | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<> | ArrayList<> |
25+
| UselessTypeTest.java:35:9:35:50 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | UselessTypeTest.java:6:18:6:29 | ExtendingRaw | ExtendingRaw | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList<?> | ArrayList<?> |
26+
| UselessTypeTest.java:36:9:36:41 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file://:0:0:0:0 | String[] | String[] | file://:0:0:0:0 | String[] | String[] |
27+
| UselessTypeTest.java:37:9:37:39 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file://:0:0:0:0 | String[] | String[] | file:///modules/java.base/java/lang/Object.class:0:0:0:0 | Object | Object |
28+
| UselessTypeTest.java:38:9:38:41 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file://:0:0:0:0 | String[] | String[] | file://:0:0:0:0 | Object[] | Object[] |
29+
| UselessTypeTest.java:39:9:39:36 | ...instanceof... | There is no need to test whether an instance of $@ is also an instance of $@ - it always is. | file://:0:0:0:0 | int[] | int[] | file:///modules/java.base/java/lang/Object.class:0:0:0:0 | Object | Object |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Map;
4+
5+
class UselessTypeTest {
6+
static class ExtendingRaw extends ArrayList {
7+
}
8+
9+
Object obj = null;
10+
boolean[] bad = {
11+
"" instanceof String,
12+
"" instanceof Object,
13+
new ArrayList<String>() instanceof ArrayList<String>,
14+
new ArrayList<String>() instanceof ArrayList<?>,
15+
new ArrayList<String>() instanceof ArrayList<? super String>,
16+
new ArrayList<String>() instanceof ArrayList<? extends String>,
17+
new ArrayList<String>() instanceof List<? super String>,
18+
new ArrayList<String>() instanceof List<? extends String>,
19+
((ArrayList<?>) obj) instanceof List,
20+
((ArrayList<? super String>) obj) instanceof List<?>,
21+
((ArrayList<? super String>) obj) instanceof List,
22+
((ArrayList<? extends String>) obj) instanceof List<?>,
23+
((ArrayList<? extends String>) obj) instanceof List,
24+
((List<?>) obj) instanceof List,
25+
((List<? super String>) obj) instanceof List<?>,
26+
((List<? super String>) obj) instanceof List,
27+
((List<? extends String>) obj) instanceof List<?>,
28+
((List<? extends String>) obj) instanceof List,
29+
new ArrayList<String>() instanceof ArrayList,
30+
new ArrayList() instanceof ArrayList<?>,
31+
new ArrayList<String>() instanceof List<String>,
32+
new ArrayList<String>() instanceof List<?>,
33+
new ArrayList<String>() instanceof List,
34+
new ExtendingRaw() instanceof ArrayList,
35+
new ExtendingRaw() instanceof ArrayList<?>,
36+
new String[0] instanceof String[],
37+
new String[0] instanceof Object,
38+
new String[0] instanceof Object[],
39+
new int[0] instanceof Object,
40+
};
41+
42+
boolean[] good = {
43+
new Object() instanceof Number,
44+
new ArrayList<String>() instanceof Map<?, ?>,
45+
new ArrayList<String>() instanceof Map,
46+
((List<? super String>) obj) instanceof ArrayList<?>,
47+
((List<? super String>) obj) instanceof ArrayList<? super String>,
48+
new Object() instanceof int[],
49+
new Object() instanceof Object[],
50+
new Object[0] instanceof String[],
51+
};
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Language Abuse/UselessTypeTest.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args --enable-preview -source 15 -target 15

0 commit comments

Comments
 (0)