Hauptinhalt

Code deactivated by constant false condition (DEACTIVATED_CODE)

R2026b

Code segment deactivated by #if 0 preprocessor directive

Description

This defect occurs when a preprocessor conditional (#if or #elif) uses a compile-time constant expression that always evaluates to zero. The code within the disabled block is never compiled.

Risk

Code deactivated by #if 0 is never compiled. Risks include:

  • The compiler cannot test or verify the disabled code.

  • The disabled code may silently become stale as surrounding code evolves.

  • Maintenance confusion arises about whether the code is intentionally disabled or forgotten.

  • Latent defects may surface if the directive is later changed.

Fix

To fix this defect:

  • Remove the #if 0 block entirely if the code is no longer needed.

  • Replace #if 0 with a named macro (for example, #if FEATURE_ENABLED) so the intent is explicit and the deactivation is conditional rather than absolute.

  • If the code serves as a reference or placeholder, move it to a comment or version control history.

If you do not want to fix the issue, add comments to your result or code to avoid another review.

Examples

expand all

In this example, a block of code is wrapped in #if 0 / #endif. The code within this block is never compiled regardless of any runtime conditions.

#include <stdio.h>

void initialize_system(void) {
    printf("System starting\n");

#if 0 // Noncompliant
    printf("Debug: detailed trace enabled\n");
    run_diagnostics();
#endif

    printf("System ready\n");
}

Polyspace® Bug Finder™ reports a DEACTIVATED_CODE defect on the #if 0 directive. The constant expression always evaluates to zero, so the enclosed code is never compiled.

Correction — Use a Named Macro

Replace the literal #if 0 with a named feature macro. This makes the intent clear and allows conditional compilation.

#include <stdio.h>

#define ENABLE_DEBUG_TRACE 0

void initialize_system(void) {
    printf("System starting\n");

#if ENABLE_DEBUG_TRACE  // Compliant
    printf("Debug: detailed trace enabled\n");
    run_diagnostics();
#endif

    printf("System ready\n");
}

Result Information

Group: Data flow
Language: C | C++
Default: off
Command-Line Syntax: DEACTIVATED_CODE
Impact: Low
PQL Name: std.defects.DEACTIVATED_CODE

Version History

Introduced in R2013b

expand all