llvm / llvm/llvm-project

[Enhancement] Diagnose constants with element type that is wider than necessary

Open
#224,071 0 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics enhancement
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

clang has no diagnostic for constants whose element type is wider than necessary to hold all of the initializer values.

For example:

```c++
// All values fit in uint8_t, but the table is 4x-8x larger than it needs to be.
static const unsigned long lookup_table[256] = {
1, 2, 3, 4, 5, ...
};
```

This pattern can show up in code where a table was copied and pasted from another codebase.

The diagnostic can be implemented as either a clang warning or a clang-tidy check. If it's going to be implemented as a clang warning, it should be off by default, as the right type for the constant can be constrained by other factors (e.g., an external API).

Contributor guide

Open the contributing guide

Research direction

Start by reviewing existing clang diagnostics and clang-tidy checks for constant arrays and initializer values; the issue names no specific files or tests. Decide whether this belongs as an opt-in clang warning or a clang-tidy check, then define coverage for arrays whose initializers fit a narrower element type and add tests showing the intended diagnostic behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.