From 2973b21b9c3fc75b7ba818317b10b76a3fb8f03c Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 9 Jan 2025 23:15:18 +0800 Subject: [PATCH] [clang-tidy] use correct template type in ``std::min`` and ``std::max`` when operand is integer literal for readability-use-std-min-max --- .../readability/UseStdMinMaxCheck.cpp | 35 ++++++++++++------- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../checkers/readability/use-std-min-max.cpp | 21 +++++++++++ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp index 179173502a8d0..6f6b8a853a91e 100644 --- a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp @@ -79,6 +79,27 @@ static QualType getNonTemplateAlias(QualType QT) { return QT; } +static QualType getReplacementCastType(const Expr *CondLhs, const Expr *CondRhs, + QualType ComparedType) { + QualType LhsType = CondLhs->getType(); + QualType RhsType = CondRhs->getType(); + QualType LhsCanonicalType = + LhsType.getCanonicalType().getNonReferenceType().getUnqualifiedType(); + QualType RhsCanonicalType = + RhsType.getCanonicalType().getNonReferenceType().getUnqualifiedType(); + QualType GlobalImplicitCastType; + if (LhsCanonicalType != RhsCanonicalType) { + if (llvm::isa(CondRhs)) { + GlobalImplicitCastType = getNonTemplateAlias(LhsType); + } else if (llvm::isa(CondLhs)) { + GlobalImplicitCastType = getNonTemplateAlias(RhsType); + } else { + GlobalImplicitCastType = getNonTemplateAlias(ComparedType); + } + } + return GlobalImplicitCastType; +} + static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs, const Expr *AssignLhs, const SourceManager &Source, @@ -92,18 +113,8 @@ static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs, const llvm::StringRef AssignLhsStr = Lexer::getSourceText( Source.getExpansionRange(AssignLhs->getSourceRange()), Source, LO); - QualType GlobalImplicitCastType; - QualType LhsType = CondLhs->getType() - .getCanonicalType() - .getNonReferenceType() - .getUnqualifiedType(); - QualType RhsType = CondRhs->getType() - .getCanonicalType() - .getNonReferenceType() - .getUnqualifiedType(); - if (LhsType != RhsType) { - GlobalImplicitCastType = getNonTemplateAlias(BO->getLHS()->getType()); - } + QualType GlobalImplicitCastType = + getReplacementCastType(CondLhs, CondRhs, BO->getLHS()->getType()); return (AssignLhsStr + " = " + FunctionName + (!GlobalImplicitCastType.isNull() diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 19c59f5b32eed..f718674cb95e8 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -364,6 +364,10 @@ Changes in existing checks ` check to remove `->`, when redundant `get()` is removed. +- Improved :doc:`readability-use-std-min-max + ` check to use correct template + type in ``std::min`` and ``std::max`` when operand is integer literal. + 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 9c0e2eabda348..35ade8a7c6d37 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 @@ -252,3 +252,24 @@ void testVectorSizeType() { if (value < v.size()) value = v.size(); } + +namespace gh121676 { + +void useLeft() { + using U16 = unsigned short; + U16 I = 0; + // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::max` instead of `<` [readability-use-std-min-max] + // CHECK-FIXES: I = std::max(I, 16U); + if (I < 16U) + I = 16U; +} +void useRight() { + using U16 = unsigned short; + U16 I = 0; + // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::min` instead of `<` [readability-use-std-min-max] + // CHECK-FIXES: I = std::min(16U, I); + if (16U < I) + I = 16U; +} + +} // namespace gh121676