File tree Expand file tree Collapse file tree
csharp/ql/src/Language Abuse Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1212import csharp
1313import 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
3623from IfStmt is , string what
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 ( ) )
4934select is ,
You can’t perform that action at this time.
0 commit comments