rspec/rules/S3610/csharp/rule.adoc

35 lines
989 B
Plaintext

== Why is this an issue?
Calling ``++GetType()++`` on a nullable object returns the underlying value type. Thus, comparing the returned ``++Type++`` object to ``++typeof(Nullable<SomeType>)++`` doesn't make sense. The comparison either throws an exception or the result can be known at compile time.
=== Noncompliant code example
[source,csharp]
----
int? nullable = 42;
bool comparison = nullable.GetType() == typeof(Nullable<int>); // Noncompliant, always false
comparison = nullable.GetType() != typeof(Nullable<int>); // Noncompliant, always true
nullable = null;
comparison = nullable.GetType() != typeof(Nullable<int>); // Noncompliant, calling GetType on a null always throws an exception
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]