rspec/rules/S3501/csharp/rule.adoc
2022-02-04 16:28:24 +00:00

42 lines
711 B
Plaintext

The output of an ``++as++`` cast will be null if the input to the cast cannot safely be cast to the desired type. So it makes sense that after such a cast you would null-check the output. But it doesn't make sense to check the input.
== Noncompliant Code Example
[source,csharp]
----
void DoTheThing(Toy toy)
{
Ball ball = toy as Ball;
if (toy != null) // Noncompliant
{
//...
}
}
----
== Compliant Solution
[source,csharp]
----
void DoTheThing(Toy toy)
{
Ball ball = toy as Ball;
if (ball != null)
{
//...
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]