Skip to content

Commit d48ce85

Browse files
committed
C#: Implement nullable warning suppression expressions.
1 parent 7790ac4 commit d48ce85

File tree

8 files changed

+81
-1
lines changed

8 files changed

+81
-1
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Factory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ internal static Expression Create(ExpressionNodeInfo info)
242242
case SyntaxKind.SwitchExpression:
243243
return Switch.Create(info);
244244

245+
case SyntaxKind.SuppressNullableWarningExpression:
246+
return PostfixUnary.Create(info.SetKind(ExprKind.SUPPRESS_NULLABLE_WARNING), ((PostfixUnaryExpressionSyntax)info.Node).Operand);
247+
245248
default:
246249
info.Context.ModelError(info.Node, $"Unhandled expression '{info.Node}' of kind '{info.Node.Kind()}'");
247250
return new Unknown(info);

csharp/extractor/Semmle.Extraction.CSharp/Kinds/ExprKind.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public enum ExprKind
113113
PROPERTY_PATTERN = 116,
114114
POSITIONAL_PATTERN = 117,
115115
SWITCH_CASE = 118,
116-
ASSIGN_COALESCE = 119
116+
ASSIGN_COALESCE = 119,
117+
SUPPRESS_NULLABLE_WARNING = 120
117118
}
118119
}

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ module LocalFlow {
8383
scope = e2 and
8484
isSuccessor = true
8585
or
86+
e1 = e2.(SuppressNullableWarningExpr).getExpr() and
87+
scope = e2 and
88+
isSuccessor = true
89+
or
8690
e2 = any(ConditionalExpr ce |
8791
e1 = ce.getThen() or
8892
e1 = ce.getElse()

csharp/ql/src/semmle/code/csharp/exprs/Expr.qll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,3 +990,20 @@ class IndexExpr extends Expr, @index_expr {
990990

991991
override string toString() { result = "^..." }
992992
}
993+
994+
/**
995+
* A nullable warning suppression expression, for example `x!` in
996+
* ```
997+
* string GetName()
998+
* {
999+
* string? x = ...;
1000+
* return x!;
1001+
* }
1002+
* ```
1003+
*/
1004+
class SuppressNullableWarningExpr extends Expr, @suppress_nullable_warning_expr {
1005+
/** Gets the expression, for example `x` in `x!`. */
1006+
Expr getExpr() { result.getParent() = this }
1007+
1008+
override string toString() { result = "...!" }
1009+
}

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ case @expr.kind of
987987
| 117 = @positional_pattern_expr
988988
| 118 = @switch_case_expr
989989
| 119 = @assign_coalesce_expr
990+
| 120 = @suppress_nullable_warning_expr
990991
;
991992

992993
@switch = @switch_stmt | @switch_expr;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
class NullableRefTypes
4+
{
5+
void TestNullableRefTypes()
6+
{
7+
string? x = "source";
8+
string y = x!;
9+
y = x!!;
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
suppressNullableWarnings
2+
| NullableRefTypes.cs:8:20:8:21 | ...! | NullableRefTypes.cs:8:20:8:20 | access to local variable x |
3+
| NullableRefTypes.cs:9:13:9:14 | ...! | NullableRefTypes.cs:9:13:9:13 | access to local variable x |
4+
| NullableRefTypes.cs:9:13:9:15 | ...! | NullableRefTypes.cs:9:13:9:14 | ...! |
5+
nullableDataFlow
6+
| NullableRefTypes.cs:7:17:7:28 | SSA def(x) | NullableRefTypes.cs:8:20:8:20 | access to local variable x |
7+
| NullableRefTypes.cs:7:21:7:28 | "source" | NullableRefTypes.cs:7:17:7:28 | SSA def(x) |
8+
| NullableRefTypes.cs:8:20:8:20 | access to local variable x | NullableRefTypes.cs:8:20:8:21 | ...! |
9+
| NullableRefTypes.cs:8:20:8:20 | access to local variable x | NullableRefTypes.cs:9:13:9:13 | access to local variable x |
10+
| NullableRefTypes.cs:9:13:9:13 | access to local variable x | NullableRefTypes.cs:9:13:9:14 | ...! |
11+
| NullableRefTypes.cs:9:13:9:14 | ...! | NullableRefTypes.cs:9:13:9:15 | ...! |
12+
nullableControlFlow
13+
| NullableRefTypes.cs:5:10:5:29 | enter TestNullableRefTypes | NullableRefTypes.cs:6:5:10:5 | {...} | successor |
14+
| NullableRefTypes.cs:6:5:10:5 | {...} | NullableRefTypes.cs:7:9:7:29 | ... ...; | successor |
15+
| NullableRefTypes.cs:7:9:7:29 | ... ...; | NullableRefTypes.cs:7:21:7:28 | "source" | successor |
16+
| NullableRefTypes.cs:7:17:7:28 | String x = ... | NullableRefTypes.cs:8:9:8:22 | ... ...; | successor |
17+
| NullableRefTypes.cs:7:21:7:28 | "source" | NullableRefTypes.cs:7:17:7:28 | String x = ... | successor |
18+
| NullableRefTypes.cs:8:9:8:22 | ... ...; | NullableRefTypes.cs:8:20:8:20 | access to local variable x | successor |
19+
| NullableRefTypes.cs:8:16:8:21 | String y = ... | NullableRefTypes.cs:9:9:9:16 | ...; | successor |
20+
| NullableRefTypes.cs:8:20:8:20 | access to local variable x | NullableRefTypes.cs:8:20:8:21 | ...! | successor |
21+
| NullableRefTypes.cs:8:20:8:21 | ...! | NullableRefTypes.cs:8:16:8:21 | String y = ... | successor |
22+
| NullableRefTypes.cs:9:9:9:15 | ... = ... | NullableRefTypes.cs:5:10:5:29 | exit TestNullableRefTypes | successor |
23+
| NullableRefTypes.cs:9:9:9:16 | ...; | NullableRefTypes.cs:9:13:9:13 | access to local variable x | successor |
24+
| NullableRefTypes.cs:9:13:9:13 | access to local variable x | NullableRefTypes.cs:9:13:9:14 | ...! | successor |
25+
| NullableRefTypes.cs:9:13:9:14 | ...! | NullableRefTypes.cs:9:13:9:15 | ...! | successor |
26+
| NullableRefTypes.cs:9:13:9:15 | ...! | NullableRefTypes.cs:9:9:9:15 | ... = ... | successor |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import csharp
2+
3+
query predicate suppressNullableWarnings(SuppressNullableWarningExpr e, Expr op) {
4+
op = e.getExpr()
5+
}
6+
7+
query predicate nullableDataFlow(DataFlow::Node src, DataFlow::Node sink) {
8+
src.getEnclosingCallable().hasName("TestNullableRefTypes") and
9+
DataFlow::localFlowStep(src, sink)
10+
}
11+
12+
query predicate nullableControlFlow(
13+
ControlFlow::Node a, ControlFlow::Node b, ControlFlow::SuccessorType t
14+
) {
15+
a.getEnclosingCallable().hasName("TestNullableRefTypes") and
16+
b = a.getASuccessorByType(t)
17+
}

0 commit comments

Comments
 (0)