You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add the list of attributes that support nullable analysis. (#1191)
* Include attribute related text from #700
Include all the nullable analysis attributes in the spec. This will be edited heavily and shrunk down to a smaller set of normative text.
* update the section links
* First set of text updates
* fix first test run
* Update samples and descriptions
* fix final build warning
* Apply suggestions from code review
Co-authored-by: Nigel-Ecma <[email protected]>
Co-authored-by: Jon Skeet <[email protected]>
* Apply suggestions from code review
Co-authored-by: Jon Skeet <[email protected]>
* respond to feedback.
* Respond to feedback.
* fix tests
* Update attributes.md
* Apply suggestions from code review
Co-authored-by: Nigel-Ecma <[email protected]>
* Don't imply unreachable code
When the `DoesNotReturn` attribute is parsed by a compiler that provides nullable diagnostics, that attribute can't impact reachable code analysis.
* Apply suggestions from code review
Co-authored-by: Neal Gafter <[email protected]>
Co-authored-by: Joseph Musser <[email protected]>
---------
Co-authored-by: Nigel-Ecma <[email protected]>
Co-authored-by: Jon Skeet <[email protected]>
Co-authored-by: Neal Gafter <[email protected]>
Co-authored-by: Joseph Musser <[email protected]>
- `System.Runtime.CompilerServices.CallerLineNumberAttribute` ([§22.5.6.2](attributes.md#22562-the-callerlinenumber-attribute)), `System.Runtime.CompilerServices.CallerFilePathAttribute` ([§22.5.6.3](attributes.md#22563-the-callerfilepath-attribute)), and `System.Runtime.CompilerServices.CallerMemberNameAttribute` ([§22.5.6.4](attributes.md#22564-the-callermembername-attribute)), whichareusedtosupplyinformationaboutthecallingcontexttooptionalparameters.
529
529
530
+
TheNullablestaticanalysisattributes (§Nullable-Analysis-Attributes) canimprovethecorrectnessofwarningsgeneratedfor nullabilities and null states (§8.9.5).
@@ -858,6 +860,277 @@ For invocations that occur within field or event initializers, the member name u
858
860
859
861
For invocations that occur within declarations of instance constructors, static constructors, finalizers and operators the member name used is implementation-dependent.
#### §Nullable-Analysis-Attributes-General General
866
+
867
+
The attributes in this section are used to provide additional information to support a compiler that provides nullability and null-state diagnostics (§8.9.5). A compiler isn't required to perform any null-state diagnostics. The presence or absence of these attributes do not affect the language nor the behavior of a program. A compiler that doesn't provide null-state diagnostics shall read and ignore the presence of these attributes. A compiler that provides null-state diagnostics shall use the meaning defined in this section for any of these attributes which it uses to inform its diagnostics.
868
+
869
+
The code-analysis attributes are declared in namespace `System.Diagnostics.CodeAnalysis`.
870
+
871
+
**Attribute** | **Meaning**
872
+
------------------ | ------------------
873
+
`AllowNull` (§The-AllowNull-Attribute) | A non-nullable argument may be null.
874
+
`DisallowNull` (§The-DisallowNull-Attribute) | A nullable argument should never be null.
875
+
`MaybeNull` (§The-MaybeNull-Attribute) | A non-nullable return value may be null.
876
+
`NotNull` (§The-NotNull-Attribute) | A nullable return value will never be null.
877
+
`MaybeNullWhen` (§The-MaybeNullWhen-Attribute) | A non-nullable argument may be null when the method returns the specified `bool` value.
878
+
`NotNullWhen` (§The-NotNullWhen-Attribute) | A nullable argument won't be null when the method returns the specified `bool` value.
879
+
`NotNullIfNotNull` (§The-NotNullIfNotNull-Attribute) | A return value isn't null if the argument for the specified parameter isn't null.
880
+
`MemberNotNull` (§The-MemberNotNull-Attribute) | The listed member won't be null when the method returns.
881
+
`MemberNotNullWhen` (§The-MemberNotNullWhen-Attribute) | The listed member won't be null when the method returns the specified `bool` value.
882
+
`DoesNotReturn` (§The-DoesNotReturn-Attribute) | This method never returns.
883
+
`DoesNotReturnIf` (§The-DoesNotReturnIf-Attribute) | This method never returns if the associated `bool` parameter has the specified value.
884
+
885
+
The following sections in §Nullable-Analysis-Attributes-General are conditionally normative.
886
+
887
+
#### §The-AllowNull-Attribute The AllowNull attribute
888
+
889
+
Specifies that a null value is allowed as an input even if the corresponding type disallows it.
890
+
891
+
> *Example*: Consider the following read/write property that never returns `null` because it has a reasonable default value. However, a user can give null to the set accessor to set the property to that default value.
> set => _comment =value??thrownewArgumentNullException(nameof(value),
934
+
>"Cannot set to null");
935
+
> }
936
+
>privatestring?_comment=default;
937
+
> }
938
+
> ```
939
+
>
940
+
>Thegetaccessorcouldreturnthedefaultvalueof `null`, sothecompilermaywarnthatitmustbecheckedbeforeaccess. Furthermore, itwarnscallersthat, eventhoughitcouldbenull, callersshouldn't explicitly set it to null. *end example*
941
+
942
+
#### §The-DoesNotReturn-Attribute The DoesNotReturn attribute
>Thepresenceoftheattributehelpsthecompilerinanumberofways. First, thecompilercanissueawarningifthere's a path where the method can exit without throwing an exception. Second, the compiler can suppress nullable warnings in any code after a call to that method, until an appropriate catch clause is found. Third, the unreachable code won'taffectanynullstates.
>Whennullreferencetypesareenabled, method `ThrowWhenNull` compileswithoutwarnings. Whenthatmethodreturns, the `value` argumentisguaranteedtobenot `null`. However, it's acceptable to call `ThrowWhenNull` with a null reference. *end example*
1096
+
1097
+
#### §The-NotNullIfNotNull-Attribute The NotNullIfNotNull attribute
1098
+
1099
+
Specifiesthatareturnvalueisn't `null` if the argument for the specified parameter isn't `null`.
1100
+
1101
+
>*Example*:Thenullstateofareturnvaluecoulddependonthenullstateofoneormorearguments. Toassistthecompiler’sanalysiswhenamethodalwaysreturnsanon-nullvaluewhencertainargumentsarenot `null` the `NotNullIfNotNull` attributemaybeused. Considerthefollowingmethod:
>Ifthe `url` argumentisn't `null`, `null` isn’t returned. When nullable references are enabled, that signature works correctly, provided the API never accepts a null argument. However, if the argument could be null, then the return value could also be null. To express that contract correctly, annotate this method as follows:
#### §The-NotNullWhen-Attribute The NotNullWhen attribute
1121
+
1122
+
Specifiesthatanullableargumentwon't be `null` when the method returns the specified `bool` value.
1123
+
1124
+
>*Example*:Thelibrarymethod `String.IsNullOrEmpty(String)` returns `true` whentheargumentis `null` oranemptystring. It'saformofnull-check:Callersdon't need to null-check the argument if the method returns `false`. To make a method like this nullable aware, make the parameter type a nullable reference type, and add the NotNullWhen attribute:
Copy file name to clipboardExpand all lines: tools/ExampleExtractor/ExtractorAndTesterUsersGuide.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -108,7 +108,7 @@ template_name
108
108
;
109
109
```
110
110
111
-
The unsuffixed and suffixed versions are identical, *except* that the unsuffixed ones have using directioves for all namespaces used by examples, while the suffixed ones do not. The unsuffixed versions are used by those few examples that begin with `#undef` or `#define`, which *must* precede using directives, and which might then have explicit using directives.
111
+
The unsuffixed and suffixed versions are identical, *except* that the unsuffixed ones have using directives for all namespaces used by examples, while the suffixed ones do not. The unsuffixed versions are used by those few examples that begin with `#undef` or `#define`, which *must* precede using directives, and which might then have explicit using directives.
0 commit comments