Skip to content

Commit b76b090

Browse files
committed
C#: Generalize pattern definition to not only include top level pattern definitions.
1 parent d8d860f commit b76b090

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎csharp/ql/lib/semmle/code/csharp/Assignable.qll‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ module AssignableInternal {
318318
TLocalVariableDefinition(LocalVariableDeclExpr lvde) {
319319
not lvde.hasInitializer() and
320320
not exists(getTupleSource(TTupleAssignmentDefinition(_, lvde))) and
321-
not lvde instanceof TopLevelPatternDecl and
321+
not lvde instanceof LocalVariablePatternDecl and
322322
not lvde.isOutArgument()
323323
} or
324324
TImplicitParameterDefinition(Parameter p) {
@@ -337,7 +337,7 @@ module AssignableInternal {
337337
default = p.getDefaultValue()
338338
} or
339339
TAddressOfDefinition(AddressOfExpr aoe) or
340-
TPatternDefinition(TopLevelPatternDecl tlpd) or
340+
TPatternDefinition(LocalVariablePatternDecl lvpd) or
341341
TAssignOperationDefinition(AssignOperation ao) {
342342
ao instanceof AssignCallExpr and not ao instanceof CompoundAssignmentOperatorCall
343343
or
@@ -750,24 +750,31 @@ module AssignableDefinitions {
750750
}
751751

752752
/**
753-
* A local variable definition in a pattern, for example `x is int i`.
753+
* A local variable definition in a pattern, for example `int i` in `x is int i`.
754754
*/
755755
class PatternDefinition extends AssignableDefinition, TPatternDefinition {
756-
TopLevelPatternDecl tlpd;
756+
LocalVariablePatternDecl lvpd;
757757

758-
PatternDefinition() { this = TPatternDefinition(tlpd) }
758+
PatternDefinition() { this = TPatternDefinition(lvpd) }
759759

760760
/** Gets the element matches against this pattern. */
761-
PatternMatch getMatch() { result = tlpd.getMatch() }
761+
PatternMatch getMatch() { result = lvpd.getMatch() }
762762

763763
/** Gets the underlying local variable declaration. */
764-
LocalVariableDeclExpr getDeclaration() { result = tlpd }
765-
766-
override Expr getSource() { result = this.getMatch().getExpr() }
764+
LocalVariableDeclExpr getDeclaration() { result = lvpd }
767765

768766
override string toString() { result = this.getDeclaration().toString() }
769767
}
770768

769+
/**
770+
* A local variable definition at the top level of a pattern.
771+
*/
772+
class TopLevelPatternDefinition extends PatternDefinition {
773+
TopLevelPatternDefinition() { lvpd.isTopLevel() }
774+
775+
override Expr getSource() { result = this.getMatch().getExpr() }
776+
}
777+
771778
/**
772779
* An initializer definition for a field or a property, for example
773780
* line 2 in

‎csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private predicate nonNullDef(SsaExplicitWrite def) {
110110
def.getValue() instanceof NonNullExpr
111111
or
112112
exists(AssignableDefinition ad | ad = def.getDefinition() |
113-
ad instanceof AssignableDefinitions::PatternDefinition
113+
ad instanceof AssignableDefinitions::TopLevelPatternDefinition
114114
or
115115
ad =
116116
any(AssignableDefinitions::LocalVariableDefinition d |

‎csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ class CastNode extends Node {
26772677
this.asExpr() instanceof Cast
26782678
or
26792679
this.(AssignableDefinitionNode).getDefinition() instanceof
2680-
AssignableDefinitions::PatternDefinition
2680+
AssignableDefinitions::TopLevelPatternDefinition
26812681
}
26822682
}
26832683

‎csharp/ql/src/Dead Code/DeadStoreOfLocal.ql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class RelevantDefinition extends AssignableDefinition {
3535
not lvde.getName() = "_"
3636
)
3737
or
38-
this instanceof AssignableDefinitions::PatternDefinition
38+
this instanceof AssignableDefinitions::TopLevelPatternDefinition
3939
or
4040
this instanceof AssignableDefinitions::AssignOperationDefinition
4141
}

0 commit comments

Comments
 (0)