rspec/rules/S1905/csharp/rule.adoc

73 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
include::../description.adoc[]
=== Exceptions
2020-06-30 12:47:33 +02:00
Issues are not raised against the https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions[default literal].
== How to fix it
To fix your code remove the unnecessary casting expression.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
public int Example(int i)
{
return (int) (i + 42); // Noncompliant
}
public IEnumerable<int> ExampleCollection(IEnumerable<int> coll)
{
return coll.Reverse().OfType<int>(); // Noncompliant
}
----
==== Compliant solution
2020-06-30 12:47:33 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
public int Example(int i)
{
return i + 42;
}
public IEnumerable<int> ExampleCollection(IEnumerable<int> coll)
{
return coll.Reverse();
}
----
[source,csharp]
2020-06-30 12:47:33 +02:00
----
bool b = (bool)default; // Doesn't raise an issue
----
== Resources
=== Documentation
* Microsoft - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions[Casting and type conversions]
* Wikipedia - https://en.wikipedia.org/wiki/Type_conversion[Type Conversion]
* Wikipedia - https://en.wikipedia.org/wiki/Strong_and_weak_typing[Strong and Weak Typing]
* Wikipedia - https://en.wikipedia.org/wiki/Polymorphism_(computer_science)[ Polymorphism (Computer Science)]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]