-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang-tidy] fix crash due to assumed callee in min-max-use-initializer-list #91992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…er-list 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.
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Julian Schmidt (5chmidti) ChangesPreviously, the call to Fixes #91982 Full diff: https://github.com/llvm/llvm-project/pull/91992.diff 2 Files Affected:
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
|
CC @sopyb I was already looking into it ^^ |
I have a few small changes to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad someone was around to make a fix 🙂
Previously, the call to
findArgs
for aCallExpr
inside of amin
ormax
call would callfindArgs
before checking if the argument is acall to
min
ormax
, which is whatfindArgs
is expecting.The fix moves the name checking before the call to
findArgs
, such thatonly a
min
ormax
function call is used as an argument.Fixes #91982
Fixes #92249