Avoid dead code

Why you should care

Dead code are instructions that can never be reached. Obvious dead code are instructions that follow unconditional jumps in the same statement block. Dead code could just be deactivated functionalities, but in this case, it should not be present in production.

How we detect

CAST Highlight counts one occurrence each time a statement is detected after a jump statement (i.e. return, break, continue, and fallthrough) in the same current block. Bad Code
func fun(a:Int)->Int{
var i = 10;
return i + a;
i++; // this is never executed
}
Good Code
func fun(a:Int)->Int{
  var i = 10;
  return i + a;
}

References

http://cwe.mitre.org/data/definitions/561.html https://rules.sonarsource.com/swift/RSPEC-1763

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_deadcode-avoid-dead-code/
Have more questions? Submit a request

Comments

Powered by Zendesk