Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 997 Bytes

File metadata and controls

41 lines (31 loc) · 997 Bytes

constVariable

Message: Variable 'x' can be declared as const
Category: Code Quality
Severity: Style
Language: C/C++

Description

A local array variable is never used to modify its contents, so it could be declared as an array of const elements.

Motivation

A missing const hides a guarantee the compiler could otherwise enforce and readers could otherwise rely on: that this array, once initialized, is never written to again.

How to fix

Before:

int f() {
    static int i[1] = {}; // <- 'i' is only read
    return i[0];
}

After:

int f() {
    static const int i[1] = {};
    return i[0];
}

Related checkers