
Co-authored-by: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com>
73 lines
1.6 KiB
Plaintext
73 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
=== Exceptions
|
|
|
|
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]
|
|
----
|
|
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
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
public int Example(int i)
|
|
{
|
|
return i + 42;
|
|
}
|
|
|
|
public IEnumerable<int> ExampleCollection(IEnumerable<int> coll)
|
|
{
|
|
return coll.Reverse();
|
|
}
|
|
----
|
|
|
|
[source,csharp]
|
|
----
|
|
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[]
|