mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
C#: C#9 Add target typed conditional tests
This commit is contained in:
@@ -335,6 +335,58 @@ ParenthesizedPattern.cs:
|
||||
# 27| 0: [TypeAccessPatternExpr] access to type String
|
||||
# 27| 0: [TypeMention] string
|
||||
# 27| 2: [IntLiteral] 5
|
||||
TargetType.cs:
|
||||
# 5| [Class] TargetType
|
||||
# 7| 5: [Method] M2
|
||||
# 7| -1: [TypeMention] Void
|
||||
# 8| 4: [BlockStmt] {...}
|
||||
# 9| 0: [LocalVariableDeclStmt] ... ...;
|
||||
# 9| 0: [LocalVariableDeclAndInitExpr] Random rand = ...
|
||||
# 9| -1: [TypeMention] Random
|
||||
# 9| 0: [LocalVariableAccess] access to local variable rand
|
||||
# 9| 1: [ObjectCreation] object creation of type Random
|
||||
# 9| 0: [TypeMention] Random
|
||||
# 10| 1: [LocalVariableDeclStmt] ... ...;
|
||||
# 10| 0: [LocalVariableDeclAndInitExpr] Boolean condition = ...
|
||||
# 10| -1: [TypeMention] bool
|
||||
# 10| 0: [LocalVariableAccess] access to local variable condition
|
||||
# 10| 1: [GTExpr] ... > ...
|
||||
# 10| 0: [MethodCall] call to method NextDouble
|
||||
# 10| -1: [LocalVariableAccess] access to local variable rand
|
||||
# 10| 1: [DoubleLiteral] 0.5
|
||||
# 12| 2: [LocalVariableDeclStmt] ... ...;
|
||||
# 12| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> x = ...
|
||||
# 12| -1: [TypeMention] int?
|
||||
# 12| 1: [TypeMention] int
|
||||
# 12| 0: [LocalVariableAccess] access to local variable x
|
||||
# 12| 1: [ConditionalExpr] ... ? ... : ...
|
||||
# 12| 0: [LocalVariableAccess] access to local variable condition
|
||||
# 13| 1: [CastExpr] (...) ...
|
||||
# 13| 1: [IntLiteral] 12
|
||||
# 14| 2: [NullLiteral] null
|
||||
# 16| 3: [LocalVariableDeclStmt] ... ...;
|
||||
# 16| 0: [LocalVariableDeclAndInitExpr] IEnumerable<Int32> xs = ...
|
||||
# 16| -1: [TypeMention] IEnumerable<Int32>
|
||||
# 16| 1: [TypeMention] int
|
||||
# 16| 0: [LocalVariableAccess] access to local variable xs
|
||||
# 16| 1: [ConditionalExpr] ... ? ... : ...
|
||||
# 16| 0: [IsExpr] ... is ...
|
||||
# 16| 0: [LocalVariableAccess] access to local variable x
|
||||
# 16| 1: [ConstantPatternExpr,NullLiteral] null
|
||||
# 17| 1: [ObjectCreation] object creation of type List<Int32>
|
||||
# 17| -2: [TypeMention] List<Int32>
|
||||
# 17| 1: [TypeMention] int
|
||||
# 17| -1: [CollectionInitializer] { ..., ... }
|
||||
# 17| 0: [ElementInitializer] call to method Add
|
||||
# 17| 0: [IntLiteral] 0
|
||||
# 17| 1: [ElementInitializer] call to method Add
|
||||
# 17| 0: [IntLiteral] 1
|
||||
# 18| 2: [ArrayCreation] array creation of type Int32[]
|
||||
# 18| -2: [TypeMention] Int32[]
|
||||
# 18| 1: [TypeMention] int
|
||||
# 18| -1: [ArrayInitializer] { ..., ... }
|
||||
# 18| 0: [IntLiteral] 2
|
||||
# 18| 1: [IntLiteral] 3
|
||||
TypeParameterNullability.cs:
|
||||
# 1| [Interface] I1
|
||||
# 3| [Class] A2
|
||||
|
||||
20
csharp/ql/test/library-tests/csharp9/TargetType.cs
Normal file
20
csharp/ql/test/library-tests/csharp9/TargetType.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class TargetType
|
||||
{
|
||||
public void M2()
|
||||
{
|
||||
var rand = new Random();
|
||||
var condition = rand.NextDouble() > 0.5;
|
||||
|
||||
int? x = condition
|
||||
? 12
|
||||
: null;
|
||||
|
||||
IEnumerable<int> xs = x is null
|
||||
? new List<int>() { 0, 1 }
|
||||
: new int[] { 2, 3 };
|
||||
}
|
||||
}
|
||||
2
csharp/ql/test/library-tests/csharp9/TargetType.expected
Normal file
2
csharp/ql/test/library-tests/csharp9/TargetType.expected
Normal file
@@ -0,0 +1,2 @@
|
||||
| TargetType.cs:12:18:14:18 | ... ? ... : ... | int? | int? | null |
|
||||
| TargetType.cs:16:31:18:32 | ... ? ... : ... | IEnumerable<Int32> | List<Int32> | Int32[] |
|
||||
7
csharp/ql/test/library-tests/csharp9/TargetType.ql
Normal file
7
csharp/ql/test/library-tests/csharp9/TargetType.ql
Normal file
@@ -0,0 +1,7 @@
|
||||
import csharp
|
||||
|
||||
query predicate conditional(ConditionalExpr expr, string t, string t1, string t2) {
|
||||
expr.getType().toStringWithTypes() = t and
|
||||
expr.getThen().getType().toStringWithTypes() = t1 and
|
||||
expr.getElse().getType().toStringWithTypes() = t2
|
||||
}
|
||||
Reference in New Issue
Block a user