[clang-tools-extra] 932f0de - [clang-tidy] fix crash due to assumed callee in min-max-use-initializer-list (#91992)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 15 05:52:36 PDT 2024
Author: Julian Schmidt
Date: 2024-05-15T14:52:32+02:00
New Revision: 932f0de43a9e334e161a69a50bd6b01cd51e238e
URL: https://github.com/llvm/llvm-project/commit/932f0de43a9e334e161a69a50bd6b01cd51e238e
DIFF: https://github.com/llvm/llvm-project/commit/932f0de43a9e334e161a69a50bd6b01cd51e238e.diff
LOG: [clang-tidy] fix crash due to assumed callee in min-max-use-initializer-list (#91992)
Previously, the call to `findArgs` for a `CallExpr` inside of a `min` or
`max` call would call `findArgs` before checking if the argument is a
call to `min` or `max`, which is what `findArgs` is expecting.
The fix moves the name checking before the call to `findArgs`, such that
only a `min` or `max` function call is used as an argument.
Fixes #91982
Fixes #92249
Added:
Modified:
clang-tools-extra/clang-tidy/modernize/MinMaxUseInitializerListCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize/min-max-use-initializer-list.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/modernize/MinMaxUseInitializerListCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MinMaxUseInitializerListCheck.cpp
index 45f7700463d57..418699ffbc4d1 100644
--- a/clang-tools-extra/clang-tidy/modernize/MinMaxUseInitializerListCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MinMaxUseInitializerListCheck.cpp
@@ -129,17 +129,17 @@ generateReplacements(const MatchFinder::MatchResult &Match,
continue;
}
+ // if the nested call is not the same as the top call
+ if (InnerCall->getDirectCallee()->getQualifiedNameAsString() !=
+ TopCall->getDirectCallee()->getQualifiedNameAsString())
+ continue;
+
const FindArgsResult InnerResult = findArgs(InnerCall);
// if the nested call doesn't have arguments skip it
if (!InnerResult.First || !InnerResult.Last)
continue;
- // if the nested call is not the same as the top call
- if (InnerCall->getDirectCallee()->getQualifiedNameAsString() !=
- TopCall->getDirectCallee()->getQualifiedNameAsString())
- continue;
-
// if the nested call doesn't have the same compare function
if ((Result.Compare || InnerResult.Compare) &&
!utils::areStatementsIdentical(Result.Compare, InnerResult.Compare,
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/min-max-use-initializer-list.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/min-max-use-initializer-list.cpp
index 51ab9bda975f1..1f2dad2b933ca 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/min-max-use-initializer-list.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/min-max-use-initializer-list.cpp
@@ -300,6 +300,27 @@ B maxTT2 = std::max(B(), std::max(B(), B()));
B maxTT3 = std::max(B(), std::max(B(), B()), [](const B &lhs, const B &rhs) { return lhs.a[0] < rhs.a[0]; });
// CHECK-FIXES: B maxTT3 = std::max(B(), std::max(B(), B()), [](const B &lhs, const B &rhs) { return lhs.a[0] < rhs.a[0]; });
+struct GH91982 {
+ int fun0Args();
+ int fun1Arg(int a);
+ int fun2Args(int a, int b);
+ int fun3Args(int a, int b, int c);
+ int fun4Args(int a, int b, int c, int d);
+
+ int foo() {
+ return std::max(
+ fun0Args(),
+ std::max(fun1Arg(0),
+ std::max(fun2Args(0, 1),
+ std::max(fun3Args(0, 1, 2), fun4Args(0, 1, 2, 3)))));
+// CHECK-MESSAGES: :[[@LINE-5]]:12: warning: do not use nested 'std::max' calls, use an initializer list instead [modernize-min-max-use-initializer-list]
+// CHECK-FIXES: return std::max(
+// CHECK-FIXES-NEXT: {fun0Args(),
+// CHECK-FIXES-NEXT: fun1Arg(0),
+// CHECK-FIXES-NEXT: fun2Args(0, 1),
+// CHECK-FIXES-NEXT: fun3Args(0, 1, 2), fun4Args(0, 1, 2, 3)});
+ }
+};
} // namespace
More information about the cfe-commits
mailing list