1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ // ReSharper disable ConvertToAutoProperty
4
5
namespace System . Diagnostics . CodeAnalysis
5
6
{
6
7
/// <summary>
@@ -40,33 +41,37 @@ public sealed class NotNullAttribute : Attribute
40
41
[ AttributeUsage ( AttributeTargets . Parameter , Inherited = false ) ]
41
42
public sealed class MaybeNullWhenAttribute : Attribute
42
43
{
44
+ private readonly bool _returnValue ;
45
+
43
46
/// <summary>
44
47
/// Initializes the attribute with the specified return value condition.
45
48
/// </summary>
46
49
/// <param name="returnValue">
47
50
/// The return value condition. If the method returns this value, the associated parameter may be null.
48
51
/// </param>
49
- public MaybeNullWhenAttribute ( bool returnValue ) => ReturnValue = returnValue ;
52
+ public MaybeNullWhenAttribute ( bool returnValue ) => _returnValue = returnValue ;
50
53
51
54
/// <summary>
52
55
/// Gets the return value condition.
53
56
/// </summary>
54
- public bool ReturnValue { get ; }
57
+ public bool ReturnValue => _returnValue ;
55
58
}
56
59
57
60
/// <summary>
58
61
/// Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.
59
62
/// </summary>
60
63
public sealed class NotNullWhenAttribute : Attribute
61
64
{
65
+ private readonly bool _returnValue ;
66
+
62
67
/// <summary>Initializes the attribute with the specified return value condition.</summary>
63
68
/// <param name="returnValue">
64
69
/// The return value condition. If the method returns this value, the associated parameter will not be null.
65
70
/// </param>
66
- public NotNullWhenAttribute ( bool returnValue ) => ReturnValue = returnValue ;
71
+ public NotNullWhenAttribute ( bool returnValue ) => _returnValue = returnValue ;
67
72
68
73
/// <summary>Gets the return value condition.</summary>
69
- public bool ReturnValue { get ; }
74
+ public bool ReturnValue => _returnValue ;
70
75
}
71
76
72
77
/// <summary>
@@ -75,14 +80,16 @@ public sealed class NotNullWhenAttribute : Attribute
75
80
[ AttributeUsage ( AttributeTargets . Parameter | AttributeTargets . Property | AttributeTargets . ReturnValue , AllowMultiple = true , Inherited = false ) ]
76
81
public sealed class NotNullIfNotNullAttribute : Attribute
77
82
{
83
+ private readonly string _parameterName ;
84
+
78
85
/// <summary>Initializes the attribute with the associated parameter name.</summary>
79
86
/// <param name="parameterName">
80
87
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
81
88
/// </param>
82
- public NotNullIfNotNullAttribute ( string parameterName ) => ParameterName = parameterName ;
89
+ public NotNullIfNotNullAttribute ( string parameterName ) => _parameterName = parameterName ;
83
90
84
91
/// <summary>Gets the associated parameter name.</summary>
85
- public string ParameterName { get ; }
92
+ public string ParameterName => _parameterName ;
86
93
}
87
94
88
95
/// <summary>
@@ -98,17 +105,19 @@ public sealed class DoesNotReturnAttribute : Attribute
98
105
[ AttributeUsage ( AttributeTargets . Parameter , Inherited = false ) ]
99
106
public sealed class DoesNotReturnIfAttribute : Attribute
100
107
{
108
+ private readonly bool _parameterValue ;
109
+
101
110
/// <summary>Initializes the attribute with the specified parameter value.</summary>
102
111
/// <param name="parameterValue">
103
112
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
104
113
/// the associated parameter matches this value.
105
114
/// </param>
106
- public DoesNotReturnIfAttribute ( bool parameterValue ) => ParameterValue = parameterValue ;
115
+ public DoesNotReturnIfAttribute ( bool parameterValue ) => _parameterValue = parameterValue ;
107
116
108
117
/// <summary>
109
118
/// Gets the condition parameter value.
110
119
/// </summary>
111
- public bool ParameterValue { get ; }
120
+ public bool ParameterValue => _parameterValue ;
112
121
}
113
122
114
123
/// <summary>
@@ -117,22 +126,24 @@ public sealed class DoesNotReturnIfAttribute : Attribute
117
126
[ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Property , Inherited = false , AllowMultiple = true ) ]
118
127
public sealed class MemberNotNullAttribute : Attribute
119
128
{
129
+ private readonly string [ ] _members ;
130
+
120
131
/// <summary>Initializes the attribute with a field or property member.</summary>
121
132
/// <param name="member">
122
133
/// The field or property member that is promised to be not-null.
123
134
/// </param>
124
- public MemberNotNullAttribute ( string member ) => Members = new [ ] { member } ;
135
+ public MemberNotNullAttribute ( string member ) => _members = new [ ] { member } ;
125
136
126
137
/// <summary>Initializes the attribute with the list of field and property members.</summary>
127
138
/// <param name="members">
128
139
/// The list of field and property members that are promised to be not-null.
129
140
/// </param>
130
- public MemberNotNullAttribute ( params string [ ] members ) => Members = members ;
141
+ public MemberNotNullAttribute ( params string [ ] members ) => _members = members ;
131
142
132
143
/// <summary>
133
144
/// Gets field or property member names.
134
145
/// </summary>
135
- public string [ ] Members { get ; }
146
+ public string [ ] Members => _members ;
136
147
}
137
148
138
149
/// <summary>
@@ -141,6 +152,9 @@ public sealed class MemberNotNullAttribute : Attribute
141
152
[ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Property , Inherited = false , AllowMultiple = true ) ]
142
153
public sealed class MemberNotNullWhenAttribute : Attribute
143
154
{
155
+ private readonly bool _returnValue ;
156
+ private readonly string [ ] _members ;
157
+
144
158
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
145
159
/// <param name="returnValue">
146
160
/// The return value condition. If the method returns this value, the associated parameter will not be null.
@@ -150,8 +164,8 @@ public sealed class MemberNotNullWhenAttribute : Attribute
150
164
/// </param>
151
165
public MemberNotNullWhenAttribute ( bool returnValue , string member )
152
166
{
153
- ReturnValue = returnValue ;
154
- Members = new [ ] { member } ;
167
+ _returnValue = returnValue ;
168
+ _members = new [ ] { member } ;
155
169
}
156
170
157
171
/// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
@@ -163,18 +177,18 @@ public MemberNotNullWhenAttribute(bool returnValue, string member)
163
177
/// </param>
164
178
public MemberNotNullWhenAttribute ( bool returnValue , params string [ ] members )
165
179
{
166
- ReturnValue = returnValue ;
167
- Members = members ;
180
+ _returnValue = returnValue ;
181
+ _members = members ;
168
182
}
169
183
170
184
/// <summary>
171
185
/// Gets the return value condition.
172
186
/// </summary>
173
- public bool ReturnValue { get ; }
187
+ public bool ReturnValue => _returnValue ;
174
188
175
189
/// <summary>
176
190
/// Gets field or property member names.
177
191
/// </summary>
178
- public string [ ] Members { get ; }
192
+ public string [ ] Members => _members ;
179
193
}
180
194
}
0 commit comments