-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
. #97766
. #97766
Conversation
…lambdas with auto. Clang-Tidy unnecessary-value-param value param will be triggered for templated functions if at least one instantiontion with expensive to copy type is present in translation unit. It is relatively common mistake to write lambda functions with auto arguments for expensive to copy types. Example: Copy of the vectors will happen on every comparison. void SortBySize(std::vector<std::vector<int>>& a) { std::sort( a.begin(), a.end(), [](auto x, auto y) { return a.size() < b.size()}); }
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Vitaly Goldshteyn (goldvitaly) ChangesClang-Tidy unnecessary-value-param value param will be triggered for It is relatively common mistake to write lambda functions with auto Example: void SortBySize(std::vector<std::vector<int>>& a) { Full diff: https://github.com/llvm/llvm-project/pull/97766.diff 1 Files Affected:
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-templates.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-templates.cpp
new file mode 100644
index 0000000000000..4e6ec401f17c8
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-templates.cpp
@@ -0,0 +1,92 @@
+// RUN: %check_clang_tidy -std=c++14-or-later %s performance-unnecessary-value-param %t
+
+struct ExpensiveToCopyType {
+ virtual ~ExpensiveToCopyType();
+};
+
+template <typename T> void templateWithNonTemplatizedParameter(const ExpensiveToCopyType S, T V) {
+ // CHECK-MESSAGES: [[@LINE-1]]:90: warning: the const qualified parameter 'S'
+ // CHECK-MESSAGES: [[@LINE-2]]:95: warning: the parameter 'V'
+ // CHECK-FIXES: template <typename T> void templateWithNonTemplatizedParameter(const ExpensiveToCopyType& S, const T& V) {
+}
+
+void instantiatedWithExpensiveValue() {
+ templateWithNonTemplatizedParameter(
+ ExpensiveToCopyType(), ExpensiveToCopyType());
+ templateWithNonTemplatizedParameter(ExpensiveToCopyType(), 5);
+}
+
+template <typename T> void templateWithNonTemplatizedParameterCheapTemplate(const ExpensiveToCopyType S, T V) {
+ // CHECK-MESSAGES: [[@LINE-1]]:103: warning: the const qualified parameter 'S'
+ // CHECK-FIXES: template <typename T> void templateWithNonTemplatizedParameterCheapTemplate(const ExpensiveToCopyType& S, T V) {
+}
+
+void instantiatedWithCheapValue() {
+ templateWithNonTemplatizedParameterCheapTemplate(ExpensiveToCopyType(), 5);
+}
+
+template <typename T> void nonInstantiatedTemplateWithConstValue(const T S) {}
+template <typename T> void nonInstantiatedTemplateWithNonConstValue(T S) {}
+
+template <typename T> void instantiatedTemplateSpecialization(T NoSpecS) {}
+template <>
+void instantiatedTemplateSpecialization<ExpensiveToCopyType>(
+ ExpensiveToCopyType SpecS) {
+ // CHECK-MESSAGES: [[@LINE-1]]:25: warning: the parameter 'SpecS'
+ // CHECK-FIXES: const T& NoSpecS
+ // CHECK-FIXES: const ExpensiveToCopyType& SpecS
+}
+
+void instantiatedTemplateSpecialization() {
+ instantiatedTemplateSpecialization(ExpensiveToCopyType());
+}
+
+template <typename T> void instantiatedTemplateWithConstValue(const T S) {
+ // CHECK-MESSAGES: [[@LINE-1]]:71: warning: the const qualified parameter 'S'
+ // CHECK-FIXES: template <typename T> void instantiatedTemplateWithConstValue(const T& S) {
+}
+
+void instantiatedConstValue() {
+ instantiatedTemplateWithConstValue(ExpensiveToCopyType());
+}
+
+template <typename T> void instantiatedTemplateWithNonConstValue(T S) {
+ // CHECK-MESSAGES: [[@LINE-1]]:68: warning: the parameter 'S'
+ // CHECK-FIXES: template <typename T> void instantiatedTemplateWithNonConstValue(const T& S) {
+}
+
+void instantiatedNonConstValue() {
+ instantiatedTemplateWithNonConstValue(ExpensiveToCopyType());
+}
+
+void lambdaConstValue() {
+ auto fn = [](const ExpensiveToCopyType S) {
+ // CHECK-MESSAGES: [[@LINE-1]]:42: warning: the const qualified parameter 'S'
+ // CHECK-FIXES: auto fn = [](const ExpensiveToCopyType& S) {
+ };
+ fn(ExpensiveToCopyType());
+}
+
+void lambdaNonConstValue() {
+ auto fn = [](ExpensiveToCopyType S) {
+ // CHECK-MESSAGES: [[@LINE-1]]:36: warning: the parameter 'S'
+ // CHECK-FIXES: auto fn = [](const ExpensiveToCopyType& S) {
+ };
+ fn(ExpensiveToCopyType());
+}
+
+void lambdaConstAutoValue() {
+ auto fn = [](const auto S) {
+ // CHECK-MESSAGES: [[@LINE-1]]:27: warning: the const qualified parameter 'S'
+ // CHECK-FIXES: auto fn = [](const auto& S) {
+ };
+ fn(ExpensiveToCopyType());
+}
+
+void lambdaNonConstAutoValue() {
+ auto fn = [](auto S) {
+ // CHECK-MESSAGES: [[@LINE-1]]:21: warning: the parameter 'S'
+ // CHECK-FIXES: auto fn = [](const auto& S) {
+ };
+ fn(ExpensiveToCopyType());
+}
|
Clang-Tidy unnecessary-value-param value param will be triggered for
templated functions if at least one instantiontion with expensive to
copy type is present in translation unit.
It is relatively common mistake to write lambda functions with auto
arguments for expensive to copy types.
Example:
Copy of the vectors will happen on every comparison.
void SortBySize(std::vector<std::vector>& a) {
std::sort(
a.begin(), a.end(),
[](auto x, auto y) { return a.size() < b.size()});
}