rspec/rules/S4586/csharp/rule.adoc
Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
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.
2023-06-22 10:38:01 +02:00

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[]