Skip to content

Commit 33361c6

Browse files
committed
C#: Replace extension type parameter access with synthetic parameter access.
1 parent 8004e76 commit 33361c6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

csharp/ql/lib/semmle/code/csharp/exprs/Access.qll

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,42 @@ class ParameterAccess extends LocalScopeVariableAccess, @parameter_access_expr {
223223
override string getAPrimaryQlClass() { result = "ParameterAccess" }
224224
}
225225

226+
/**
227+
* An access to a synthetic parameter for an extension method, for example the
228+
* access to `s` on line 3 in
229+
*
230+
* ```csharp
231+
* static class MyExtensions {
232+
* extension(string s) {
233+
* public bool IsEmpty() { return s == string.Empty; }
234+
* }
235+
* }
236+
* ```
237+
*/
238+
class SyntheticParameterAccess extends ParameterAccess {
239+
private Parameter p;
240+
241+
SyntheticParameterAccess() {
242+
exists(ExtensionType et |
243+
p = et.getReceiverParameter() and
244+
expr_access(this, p)
245+
)
246+
}
247+
248+
override Parameter getTarget() {
249+
exists(ExtensionCallable c |
250+
this.getEnclosingCallable+() = c and
251+
result = c.getParameter(0)
252+
)
253+
}
254+
255+
override string toString() {
256+
result = "access to synthetic parameter " + this.getTarget().getName()
257+
}
258+
259+
override string getAPrimaryQlClass() { result = "SyntheticParameterAccess" }
260+
}
261+
226262
/**
227263
* An access to a parameter that reads the underlying value, for example
228264
* the access to `p` on line 2 in

0 commit comments

Comments
 (0)