Skip to content

Commit

Permalink
Java: Improve UselessTypeTest.ql
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Apr 19, 2021
1 parent 5c2bf68 commit 7b24846
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
9 changes: 8 additions & 1 deletion java/ql/src/Language Abuse/UselessTypeTest.ql
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@

import java

// This matches more than just `getAnAcestor()` or `getASourceSupertype*()`, mainly
// cases where raw types are used
RefType getSelfOrSupertype(RefType t) {
result = t or
result = getSelfOrSupertype([t.getSourceDeclaration(), t.getASupertype()])
}

from InstanceOfExpr ioe, RefType t, RefType ct
where
t = ioe.getExpr().getType() and
ct = ioe.getTypeName().getType() and
ct = t.getASupertype+()
ct = getSelfOrSupertype(t)
select ioe,
"There is no need to test whether an instance of $@ is also an instance of $@ - it always is.", t,
t.getName(), ct, ct.getName()
29 changes: 29 additions & 0 deletions java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
| 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 |
| 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 |
| 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> |
| 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<?> |
| 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> |
| 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> |
| 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> |
| 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> |
| 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<> |
| 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<?> |
| 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<> |
| 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<?> |
| 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<> |
| 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<> |
| 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<?> |
| 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<> |
| 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<?> |
| 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<> |
| 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<> |
| 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<?> |
| 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> |
| 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<?> |
| 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<> |
| 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<> |
| 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<?> |
| 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[] |
| 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 |
| 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[] |
| 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 |
52 changes: 52 additions & 0 deletions java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

class UselessTypeTest {
static class ExtendingRaw extends ArrayList {
}

Object obj = null;
boolean[] bad = {
"" instanceof String,
"" instanceof Object,
new ArrayList<String>() instanceof ArrayList<String>,
new ArrayList<String>() instanceof ArrayList<?>,
new ArrayList<String>() instanceof ArrayList<? super String>,
new ArrayList<String>() instanceof ArrayList<? extends String>,
new ArrayList<String>() instanceof List<? super String>,
new ArrayList<String>() instanceof List<? extends String>,
((ArrayList<?>) obj) instanceof List,
((ArrayList<? super String>) obj) instanceof List<?>,
((ArrayList<? super String>) obj) instanceof List,
((ArrayList<? extends String>) obj) instanceof List<?>,
((ArrayList<? extends String>) obj) instanceof List,
((List<?>) obj) instanceof List,
((List<? super String>) obj) instanceof List<?>,
((List<? super String>) obj) instanceof List,
((List<? extends String>) obj) instanceof List<?>,
((List<? extends String>) obj) instanceof List,
new ArrayList<String>() instanceof ArrayList,
new ArrayList() instanceof ArrayList<?>,
new ArrayList<String>() instanceof List<String>,
new ArrayList<String>() instanceof List<?>,
new ArrayList<String>() instanceof List,
new ExtendingRaw() instanceof ArrayList,
new ExtendingRaw() instanceof ArrayList<?>,
new String[0] instanceof String[],
new String[0] instanceof Object,
new String[0] instanceof Object[],
new int[0] instanceof Object,
};

boolean[] good = {
new Object() instanceof Number,
new ArrayList<String>() instanceof Map<?, ?>,
new ArrayList<String>() instanceof Map,
((List<? super String>) obj) instanceof ArrayList<?>,
((List<? super String>) obj) instanceof ArrayList<? super String>,
new Object() instanceof int[],
new Object() instanceof Object[],
new Object[0] instanceof String[],
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Language Abuse/UselessTypeTest.ql
1 change: 1 addition & 0 deletions java/ql/test/query-tests/UselessTypeTest/options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --enable-preview -source 15 -target 15

0 comments on commit 7b24846

Please sign in to comment.