Skip to content

M0-1-9: constexpr used in array size is detected as dead code #678

Closed
@fjatWbyT

Description

@fjatWbyT

Affected rules

  • M0-1-9 (dead code)

Description

Integer constant expression used for the size in an array declaration produces dead-code false positive. In other words, the e.g. constexpr int is not dead code because it is used to define the array size. This is contrast with when the size is (static) const, which does not produce the alert.

Example

int main() {
    constexpr int constexpr_unused = 1;   // True Positives, these first three are indeed unused / dead code
    static const int static_const_unused = 2;
    int unused_variable = 3;                            

    constexpr int constexpr_size       = 7;  // dead code detection  <-- False Positive, it is used in array a

    static const int static_const_size = 8;  // True Negative, 
                                             // (static) const doesn't trigger dead code

    int a[constexpr_size]    = {};  // The remaining have no dead code issue either
    int c[static_const_size] = {};  // they are used at the end

    return a[0] + c[2];
}

Metadata

Metadata

Assignees

Labels

Difficulty-MediumA false positive or false negative report which is expected to take 1-5 days effort to addressImpact-Mediumfalse positive/false negativeAn issue related to observed false positives or false negatives.user-reportIssue reported by an end user of CodeQL Coding Standards

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions