llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open philipp-hackerman opened this issue 3 months ago • 2 comments

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:

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

philipp-hackerman avatar Aug 26 '26 15:08 philipp-hackerman

@llvm/issue-subscribers-clang-tidy

Author: philipp-hackerman

`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:

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

https://godbolt.org/z/j1G4GYEP7

vbvictor avatar Aug 26 '26 19:08 vbvictor