File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
csharp/ql/test/query-tests/Language Abuse/MissedReadonlyOpportunity Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -2,22 +2,26 @@ class MissedReadonlyOpportunity<T>
2
2
{
3
3
public int Bad1 ; // $ Alert
4
4
public T Bad2 ; // $ Alert
5
+ public Immutable Bad3 ; // $ Alert
5
6
public readonly int Good1 ;
6
7
public readonly int Good2 = 0 ;
7
8
public const int Good3 = 0 ;
8
9
public int Good4 ;
9
10
public readonly T Good5 ;
10
11
public T Good6 ;
12
+ public Mutable Good7 ;
11
13
12
14
public MissedReadonlyOpportunity ( int i , T t )
13
15
{
14
16
Bad1 = i ;
15
17
Bad2 = t ;
18
+ Bad3 = new Immutable ( ) ;
16
19
Good1 = i ;
17
20
Good2 = i ;
18
21
Good4 = i ;
19
22
Good5 = t ;
20
23
Good6 = t ;
24
+ Good7 = new Mutable ( ) ;
21
25
}
22
26
23
27
public void M ( int i )
@@ -27,3 +31,54 @@ public void M(int i)
27
31
x . Good6 = false ;
28
32
}
29
33
}
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
+ }
Original file line number Diff line number Diff line change
1
+ #select
1
2
| MissedReadonlyOpportunity.cs:3:16:3:19 | Bad1 | Field 'Bad1' can be 'readonly'. |
2
3
| 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'. |
3
10
| 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 |
You can’t perform that action at this time.
0 commit comments