[clang-tools-extra] [clang-tidy] Suggest materializing temporary ranges in readability-use-anyofallof (PR #185791)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 20 06:54:04 PDT 2026
================
@@ -17,3 +20,55 @@ bool good_all_of() {
return false;
return true;
}
+
+std::vector<int> get_dummy_vec();
+
+bool good_any_of_temporary_vector() {
+ // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: replace loop by 'std::ranges::any_of()'
+ for (int i : get_dummy_vec())
+ if (i)
+ return true;
+ return false;
+}
+
+bool good_all_of_temporary_vector() {
+ // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: replace loop by 'std::ranges::all_of()'
+ for (int i : get_dummy_vec())
+ if (i)
+ return false;
+ return true;
+}
+
+bool any_of_initializer_list(int a, int b, int c) {
+ // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: replace loop by 'std::ranges::any_of()'
+ // CHECK-MESSAGES: :[[@LINE+1]]:23: note: reusing the temporary range directly in the replacement may be unsafe; consider materializing it in a local variable first
+ for (const auto i : {a, b, c})
+ if (i == 0)
+ return true;
+ return false;
+}
+
+bool all_of_initializer_list(int a, int b, int c) {
+ // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: replace loop by 'std::ranges::all_of()'
+ // CHECK-MESSAGES: :[[@LINE+1]]:23: note: reusing the temporary range directly in the replacement may be unsafe; consider materializing it in a local variable first
+ for (const auto i : {a, b, c})
+ if (i == 0)
+ return false;
+ return true;
+}
+
+bool good_any_of_explicit_initializer_list(int a, int b, int c) {
+ // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: replace loop by 'std::ranges::any_of()'
+ for (const auto i : std::initializer_list<int>{a, b, c})
+ if (i == 0)
+ return true;
+ return false;
+}
+
+bool good_all_of_explicit_initializer_list(int a, int b, int c) {
+ // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: replace loop by 'std::ranges::all_of()'
+ for (const auto i : std::initializer_list<int>{a, b, c})
+ if (i == 0)
+ return false;
+ return true;
----------------
zeyi2 wrote:
> These should have a note too
Thanks! Added.
> maybe thats the return false that didn't work that you mentioned.
After some trying, the missing notes are because of `IgnoreParenImpCasts()` vs `IgnoreParenCasts()`.
Also, `return false` will actually not emit any notes for range-init (e.g. both `{a, b, c}` and `std::initializer_list<int>{a,b,c}`). I believe this is against what we want.
https://github.com/llvm/llvm-project/pull/185791
More information about the cfe-commits
mailing list