Skip to content

Commit a1db011

Browse files
committed
C#: Add extractor support for the not null general type parameter constraint.
1 parent 603ce81 commit a1db011

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameterConstraints.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public override void Populate(TextWriter trapFile)
4040
if (Symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated)
4141
trapFile.general_type_parameter_constraints(this, 5);
4242

43+
if (Symbol.HasNotNullConstraint)
44+
trapFile.general_type_parameter_constraints(this, 6);
45+
4346
foreach (var abase in Symbol.GetAnnotatedTypeConstraints())
4447
{
4548
var t = Type.Create(Context, abase.Symbol);

csharp/ql/lib/semmle/code/csharp/Generics.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {
287287
/** Holds if these constraints include a nullable reference type constraint. */
288288
predicate hasNullableRefTypeConstraint() { general_type_parameter_constraints(this, 5) }
289289

290+
/** Holds if these constraints include a not-null type constraint. */
291+
predicate hasNotNullTypeConstraint() { general_type_parameter_constraints(this, 6) }
292+
290293
/** Gets a textual representation of these constraints. */
291294
override string toString() { result = "where " + this.getTypeParameter().getName() + ": ..." }
292295

csharp/ql/test/library-tests/typeparameterconstraints/typeParameterConstraints.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ hasValueTypeConstraint
1818
hasUnmanagedTypeConstraint
1919
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
2020
hasNullableRefTypeConstraint
21+
hasNotNullConstraint
22+
| TypeParameterConstraints.cs:14:20:14:21 | T5 | file://:0:0:0:0 | where T5: ... |

csharp/ql/test/library-tests/typeparameterconstraints/typeParameterConstraints.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ query predicate hasUnmanagedTypeConstraint(TypeParameter tp, TypeParameterConstr
2929
query predicate hasNullableRefTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
3030
typeParameterContraints(tp, tpc) and tpc.hasNullableRefTypeConstraint()
3131
}
32+
33+
query predicate hasNotNullConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
34+
typeParameterContraints(tp, tpc) and tpc.hasNotNullTypeConstraint()
35+
}

0 commit comments

Comments
 (0)