[clang-tools-extra] [clang-tidy] Fix invalid code generation for member typedefs in readability-use-std-min-max (PR #208762)

via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 2 09:13:25 PDT 2026


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

>From ba0ed0b9c401624b778d89a10e6e780eadc3347b Mon Sep 17 00:00:00 2001
From: hehuan <2128534713 at qq.com>
Date: Fri, 10 Jul 2026 23:50:13 +0800
Subject: [PATCH] [clang-tidy] Fix invalid code generation for member typedefs
 in readability-use-std-min-max

When the two compared operands have different types, the check emits an
explicit template argument for std::min/std::max. For class member
typedefs (e.g. std::string::size_type), the bare typedef name is not
usable at the fix location, producing code that does not compile.

Fix getNonTemplateAlias() to keep desugaring when the typedef is
declared inside a record (class/struct), so the underlying type is used
instead.

Fixes #208693.
---
 .../readability/UseStdMinMaxCheck.cpp           | 10 +++++++---
 clang-tools-extra/docs/ReleaseNotes.rst         |  5 ++++-
 .../checkers/readability/use-std-min-max.cpp    | 17 +++++++++++++++++
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
index 38e6dfb5328fa..67396c58736a9 100644
--- a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
@@ -63,9 +63,13 @@ static QualType getNonTemplateAlias(QualType QT) {
   while (true) {
     // cast to a TypedefType
     if (const auto *TT = dyn_cast<TypedefType>(QT)) {
-      // check if the typedef is a template and if it is dependent
-      if (!TT->getDecl()->getDescribedTemplate() &&
-          !TT->getDecl()->getDeclContext()->isDependentContext())
+      const TypedefNameDecl *TD = TT->getDecl();
+      // Check if the typedef is a template and if it is dependent. A class
+      // member typedef (e.g. std::string::size_type) is not usable by its
+      // bare name at the location of the fix, so keep desugaring in that case.
+      if (!TD->getDescribedTemplate() &&
+          !TD->getDeclContext()->isDependentContext() &&
+          !TD->getDeclContext()->isRecord())
         return QT;
       QT = TT->desugar();
     } else {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 66ce08534cae8..8d1edfc0b842e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -123,7 +123,10 @@ Changes in existing checks
 
 - 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.
+  trailing semicolons and lost comments when the ``if`` body has no braces, and
+  by fixing invalid code generation when the explicit template argument resolved
+  to a class member typedef (for example ``std::string::size_type``); the
+  underlying type is now used instead.
 
 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 ebc3783e84650..19a2f7b94548f 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
@@ -265,6 +265,23 @@ void f(int &n) {
 }
 } // namespace gh208708
 
+namespace gh208693 {
+struct B {
+protected:
+  typedef unsigned long bsize;
+};
+struct S : B {
+  typedef B::bsize size_type;
+  size_type size() const;
+};
+void f(const S &s, unsigned &n) {
+  // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::max` instead of `>` [readability-use-std-min-max]
+  // CHECK-FIXES: n = std::max<unsigned long>(s.size(), n);
+  if (s.size() > n)
+    n = s.size();
+}
+} // namespace gh208693
+
 namespace gh121676 {
 
 void useLeft() {



More information about the cfe-commits mailing list