Detect and Remove Dead Code

Dead code -also known as unused code- is code that is never executed at run-time and that can be safely removed. Dead code must be detected and removed because it adds confusion for developers at best. At worst dead code ends up being used again and introduces a bug because it hasen't been maintained for long.

CppDepend provides several rules in the category Dead Code.

  • Potentially Dead Types: This rule matches dead types. This rule lists not only types not used anywhere in code, but also types used only by types not used anywhere in code. At first glance you might think that a type used is wrongly matched but you then need to examine its users that are only dead types. By default this rule doesn't match public types because such type might not be used by the analyzed code, but still be used by client code, not analyzed. This default behavior can be easily changed in the rule source code.
  • Potentially Dead Methods: This rule matches dead methods. As for the previous rule this rule matches not only methods not called anywhere but also methods called only by methods unused in code. Also public methods are not matched for the same reason as above and this behavior can be easily changed.
  • Potentially Dead Fields: This rule matches dead fields, fields used nowhere in code except maybe at initialization time. Here also public fields are not matched for the same reason as above and this behavior can be easily changed.

Note that each of these rules contains some logic to avoid false positive, types not seen as used but still used through reflection by the infrastructure. For example concerning dead type those types are not matched:

  • Types tagged with one of the attribute CppDepend.Attributes.IsNotDeadCodeAttribute or JetBrains.Annotations.UsedImplicitlyAttribute.
  • Entity Framework ModelSnapshot classes that derive from Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot.
  • ASP.NET MVC Controller classes like type whose named is suffixed by Controller or that derive from Microsoft.AspNetCore.Mvc.Controller or Microsoft.AspNetCore.Mvc.ControllerBase or that are tagged with Microsoft.AspNetCore.Mvc.ControllerAttribute.

If you detect more false positive situations please let us know at support@cppdepend.com and we will improve the rule for the next version.

CppDepend offers a wide range of features. It is often described as a Swiss Army Knife for C and C++ developers.

Start Free Trial