Skip to content

Commit 74b8e73

Browse files
committed
C#: Use Gvn comparison instead of StructuralComparisonConfiguration in MissedTernaryOpportunity.
1 parent 94999d4 commit 74b8e73

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

csharp/ql/src/Language Abuse/MissedTernaryOpportunity.ql

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,12 @@
1212
import csharp
1313
import semmle.code.csharp.commons.StructuralComparison
1414

15-
class StructuralComparisonConfig extends StructuralComparisonConfiguration {
16-
StructuralComparisonConfig() { this = "MissedTernaryOpportunity" }
17-
18-
override predicate candidate(ControlFlowElement x, ControlFlowElement y) {
19-
exists(IfStmt is, AssignExpr ae1 |
20-
ae1 = is.getThen().stripSingletonBlocks().(ExprStmt).getExpr()
21-
|
22-
x = ae1.getLValue() and
23-
exists(AssignExpr ae2 | ae2 = is.getElse().stripSingletonBlocks().(ExprStmt).getExpr() |
24-
y = ae2.getLValue()
25-
)
26-
)
27-
}
15+
private Expr getAssignedExpr(Stmt stmt) {
16+
result = stmt.stripSingletonBlocks().(ExprStmt).getExpr().(AssignExpr).getLValue()
17+
}
2818

29-
IfStmt getIfStmt() {
30-
exists(AssignExpr ae | ae = result.getThen().stripSingletonBlocks().(ExprStmt).getExpr() |
31-
same(ae.getLValue(), _)
32-
)
33-
}
19+
private predicate ifStmt(IfStmt is) {
20+
sameGvn(getAssignedExpr(is.getThen()), getAssignedExpr(is.getElse()))
3421
}
3522

3623
from IfStmt is, string what
@@ -40,10 +27,8 @@ where
4027
is.getElse().stripSingletonBlocks() instanceof ReturnStmt and
4128
what = "return"
4229
or
43-
exists(StructuralComparisonConfig c |
44-
is = c.getIfStmt() and
45-
what = "write to the same variable"
46-
)
30+
ifStmt(is) and
31+
what = "write to the same variable"
4732
) and
4833
not exists(IfStmt other | is = other.getElse())
4934
select is,

0 commit comments

Comments
 (0)