[clang-tools-extra] [clang-tidy] add check to suggest replacement of nested std::min or std::max with initializer lists (PR #85572)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 24 08:19:59 PDT 2024


https://github.com/5chmidti requested changes to this pull request.

Going in the right direction, but some things still have to be resolved or cleaned up.

Add a few more test (these are the ones I tried locally):
```c++
template <typename T>
struct initializer_list {
  initializer_list()=default;
  initializer_list(T*,int){}
};

template< class T >
const T& max(initializer_list<T> list);
template< class T, class Compare >
const T& max(initializer_list<T> list, Compare comp);
template< class T >
const T& min(initializer_list<T> list);
template< class T, class Compare >
const T& min(initializer_list<T> list, Compare comp);

...

int max2b = std::max(std::max(std::max(1, 2), std::max(3, 4)), std::max(std::max(5, 6), std::max(7, 8)));
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use nested std::max calls, use std::max({1, 2, 3, 4, 5, 6, 7, 8}) instead [modernize-min-max-use-initializer-list]
// CHECK-FIXES: int max2b = std::max({1, 2, 3, 4, 5, 6, 7, 8});

int max2c = std::max(std::max(1, std::max(2, 3)), 4);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use nested std::max calls, use std::max({1, 2, 3, 4}) instead [modernize-min-max-use-initializer-list]
// CHECK-FIXES: int max2c = std::max({1, 2, 3, 4});

int max2d = std::max(std::max({1, 2, 3}), 4);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use nested std::max calls, use std::max({1, 2, 3, 4}) instead [modernize-min-max-use-initializer-list]
// CHECK-FIXES: int max2d = std::max({1, 2, 3, 4});

int max2e = std::max(1, max(2, max(3, 4)));
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use nested std::max calls, use std::max({1, 2, 3, 4}) instead [modernize-min-max-use-initializer-list]
// CHECK-FIXES: int max2e = std::max({1, 2, 3, 4});
```
In the above tests, `max2b` and `max2d` fail, which IMO should work, `max2b` especially.
And please add some tests with type aliases.
My quick test with templates also tells me that they are currently unsupported:
```c++

template <typename T>
void foo(T a, T b, T c){
  auto v = std::max(a, std::max(b, c));
}
```

https://github.com/llvm/llvm-project/pull/85572


More information about the cfe-commits mailing list