Force casts should not be used

Why you should care

Because force casting (as!) does not perform any type safety validations, it is capable of performing dangerous conversions between unrelated types. When the types are truly unrelated, the cast will cause a system crash.

How we detect

CAST Highlight counts one occurrence each time a "as!" pattern is detected in source code. Bad Code 
foo as! MyClass // Noncompliant
Good Code 
foo as? MyClass

References

CppCoreGuidelines, Type safety profile - Type.1: Don't use reinterpret_cast https://rules.sonarsource.com/swift/RSPEC-3630

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.
For reference only. For the complete details please refer the original article
https://doc.casthighlight.com/alt_dangerouscast-force-casts-not-used/
Have more questions? Submit a request

Comments

Powered by Zendesk