Skip to content

Commit b24b275

Browse files
authored
Merge pull request #7669 from erik-krogh/fieldUnusedInDisjunct
QL: field unused in disjunct
2 parents 77b2f07 + 7c5ac63 commit b24b275

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

ql/ql/src/queries/performance/VarUnusedInDisjunct.ql

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ import ql
1717
*/
1818
pragma[noinline]
1919
predicate alwaysBindsVar(VarDef var, AstNode node) {
20-
// base case
21-
node.(VarAccess).getDeclaration() = var and
20+
(
21+
// base case
22+
node.(VarAccess).getDeclaration() = var
23+
or
24+
exists(Class clz |
25+
node.(FieldAccess).getDeclaration().getVarDecl() = var and
26+
node.(FieldAccess).getDeclaration() = clz.getAField() and // <- ensuring that the field is not inherited from a super class
27+
node.getEnclosingPredicate() = clz.getCharPred() // <- in non-charpred, the fields are implicitly bound by their relation to `this`.
28+
)
29+
) and
2230
not isSmallType(var.getType()) // <- early pruning
2331
or
2432
// recursive cases
@@ -205,8 +213,9 @@ predicate badDisjunction(EffectiveDisjunction disj, VarDef var) {
205213
not isTinyAssignment(disj.getAnOperand())
206214
}
207215

208-
from EffectiveDisjunction disj, VarDef var
216+
from EffectiveDisjunction disj, VarDef var, string type
209217
where
210218
badDisjunction(disj, var) and
211-
not badDisjunction(disj.getParent(), var) // avoid duplicate reporting of the same error
212-
select disj, "The variable " + var.getName() + " is only used in one side of disjunct."
219+
not badDisjunction(disj.getParent(), var) and // avoid duplicate reporting of the same error
220+
if var.getParent() instanceof FieldDecl then type = "field" else type = "variable"
221+
select disj, "The $@ is only used in one side of disjunct.", var, type + " " + var.getName()

ql/ql/test/queries/performance/VarUnusedInDisjunct/Test.qll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,12 @@ predicate good7() {
157157
)
158158
}
159159

160-
// TOOD: Next test, this one is
161-
string good8(int bitSize) {
162-
if bitSize != 0
163-
then bitSize = 1 and result = bitSize.toString()
164-
else (
165-
if 1 = 0 then result = "foo" else result = "bar"
166-
)
160+
class HasField extends Big {
161+
Big field;
162+
163+
HasField() {
164+
field = this
165+
or
166+
this.toString().matches("%foo") // <- field only defined here.
167+
}
167168
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
| Test.qll:14:3:16:7 | Disjunction | The variable b is only used in one side of disjunct. |
2-
| Test.qll:21:5:23:37 | Disjunction | The variable big is only used in one side of disjunct. |
3-
| Test.qll:28:3:30:33 | Disjunction | The variable t is only used in one side of disjunct. |
4-
| Test.qll:49:3:53:26 | Disjunction | The variable toType is only used in one side of disjunct. |
5-
| Test.qll:74:8:74:77 | Disjunction | The variable bad is only used in one side of disjunct. |
6-
| Test.qll:115:26:115:80 | IfFormula | The variable bb is only used in one side of disjunct. |
7-
| Test.qll:127:5:129:9 | Disjunction | The variable a is only used in one side of disjunct. |
8-
| Test.qll:132:5:134:9 | Disjunction | The variable a is only used in one side of disjunct. |
1+
| Test.qll:14:3:16:7 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:13:16:13:20 | b | variable b |
2+
| Test.qll:21:5:23:37 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:20:10:20:16 | big | variable big |
3+
| Test.qll:28:3:30:33 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:27:12:27:16 | t | variable t |
4+
| Test.qll:49:3:53:26 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:48:30:48:39 | toType | variable toType |
5+
| Test.qll:74:8:74:77 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:70:9:70:15 | bad | variable bad |
6+
| Test.qll:115:26:115:80 | IfFormula | The $@ is only used in one side of disjunct. | Test.qll:115:16:115:21 | bb | variable bb |
7+
| Test.qll:127:5:129:9 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:125:16:125:20 | a | variable a |
8+
| Test.qll:132:5:134:9 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:125:16:125:20 | a | variable a |
9+
| Test.qll:164:5:166:35 | Disjunction | The $@ is only used in one side of disjunct. | Test.qll:161:3:161:11 | field | field field |

0 commit comments

Comments
 (0)