Skip to content

Commit 19119ea

Browse files
committed
C#: Add some lambda flow tests for demo
1 parent 1347076 commit 19119ea

File tree

3 files changed

+322
-0
lines changed

3 files changed

+322
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
using System;
2+
3+
public class LambdaFlow
4+
{
5+
/// <summary>
6+
/// Flow into a normal method
7+
/// </summary>
8+
class Ex1
9+
{
10+
void M1(string s)
11+
{
12+
Sink(s); // $ hasValueFlow=1
13+
}
14+
15+
public void M2()
16+
{
17+
var source = Source(1);
18+
M1(source);
19+
}
20+
}
21+
22+
23+
24+
25+
26+
27+
28+
/// <summary>
29+
/// Flow into a lambda
30+
/// </summary>
31+
class Ex2
32+
{
33+
void M1(Action<string> lambda)
34+
{
35+
var source = Source(2);
36+
lambda(source);
37+
}
38+
39+
void M2()
40+
{
41+
Action<string> lambda = x => Sink(x); // $ hasValueFlow=2
42+
M1(lambda);
43+
}
44+
}
45+
46+
47+
48+
49+
50+
51+
52+
/// <summary>
53+
/// Flow out of a lambda
54+
/// </summary>
55+
class Ex3
56+
{
57+
Func<string> M1()
58+
{
59+
return () => Source(3);
60+
}
61+
62+
void M2()
63+
{
64+
var lambda = M1();
65+
Sink(lambda()); // $ hasValueFlow=3
66+
}
67+
}
68+
69+
70+
71+
72+
73+
74+
75+
/// <summary>
76+
/// Flow through a lambda
77+
/// </summary>
78+
class Ex4
79+
{
80+
string M1(Func<string, string> lambda, string input)
81+
{
82+
return lambda(input);
83+
}
84+
85+
void M2()
86+
{
87+
Func<string, string> id = x => x;
88+
var source = Source(4);
89+
var output = M1(id, source);
90+
Sink(output); // $ hasValueFlow=4
91+
}
92+
}
93+
94+
95+
96+
97+
98+
99+
100+
/// <summary>
101+
/// No flow into lambda (call context sensitivity)
102+
/// </summary>
103+
class Ex5
104+
{
105+
void M1(Action<string> lambda, string input)
106+
{
107+
lambda(input);
108+
}
109+
110+
void M2(Action<string> lambda, string input)
111+
{
112+
M1(lambda, input);
113+
}
114+
115+
void M3()
116+
{
117+
Action<string> lambda1 = arg => Sink(arg);
118+
Action<string> lambda2 = arg => { };
119+
120+
var source = Source(5);
121+
var nonSource = "non-source";
122+
123+
M1(lambda1, nonSource);
124+
M1(lambda2, source);
125+
126+
M2(lambda1, nonSource);
127+
M2(lambda2, source);
128+
}
129+
}
130+
131+
132+
133+
134+
135+
136+
137+
/// <summary>
138+
/// Flow into a returned lambda
139+
/// </summary>
140+
class Ex6
141+
{
142+
Action<string> M1()
143+
{
144+
return x => Sink(x); // $ hasValueFlow=6
145+
}
146+
147+
void M2()
148+
{
149+
var source = Source(6);
150+
var lambda = M1();
151+
lambda(source);
152+
}
153+
}
154+
155+
156+
157+
158+
159+
160+
161+
/// <summary>
162+
/// No flow through lambda
163+
/// </summary>
164+
class Ex7
165+
{
166+
void M1(Func<string, string> lambda)
167+
{
168+
var source = Source(7);
169+
lambda(source);
170+
}
171+
172+
void M2(Func<string, string> lambda)
173+
{
174+
var nonSource = "non-source";
175+
var output = lambda(nonSource);
176+
Sink(output);
177+
}
178+
179+
void M3()
180+
{
181+
Func<string, string> id = x => x;
182+
M1(id);
183+
M2(id);
184+
}
185+
}
186+
187+
static string Source(int source) => source.ToString();
188+
189+
static void Sink(string value) { }
190+
}

0 commit comments

Comments
 (0)