[clang-tools-extra] [clang-tidy] Fix readability-trailing-comma after multi-line comments (PR #241556)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 14:52:57 PST 2026
https://github.com/ljkx updated https://github.com/llvm/llvm-project/pull/241556
>From 9ca138961f403d4cf89da807f563ede437cc7e03 Mon Sep 17 00:00:00 2001
From: ljkx <177671075+ljkx at users.noreply.github.com>
Date: Wed, 11 Nov 2026 22:35:34 +0100
Subject: [PATCH 1/2] [clang-tidy] Fix readability-trailing-comma after
multi-line comments
The check found the last token of an enum by searching backward from the
closing brace with Lexer::findPreviousToken. That restarts lexing at the
beginning of the line, so when the closing brace follows a block comment
spanning multiple lines, lexing starts inside the comment and the `/` of
`*/` was taken as the last token. This produced a false positive and a
fix-it inserting a comma after the comment, even when a trailing comma
was already present.
Find the last token by lexing forward from the opening brace instead.
Fixes #237305.
Assisted-by: Claude Code (claude-opus-5-5)
---
.../readability/TrailingCommaCheck.cpp | 24 ++++++++++++++++---
clang-tools-extra/docs/ReleaseNotes.md | 3 +++
.../Inputs/trailing-comma/split-enum.h | 2 ++
.../readability/trailing-comma-remove.cpp | 10 ++++++++
.../readability/trailing-comma-split-enum.cpp | 8 +++++++
.../checkers/readability/trailing-comma.cpp | 15 ++++++++++++
6 files changed, 59 insertions(+), 3 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
index c0ed9849304de9..4a2741bc2a602c 100644
--- a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
@@ -122,9 +122,27 @@ void TrailingCommaCheck::checkEnumDecl(const EnumDecl *Enum,
if (Policy == CommaPolicyKind::Ignore)
return;
- const std::optional<Token> LastTok = Lexer::findPreviousToken(
- Enum->getBraceRange().getEnd(), *Result.SourceManager, getLangOpts(),
- /*IncludeComments=*/false);
+ // Lex forward from the opening brace rather than backward from the closing
+ // one: lexing backward restarts at the beginning of a line, which yields
+ // bogus tokens when that line is inside a multi-line block comment.
+ const SourceLocation RBraceLoc = Enum->getBraceRange().getEnd();
+ std::optional<Token> LastTok;
+ SourceLocation Loc = Enum->getBraceRange().getBegin();
+ while (true) {
+ const std::optional<Token> Tok =
+ Lexer::findNextToken(Loc, *Result.SourceManager, getLangOpts());
+ // The braces can be in different files, e.g. when the enum's body is
+ // split across an #include.
+ if (!Tok || Tok->is(tok::eof))
+ return;
+ if (Tok->getLocation() == RBraceLoc)
+ break;
+ if (!Result.SourceManager->isBeforeInTranslationUnit(Tok->getLocation(),
+ RBraceLoc))
+ return;
+ LastTok = Tok;
+ Loc = Tok->getLocation();
+ }
if (!LastTok)
return;
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index b6774370511feb..e7a55c3cce6118 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -473,6 +473,9 @@ infrastructure are described first, followed by tool-specific sections.
- Fixed a false positive on empty brace initializers of types with default
member initializers.
+ - Fixed a false positive and an invalid fix-it when an enum's last
+ enumerator is followed by a block comment that spans multiple lines.
+
- Improved {doc}`readability-use-std-min-max
<clang-tidy/checks/readability/use-std-min-max>` check by fixing spurious
trailing semicolons and lost comments when the `if` body has no braces.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h
new file mode 100644
index 00000000000000..f7e0a32f07fa65
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h
@@ -0,0 +1,2 @@
+enum SplitEnum {
+ SplitA,
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp
index fadce1808876f7..27461572b1e47b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp
@@ -33,6 +33,16 @@ enum E3 {
};
enum Empty {};
+enum MultiLineComment {
+ MLC_A, /* comment that
+ spans multiple lines */
+};
+// CHECK-MESSAGES: :[[@LINE-3]]:8: warning: enum should not have a trailing comma
+// CHECK-FIXES: enum MultiLineComment {
+// CHECK-FIXES-NEXT: MLC_A /* comment that
+// CHECK-FIXES-NEXT: spans multiple lines */
+// CHECK-FIXES-NEXT: };
+
void f() {
// Multi-line init lists with trailing commas - should warn to remove
int a[] = {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
new file mode 100644
index 00000000000000..e46f0341209c02
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
@@ -0,0 +1,8 @@
+// RUN: %check_clang_tidy %s readability-trailing-comma %t -- \
+// RUN: -- -I %S/Inputs/trailing-comma
+
+// The enum's opening brace is in the header and its closing brace is here.
+// The check must not hang looking for the closing brace.
+#include "split-enum.h"
+ SplitB
+};
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
index fb15931731b076..fc120002c6a67f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
@@ -190,6 +190,21 @@ enum NullDirectiveAfter {
#
};
+enum MultiLineCommentAfterComma {
+ MLC_A, /* comment that
+ spans multiple lines */
+};
+
+enum MultiLineCommentNoComma {
+ MLC_B /* comment that
+ spans multiple lines */
+};
+// CHECK-MESSAGES: :[[@LINE-3]]:8: warning: enum should have a trailing comma
+// CHECK-FIXES: enum MultiLineCommentNoComma {
+// CHECK-FIXES-NEXT: MLC_B, /* comment that
+// CHECK-FIXES-NEXT: spans multiple lines */
+// CHECK-FIXES-NEXT: };
+
// Macros are ignored
#define ENUM(n, a, b) enum n { a, b }
#define INIT {1, 2}
>From 127ad72386585431d052ffd0fbeaacbbcece34ca Mon Sep 17 00:00:00 2001
From: ljkx <177671075+ljkx at users.noreply.github.com>
Date: Wed, 11 Nov 2026 23:52:47 +0100
Subject: [PATCH 2/2] Lex only the closing brace's file, with a single raw
lexer
Keep diagnosing enums whose braces are in different files, and avoid
creating a new lexer for every token.
Co-Authored-By: Claude Fable 5.2 <noreply at anthropic.com>
---
.../readability/TrailingCommaCheck.cpp | 42 ++++++++++---------
.../readability/trailing-comma-split-enum.cpp | 4 +-
2 files changed, 26 insertions(+), 20 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
index 4a2741bc2a602c..bd7e2581637bec 100644
--- a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
@@ -122,27 +122,31 @@ void TrailingCommaCheck::checkEnumDecl(const EnumDecl *Enum,
if (Policy == CommaPolicyKind::Ignore)
return;
- // Lex forward from the opening brace rather than backward from the closing
- // one: lexing backward restarts at the beginning of a line, which yields
- // bogus tokens when that line is inside a multi-line block comment.
- const SourceLocation RBraceLoc = Enum->getBraceRange().getEnd();
+ // Find the last token before the closing brace by lexing forward rather
+ // than backward from the brace: lexing backward restarts at the beginning of
+ // a line, which yields bogus tokens when that line is inside a multi-line
+ // block comment.
+ const SourceManager &SM = *Result.SourceManager;
+ const auto [LBraceFile, LBraceOffset] =
+ SM.getDecomposedExpansionLoc(Enum->getBraceRange().getBegin());
+ const auto [RBraceFile, RBraceOffset] =
+ SM.getDecomposedExpansionLoc(Enum->getBraceRange().getEnd());
+ // The braces can be in different files, e.g. when the enum's body is split
+ // across an #include. Then start at the beginning of the closing brace's
+ // file.
+ const bool SameFile = LBraceFile == RBraceFile;
+ const StringRef Buffer = SM.getBufferData(RBraceFile);
+ Lexer RawLexer(SM.getLocForStartOfFile(RBraceFile), getLangOpts(),
+ Buffer.begin(), Buffer.data() + (SameFile ? LBraceOffset : 0),
+ Buffer.end());
std::optional<Token> LastTok;
- SourceLocation Loc = Enum->getBraceRange().getBegin();
- while (true) {
- const std::optional<Token> Tok =
- Lexer::findNextToken(Loc, *Result.SourceManager, getLangOpts());
- // The braces can be in different files, e.g. when the enum's body is
- // split across an #include.
- if (!Tok || Tok->is(tok::eof))
- return;
- if (Tok->getLocation() == RBraceLoc)
- break;
- if (!Result.SourceManager->isBeforeInTranslationUnit(Tok->getLocation(),
- RBraceLoc))
- return;
+ Token Tok;
+ // Skip the opening brace.
+ if (SameFile && RawLexer.LexFromRawLexer(Tok))
+ return;
+ while (!RawLexer.LexFromRawLexer(Tok) &&
+ SM.getFileOffset(Tok.getLocation()) < RBraceOffset)
LastTok = Tok;
- Loc = Tok->getLocation();
- }
if (!LastTok)
return;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
index e46f0341209c02..1531bde2290346 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
@@ -2,7 +2,9 @@
// RUN: -- -I %S/Inputs/trailing-comma
// The enum's opening brace is in the header and its closing brace is here.
-// The check must not hang looking for the closing brace.
#include "split-enum.h"
SplitB
};
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: enum should have a trailing comma
+// CHECK-FIXES: SplitB,
+// CHECK-FIXES-NEXT: };
More information about the cfe-commits
mailing list