From d29338817220c19536bed5367faef01764cb79e5 Mon Sep 17 00:00:00 2001 From: Max Schaefer Date: Fri, 24 Jan 2020 16:20:08 +0000 Subject: [PATCH 1/2] Add failing test case for RedundantExpr. --- ql/test/query-tests/RedundantCode/RedundantExpr/tst.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ql/test/query-tests/RedundantCode/RedundantExpr/tst.go b/ql/test/query-tests/RedundantCode/RedundantExpr/tst.go index 1b2f59c922c..e4106fb7bfa 100644 --- a/ql/test/query-tests/RedundantCode/RedundantExpr/tst.go +++ b/ql/test/query-tests/RedundantCode/RedundantExpr/tst.go @@ -24,5 +24,7 @@ func baz(b bool) int { } func main() { - foo(42) + if c == '\xA8' || c == '\xA9' { + foo(42) + } } From 3c1a68ee8f45dbb7361a2abe3808835d8a3b38ec Mon Sep 17 00:00:00 2001 From: Max Schaefer Date: Mon, 27 Jan 2020 12:05:48 +0000 Subject: [PATCH 2/2] Fix hash-consing of literals. We shouldn't rely on the literal value given in the `literals` table, but use the exact value (where available) instead. --- ql/src/RedundantCode/Clones.qll | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ql/src/RedundantCode/Clones.qll b/ql/src/RedundantCode/Clones.qll index 48650674706..beccbb9d1ec 100644 --- a/ql/src/RedundantCode/Clones.qll +++ b/ql/src/RedundantCode/Clones.qll @@ -50,10 +50,16 @@ class HashableNode extends AstNode { * Gets the value of this AST node, or the empty string if it does not have one. */ string getValue() { - literals(this, result, _) - or - not literals(this, _, _) and - result = "" + // for literals, get the exact value if available + if exists(this.(BasicLit).getExactValue()) + then result = this.(BasicLit).getExactValue() + else + // for identifiers, get the name + if this instanceof Ident + then result = this.(Ident).getName() + else + // for everything else, give up + result = "" } /**