C#: Add extractor support for the notnull general type parameter constraint.

This commit is contained in:
Michael Nebel
2025-01-02 14:49:13 +01:00
parent c0974f364e
commit 958d8f1f01
4 changed files with 12 additions and 0 deletions

View File

@@ -40,6 +40,9 @@ namespace Semmle.Extraction.CSharp.Entities
if (Symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated)
trapFile.general_type_parameter_constraints(this, 5);
if (Symbol.HasNotNullConstraint)
trapFile.general_type_parameter_constraints(this, 6);
foreach (var abase in Symbol.GetAnnotatedTypeConstraints())
{
var t = Type.Create(Context, abase.Symbol);

View File

@@ -287,6 +287,9 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {
/** Holds if these constraints include a nullable reference type constraint. */
predicate hasNullableRefTypeConstraint() { general_type_parameter_constraints(this, 5) }
/** Holds if these constraints include a not-null type constraint. */
predicate hasNotNullTypeConstraint() { general_type_parameter_constraints(this, 6) }
/** Gets a textual representation of these constraints. */
override string toString() { result = "where " + this.getTypeParameter().getName() + ": ..." }

View File

@@ -18,3 +18,5 @@ hasValueTypeConstraint
hasUnmanagedTypeConstraint
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
hasNullableRefTypeConstraint
hasNotNullConstraint
| TypeParameterConstraints.cs:14:20:14:21 | T5 | file://:0:0:0:0 | where T5: ... |

View File

@@ -29,3 +29,7 @@ query predicate hasUnmanagedTypeConstraint(TypeParameter tp, TypeParameterConstr
query predicate hasNullableRefTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasNullableRefTypeConstraint()
}
query predicate hasNotNullConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasNotNullTypeConstraint()
}