[clang-tools-extra] [clang-tidy] Preserve comments in `readability-use-std-min-max` (PR #169908)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 2 23:31:56 PST 2025
================
@@ -172,13 +172,52 @@ void UseStdMinMaxCheck::check(const MatchFinder::MatchResult &Result) {
auto ReplaceAndDiagnose = [&](const llvm::StringRef FunctionName) {
const SourceManager &Source = *Result.SourceManager;
+ llvm::SmallString<64> Comment;
+
+ const auto AppendNormalized = [&](llvm::StringRef Text) {
+ Text = Text.ltrim();
+ if (!Text.empty()) {
+ if (!Comment.empty())
+ Comment += " ";
+ Comment += Text;
+ }
+ };
+
+ AppendNormalized(Lexer::getSourceText(
+ CharSourceRange::getCharRange(
+ Lexer::getLocForEndOfToken(If->getRParenLoc(), 0, Source, LO),
+ If->getThen()->getBeginLoc()),
+ Source, LO));
+
+ if (const auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
+ const Stmt *Inner = CS->body_front();
+ AppendNormalized(Lexer::getSourceText(
+ CharSourceRange::getCharRange(
+ Lexer::getLocForEndOfToken(CS->getBeginLoc(), 0, Source, LO),
+ Inner->getBeginLoc()),
+ Source, LO));
+
+ llvm::StringRef PostInner = Lexer::getSourceText(
----------------
zeyi2 wrote:
Captures:
- `if (cond) { x = y; // Comment C`
- `if (cond) { x = y; /* Comment C */ }`
It also strip the trailing `;`
https://github.com/llvm/llvm-project/pull/169908
More information about the cfe-commits
mailing list