<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60896>60896</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy]: potential memory leak when init struct field during construction
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
igagis
</td>
</tr>
</table>
<pre>
# To reproduce:
check out the minimal reproducer example at:
https://github.com/igagis/clang-tidy-test/blob/master/src/main.cpp
the relevant part of the code is:
```cpp
struct result{
std::shared_ptr<std::string> p;
bool some_other_field;
};
result potential_memory_leak_2(bool arg){
result ret{.p = std::make_shared<std::string>("Hello world!")};
if(arg){
throw std::runtime_error("error");
}
return ret;
}
```
run `clang-tidy` with all checks on.
Or just refer to the minimal reproducer's CI run:
https://github.com/igagis/clang-tidy-test/actions/runs/4231046017/jobs/7349082519
# Expected result:
No false-positive potential memory leaks found
# Actual result:
The following error is reported by `clang-tidy`:
```
src/main.cpp:26:1: error: Potential leak of memory pointed to by field '_M_pi' [clang-analyzer-cplusplus.NewDeleteLeaks,-warnings-as-errors]
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyklE2TozYQhn-NuHTZBZL58IGDZ7yupCrZ5LB3SogGNCMkSh_jdX59SuCxPbObXHZqDELQ_T5qvS3unBw0Yk3yJ5IfEx78aGwtBz5Il7Smu9SEMvhmwOJsTRcEEnYg6ZGk16sYUbyCCR78iDBJLSeu7l9bwO98mhUC97fI0fvZxSd6IvQ0SD-GdivMROhpVSb0JBTXw8bL7rLx6Dyhp1aZltDTxJ1HS-jJWbE8Sr0V8_zIFEksKnzj2sPMrQfTL3jCdAjS3ZdQpOv_LYHzNggPFl1QnpRP6yw438UgdnAjt9g1s7eEPd9nvZV6IOwLzIS9xwBAa4wCZyZsjB_RNr1E1d2-IOXxPl6uqyzMxqP2kqtmwsnYS6OQvzaU0GpJyO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