Skip to content

Commit 734001b

Browse files
authored
Merge pull request github#12334 from michaelnebel/csharp/staticinitialisers
C#: Update query to handle static field writes from properties.
2 parents d5952a1 + 5174662 commit 734001b

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `cs/static-field-written-by-instance` is updated to handle properties.
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Static field written by instance method
3-
* @description Finds instance methods that write static fields.
3+
* @description Finds instance methods and properties that write to static fields.
44
* This is tricky to get right if multiple instances are being manipulated,
55
* and generally bad practice.
66
* @kind problem
@@ -14,12 +14,12 @@
1414

1515
import csharp
1616

17-
from FieldWrite fw, Field f, Callable m
17+
from FieldWrite fw, Field f, Callable c
1818
where
1919
fw.getTarget() = f and
2020
f.isStatic() and
21-
m = fw.getEnclosingCallable() and
22-
not m.(Member).isStatic() and
23-
f.getDeclaringType() = m.getDeclaringType() and
24-
m.fromSource()
25-
select fw.(VariableAccess), "Write to static field from instance method or constructor."
21+
c = fw.getEnclosingCallable() and
22+
not [c.(Member), c.(Accessor).getDeclaration()].isStatic() and
23+
f.getDeclaringType() = c.getDeclaringType() and
24+
c.fromSource()
25+
select fw.(VariableAccess), "Write to static field from instance method, property, or constructor."

csharp/ql/test/query-tests/Likely Bugs/StaticFieldWrittenByInstance/StaticFieldWrittenByInstance.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,21 @@ void InstanceTest()
2626
staticField = 0; // BAD
2727
instanceField = 0; // OK
2828
}
29+
30+
static object backingField;
31+
static object StaticProp
32+
{
33+
get
34+
{
35+
return backingField ?? (backingField = new object()); // OK
36+
}
37+
}
38+
39+
object Prop
40+
{
41+
get
42+
{
43+
return backingField ?? (backingField = new object()); // BAD
44+
}
45+
}
2946
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
| StaticFieldWrittenByInstance.cs:15:9:15:19 | access to field staticField | Write to static field from instance method or constructor. |
2-
| StaticFieldWrittenByInstance.cs:26:9:26:19 | access to field staticField | Write to static field from instance method or constructor. |
1+
| StaticFieldWrittenByInstance.cs:15:9:15:19 | access to field staticField | Write to static field from instance method, property, or constructor. |
2+
| StaticFieldWrittenByInstance.cs:26:9:26:19 | access to field staticField | Write to static field from instance method, property, or constructor. |
3+
| StaticFieldWrittenByInstance.cs:43:37:43:48 | access to field backingField | Write to static field from instance method, property, or constructor. |

0 commit comments

Comments
 (0)