https://github.com/xxxxbc updated 
https://github.com/llvm/llvm-project/pull/208782

>From d2b5443c19382a42d8a84db2ebc7f0dff4cd71b1 Mon Sep 17 00:00:00 2001
From: hehuan <[email protected]>
Date: Sat, 11 Jul 2026 01:11:39 +0800
Subject: [PATCH] [clang-tidy] Fix trailing semicolon and lost comment in
 readability-use-std-min-max

For non-compound if bodies (no braces), If->getEndLoc() returns the end
of the expression, not the trailing semicolon. The replacement range
therefore stopped before the semicolon, causing a duplicate semicolon in
the output. Any comments between the expression and the semicolon were
also lost.

Add handling for the non-compound case that mirrors the existing
compound-statement logic: find the trailing semicolon, capture any
intervening comments, and extend the replacement range to cover it.

Fixes #208708.
---
 .../clang-tidy/readability/UseStdMinMaxCheck.cpp   | 14 +++++++++++++-
 clang-tools-extra/docs/ReleaseNotes.rst            |  4 ++++
 .../checkers/readability/use-std-min-max.cpp       | 12 ++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
index ed1af05d232ce..c382c0fcb5eb3 100644
--- a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
@@ -167,7 +167,7 @@ void UseStdMinMaxCheck::check(const 
MatchFinder::MatchResult &Result) {
   const auto *BinaryOp = Result.Nodes.getNodeAs<BinaryOperator>("binaryOp");
   const BinaryOperatorKind BinaryOpcode = BinaryOp->getOpcode();
   const SourceLocation IfLocation = If->getIfLoc();
-  const SourceLocation ThenLocation = If->getEndLoc();
+  SourceLocation ThenLocation = If->getEndLoc();
 
   auto ReplaceAndDiagnose = [&](const StringRef FunctionName) {
     const SourceManager &Source = *Result.SourceManager;
@@ -218,6 +218,18 @@ void UseStdMinMaxCheck::check(const 
MatchFinder::MatchResult &Result) {
       if (Semi != StringRef::npos && PostInner.take_front(Semi).trim().empty())
         PostInner = PostInner.drop_front(Semi + 1);
       AppendNormalized(PostInner);
+    } else {
+      // Non-compound case: find the trailing semicolon after the expression
+      // and capture any comments between the expression end and the
+      // semicolon.
+      std::optional<Token> SemiTok = Lexer::findNextToken(
+          Lexer::getLocForEndOfToken(ThenLocation, 0, Source, LO), Source, LO);
+      if (SemiTok && SemiTok->is(tok::semi)) {
+        StringRef BetweenText =
+            GetSourceText(ThenLocation, SemiTok->getLocation());
+        AppendNormalized(BetweenText);
+        ThenLocation = SemiTok->getLocation();
+      }
     }
 
     diag(IfLocation, "use `%0` instead of `%1`")
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 9a5a23f3d8542..66ce08534cae8 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -121,6 +121,10 @@ Changes in existing checks
   exclusively for overload resolution. Added the :option:`IgnoredTypes`
   option to allow customizing the set of ignored types.
 
+- 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.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
index 35570189e1122..0e74eaeb36e08 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
@@ -253,6 +253,18 @@ void testVectorSizeType() {
     value = v.size();
 }
 
+namespace gh208708 {
+void f(int &n) {
+  // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::max` instead of `<` 
[readability-use-std-min-max]
+  // CHECK-FIXES: n = std::max(n, -1);
+  if (n < -1) n = -1 ;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::min` instead of `>` 
[readability-use-std-min-max]
+  // CHECK-FIXES: n = std::min(n, 1); /*use clamp when c++17 or later is 
enabled*/
+  if (n > 1) n = 1/*use clamp when c++17 or later is enabled*/;
+}
+} // namespace gh208708
+
 namespace gh121676 {
 
 void useLeft() {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to