
When an include is not surrounded by empty lines, its content is inlined on the same line as the adjacent content. That can lead to broken tags and other display issues. This PR fixes all such includes and introduces a validation step that forbids introducing the same problem again.
77 lines
1.6 KiB
Plaintext
77 lines
1.6 KiB
Plaintext
:keyword_null: null
|
|
:keyword_async: async
|
|
:concept_method: method
|
|
:typeparameter_TResult: <TResult>
|
|
|
|
== Why is this an issue?
|
|
|
|
include::../why-dotnet.adoc[]
|
|
|
|
[source,csharp]
|
|
----
|
|
public Task DoFooAsync()
|
|
{
|
|
return null; // Noncompliant: Causes a NullReferenceException if awaited.
|
|
}
|
|
|
|
public async Task Main()
|
|
{
|
|
await DoFooAsync(); // NullReferenceException
|
|
}
|
|
----
|
|
|
|
== How to fix it
|
|
|
|
include::../how-to-fix-it-dotnet.adoc[]
|
|
|
|
=== Code examples
|
|
|
|
A `Task` returning {concept_method} can be fixed like so:
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public Task DoFooAsync()
|
|
{
|
|
return null; // Noncompliant: Causes a NullReferenceException if awaited.
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
public Task DoFooAsync()
|
|
{
|
|
return Task.CompletedTask; // Compliant: Method can be awaited.
|
|
}
|
|
----
|
|
|
|
A `Task{typeparameter_TResult}` returning {concept_method} can be fixed like so:
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
public Task<object> GetFooAsync()
|
|
{
|
|
return null; // Noncompliant: Causes a NullReferenceException if awaited.
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=2,diff-type=compliant]
|
|
----
|
|
public Task<object> GetFooAsync()
|
|
{
|
|
return Task.FromResult<object>(null); // Compliant: Method can be awaited.
|
|
}
|
|
----
|
|
|
|
include::../resources.adoc[]
|
|
|
|
* C# Language Design - https://github.com/dotnet/csharplang/issues/35[Proposal Champion "Null-conditional await"]
|
|
|
|
include::../rspecator.adoc[] |