llvm / llvm/llvm-project

readability-trailing-comma false positive on enum with preprocessor #endif

Open
#218,957 2 comments 0 reactions 0 assignees View on GitHub
clang-tidy false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`clang-tidy --checks='-*,readability-trailing-comma' test.cpp -- -std=c++20`

What happens: the check reports "enum should have a trailing comma" and inserts , right after `#endif`. But both branches already end their last enumerator with a trailing comma — the fix inserts a stray comma outside the enumerator list, corrupting the source.

Reproducer:

```c++
enum class color_t : unsigned {
RED = 0,
GREEN = 1,
BLUE = 2,
CYAN = 3,
#ifdef USE_MAGENTA
LAST = CYAN,
#else
LAST = BLUE,
#endif
};
```

Contributor guide

Open the contributing guide

Research direction

Start by running the clang-tidy readability-trailing-comma check on the provided test.cpp reproducer with -std=c++20, then trace the check's handling of enum entries around the preprocessor branches. Done means the existing trailing commas produce no false diagnostic and no comma is inserted after #endif, with the reproducer covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.