From 8ef64a81e81c4661900b1f37e8d0876323d251a6 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Mon, 19 Apr 2021 02:30:19 +0200 Subject: [PATCH] Java: Improve UselessTypeTest.ql --- java/ql/src/Language Abuse/UselessTypeTest.ql | 8 ++- .../UselessTypeTest/UselessTypeTest.expected | 29 +++++++++++ .../UselessTypeTest/UselessTypeTest.java | 52 +++++++++++++++++++ .../UselessTypeTest/UselessTypeTest.qlref | 1 + .../test/query-tests/UselessTypeTest/options | 1 + 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.expected create mode 100644 java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.java create mode 100644 java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.qlref create mode 100644 java/ql/test/query-tests/UselessTypeTest/options diff --git a/java/ql/src/Language Abuse/UselessTypeTest.ql b/java/ql/src/Language Abuse/UselessTypeTest.ql index fac42d3059611..804e2fc5db03e 100644 --- a/java/ql/src/Language Abuse/UselessTypeTest.ql +++ b/java/ql/src/Language Abuse/UselessTypeTest.ql @@ -12,11 +12,17 @@ import java +// This matches more than just `getAnAcestor()` or `getASourceSupertype*()` +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() diff --git a/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.expected b/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.expected new file mode 100644 index 0000000000000..abbfce36b475e --- /dev/null +++ b/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.expected @@ -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 | ArrayList | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList | ArrayList | +| 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 | ArrayList | 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 | ArrayList | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList | ArrayList | +| 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 | ArrayList | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList | ArrayList | +| 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 | ArrayList | file:///modules/java.base/java/util/List.class:0:0:0:0 | List | List | +| 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 | ArrayList | file:///modules/java.base/java/util/List.class:0:0:0:0 | List | List | +| 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 | ArrayList | 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 | ArrayList | 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 | ArrayList | 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 | ArrayList | 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 | List | 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 | List | 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 | List | 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 | List | 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 | ArrayList | 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 | ArrayList | file:///modules/java.base/java/util/List.class:0:0:0:0 | List | List | +| 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 | ArrayList | 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 | ArrayList | 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 | diff --git a/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.java b/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.java new file mode 100644 index 0000000000000..7440913218cc8 --- /dev/null +++ b/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.java @@ -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() instanceof ArrayList, + new ArrayList() instanceof ArrayList, + new ArrayList() instanceof ArrayList, + new ArrayList() instanceof ArrayList, + new ArrayList() instanceof List, + new ArrayList() instanceof List, + ((ArrayList) obj) instanceof List, + ((ArrayList) obj) instanceof List, + ((ArrayList) obj) instanceof List, + ((ArrayList) obj) instanceof List, + ((ArrayList) obj) instanceof List, + ((List) obj) instanceof List, + ((List) obj) instanceof List, + ((List) obj) instanceof List, + ((List) obj) instanceof List, + ((List) obj) instanceof List, + new ArrayList() instanceof ArrayList, + new ArrayList() instanceof ArrayList, + new ArrayList() instanceof List, + new ArrayList() instanceof List, + new ArrayList() 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() instanceof Map, + new ArrayList() instanceof Map, + ((List) obj) instanceof ArrayList, + ((List) obj) instanceof ArrayList, + new Object() instanceof int[], + new Object() instanceof Object[], + new Object[0] instanceof String[], + }; +} diff --git a/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.qlref b/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.qlref new file mode 100644 index 0000000000000..9380aa085af4e --- /dev/null +++ b/java/ql/test/query-tests/UselessTypeTest/UselessTypeTest.qlref @@ -0,0 +1 @@ +Language Abuse/UselessTypeTest.ql diff --git a/java/ql/test/query-tests/UselessTypeTest/options b/java/ql/test/query-tests/UselessTypeTest/options new file mode 100644 index 0000000000000..81817b3778525 --- /dev/null +++ b/java/ql/test/query-tests/UselessTypeTest/options @@ -0,0 +1 @@ +//semmle-extractor-options: --javac-args --enable-preview -source 15 -target 15