| Issue |
180441
|
| Summary |
[clang-tidy] New check: modernize-use-shared-ptr-array
|
| Labels |
clang-tidy
|
| Assignees |
|
| Reporter |
denzor200
|
### Motivation
With C++17, `std::shared_ptr` gained proper array support through the specialization `std::shared_ptr<T[]>`. This provides:
- Type safety: Clear indication that the pointer owns an array
- Simplified syntax: No need for custom deleters
- Standard compliance: Uses standardized array deletion
- Readability: Intent is immediately clear from the type
In old C++ versions developers must use workarounds like:
```cpp
// Pre-C++17 workarounds
std::shared_ptr<A> sp1(new A[10], std::default_delete<A[]>());
std::shared_ptr<A> sp2(new A[10], [](A* p) { delete[] p; });
```
These can be modernized to:
```cpp
// C++17 and later
std::shared_ptr<A[]> sp(new A[10]);
```
### Scope
The check would identify and transform:
- `std::shared_ptr<T>` constructed with `new T[N]` and `std::default_delete<T[]>`
- `std::shared_ptr<T>` constructed with `new T[N]` and a lambda deleter containing `delete[]`
### Implementation Approach
The check would:
1. **Match patterns** using Clang AST matchers for:
- `std::shared_ptr` constructors with two arguments
- First argument: `new T[N]` _expression_
- Second argument: Custom deleter (default_delete or lambda)
2. **Verify deleter semantics**:
- For `std::default_delete<T[]>`: Confirm template argument is array type
- For lambdas: Analyze body for `delete[]` statement
- For other callables: Attempt to determine if they perform array deletion
3. **Apply transformation**:
- Change `shared_ptr<T>` to `shared_ptr<T[]>`
- Remove custom deleter argument
- Preserve any explicit template arguments or qualifications
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs