| Issue |
238783
|
| Summary |
[clang-tidy] False positive bugprone-std-namespace-modification with constrained partial specializations
|
| Labels |
clang-tidy
|
| Assignees |
|
| Reporter |
TedLyngmo
|
bugprone-std-namespace-modification (also cert-dcl58-cpp) warns on partial specializations of standard library templates that are constrained to program-defined types by a concept or a requires-clause.
[namespace.std]/2 allows a program to add a specialization of a standard library class template to namespace `std` provided that the added declaration "depends on at least one program-defined type". A partial specialization whose template parameter is constrained to program-defined types satisfies that, but the check only looks at the template arguments as written. A bare `T` is not a program-defined type, so the constraint is ignored.
Example:
```cpp
#include <concepts>
#include <cstddef>
#include <functional>
#include <type_traits>
namespace foo {
template <class...>
struct Foo {};
template <class>
struct is_Foo : std::false_type {};
template <class... Args>
struct is_Foo<Foo<Args...>> : std::true_type {};
template <class T>
concept FooType = is_Foo<T>::value;
struct Base {};
} // namespace foo
// Constrained to program-defined types through a program-defined concept.
template <foo::FooType T>
struct std::hash<T> {
std::size_t operator()(const T &) const noexcept { return 0; }
};
// Constrained to types derived from a program-defined type.
template <class T>
requires std::derived_from<T, foo::Base>
struct std::hash<T> {
std::size_t operator()(const T &) const noexcept { return 0; }
};
```
`clang-tidy -checks='-*,bugprone-std-namespace-modification' repro.cpp -- -std=c++20` with clang-tidy built from main (030f8ab07f25):
```
repro.cpp:24:13: warning: modification of 'std' namespace can result in undefined behavior [bugprone-std-namespace-modification]
24 | struct std::hash<T> {
| ^
repro.cpp:31:13: warning: modification of 'std' namespace can result in undefined behavior [bugprone-std-namespace-modification]
31 | struct std::hash<T> {
| ^
```
clang-tidy 22.1.8 gives the same warnings.
Matching on a template pattern is not always an option, for example when the specialization should apply to all types derived from a program-defined base, whose set is open-ended. Constraining the specialization is then the only way to do it, apart from suppressing the warning with `NOLINT`.
The same applies to other templates that may be specialized, such as `std::tuple_size` and `std::tuple_element`.
Expected: no warning when the constraints restrict the specialization to program-defined types. The check should still warn when the constraint does not relate a program-defined type to the template parameter, for example `template <std::integral T>` or `requires (!foo::FooType<T>)`.
Related: #183752 (fixed by #183984) and #45454, which cover partial specializations over program-defined templates such as `std::hash<Bar<T>>`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs