https://github.com/xxxxbc updated https://github.com/llvm/llvm-project/pull/208762
>From 1522b7c53a39fc31767c06a4dbee4c56e2f32861 Mon Sep 17 00:00:00 2001 From: hehuan <[email protected]> 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 | 6 ++++++ .../checkers/readability/use-std-min-max.cpp | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 3 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..5edb61f429c64 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -125,6 +125,12 @@ Changes in existing checks <clang-tidy/checks/readability/use-std-min-max>` check by fixing spurious trailing semicolons and lost comments when the ``if`` body has no braces. +- Improved :doc:`readability-use-std-min-max + <clang-tidy/checks/readability/use-std-min-max>` check 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() { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
