C#: Use Gvn comparison instead of StructuralComparisonConfiguration in MissedTernaryOpportunity.

This commit is contained in:
Michael Nebel
2022-03-10 14:37:08 +01:00
parent 94999d4df5
commit 74b8e73133

View File

@@ -12,25 +12,12 @@
import csharp
import semmle.code.csharp.commons.StructuralComparison
class StructuralComparisonConfig extends StructuralComparisonConfiguration {
StructuralComparisonConfig() { this = "MissedTernaryOpportunity" }
private Expr getAssignedExpr(Stmt stmt) {
result = stmt.stripSingletonBlocks().(ExprStmt).getExpr().(AssignExpr).getLValue()
}
override predicate candidate(ControlFlowElement x, ControlFlowElement y) {
exists(IfStmt is, AssignExpr ae1 |
ae1 = is.getThen().stripSingletonBlocks().(ExprStmt).getExpr()
|
x = ae1.getLValue() and
exists(AssignExpr ae2 | ae2 = is.getElse().stripSingletonBlocks().(ExprStmt).getExpr() |
y = ae2.getLValue()
)
)
}
IfStmt getIfStmt() {
exists(AssignExpr ae | ae = result.getThen().stripSingletonBlocks().(ExprStmt).getExpr() |
same(ae.getLValue(), _)
)
}
private predicate ifStmt(IfStmt is) {
sameGvn(getAssignedExpr(is.getThen()), getAssignedExpr(is.getElse()))
}
from IfStmt is, string what
@@ -40,10 +27,8 @@ where
is.getElse().stripSingletonBlocks() instanceof ReturnStmt and
what = "return"
or
exists(StructuralComparisonConfig c |
is = c.getIfStmt() and
what = "write to the same variable"
)
ifStmt(is) and
what = "write to the same variable"
) and
not exists(IfStmt other | is = other.getElse())
select is,