Skip to content

Commit 28cd8a8

Browse files
committed
C#: Add more test examples for cs/missing-readonly-modifier.
1 parent 3a1cd3f commit 28cd8a8

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity/MissedReadonlyOpportunity.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@ class MissedReadonlyOpportunity<T>
22
{
33
public int Bad1; // $ Alert
44
public T Bad2; // $ Alert
5+
public Immutable Bad3; // $ Alert
56
public readonly int Good1;
67
public readonly int Good2 = 0;
78
public const int Good3 = 0;
89
public int Good4;
910
public readonly T Good5;
1011
public T Good6;
12+
public Mutable Good7;
1113

1214
public MissedReadonlyOpportunity(int i, T t)
1315
{
1416
Bad1 = i;
1517
Bad2 = t;
18+
Bad3 = new Immutable();
1619
Good1 = i;
1720
Good2 = i;
1821
Good4 = i;
1922
Good5 = t;
2023
Good6 = t;
24+
Good7 = new Mutable();
2125
}
2226

2327
public void M(int i)
@@ -27,3 +31,54 @@ public void M(int i)
2731
x.Good6 = false;
2832
}
2933
}
34+
35+
struct Mutable
36+
{
37+
private int x;
38+
public int Mutate()
39+
{
40+
x = x + 1;
41+
return x;
42+
}
43+
}
44+
45+
readonly struct Immutable { }
46+
47+
class Tree
48+
{
49+
private Tree? Parent;
50+
private Tree? Left; // $ Alert
51+
private readonly Tree? Right;
52+
53+
public Tree(Tree left, Tree right)
54+
{
55+
this.Left = left;
56+
this.Right = right;
57+
left.Parent = this;
58+
right.Parent = this;
59+
}
60+
61+
public Tree()
62+
{
63+
Left = null;
64+
Right = null;
65+
}
66+
}
67+
68+
class StaticFields
69+
{
70+
static int X; // $ Alert
71+
static int Y;
72+
73+
// Static constructor
74+
static StaticFields()
75+
{
76+
X = 0;
77+
}
78+
79+
// Instance constructor
80+
public StaticFields(int y)
81+
{
82+
Y = y;
83+
}
84+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
#select
12
| MissedReadonlyOpportunity.cs:3:16:3:19 | Bad1 | Field 'Bad1' can be 'readonly'. |
23
| MissedReadonlyOpportunity.cs:4:14:4:17 | Bad2 | Field 'Bad2' can be 'readonly'. |
4+
| MissedReadonlyOpportunity.cs:5:22:5:25 | Bad3 | Field 'Bad3' can be 'readonly'. |
5+
| MissedReadonlyOpportunity.cs:12:20:12:24 | Good7 | Field 'Good7' can be 'readonly'. |
6+
| MissedReadonlyOpportunity.cs:49:19:49:24 | Parent | Field 'Parent' can be 'readonly'. |
7+
| MissedReadonlyOpportunity.cs:50:19:50:22 | Left | Field 'Left' can be 'readonly'. |
8+
| MissedReadonlyOpportunity.cs:70:16:70:16 | X | Field 'X' can be 'readonly'. |
9+
| MissedReadonlyOpportunity.cs:71:16:71:16 | Y | Field 'Y' can be 'readonly'. |
310
| MissedReadonlyOpportunityBad.cs:3:9:3:13 | Field | Field 'Field' can be 'readonly'. |
11+
testFailures
12+
| MissedReadonlyOpportunity.cs:12:20:12:24 | Field 'Good7' can be 'readonly'. | Unexpected result: Alert |
13+
| MissedReadonlyOpportunity.cs:49:19:49:24 | Field 'Parent' can be 'readonly'. | Unexpected result: Alert |
14+
| MissedReadonlyOpportunity.cs:71:16:71:16 | Field 'Y' can be 'readonly'. | Unexpected result: Alert |

0 commit comments

Comments
 (0)