<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/100869>100869</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Local object's destructor executed before function return value evaluation
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          fuhsnn
      </td>
    </tr>
</table>

<pre>
    The code use a lambda class member to implement the scope guard pattern. The object "guard"'s destructor (which execute the lambda) should be run after the return value being determined. 

godbolt: https://godbolt.org/z/aYzcn5dKK
```
#include <cstdio>

template <typename T>
struct scopeguard {
    T lambda;
    scopeguard(T&& init) : lambda(init) {};
    ~scopeguard() { lambda(); }
};

struct S {
    int i;
};

S test(void) {
    S s {};
    scopeguard guard { [&](){ s.i += 7; } };

 std::printf("returning %d\n", s.i = 22);
    return s;
}

int main()
{
  std::printf("returned  %d\n", test().i);
  return 0;
}
```
GCC and MSVC print:
```
returning 22
returned 22
```
Clang print:
```
returning 22
returned  29
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVE2P4zYM_TX0hdjAoeOvgw-TpOlh29MMCvQoW3SsQpYMS57t7qG_vZDtSeLsoIcCQQxTfHyPTzSFc-pqmCtIj5CeIzH5zo5VO3XOmKi28nv11jE2VjJOjlGgFn0tBTZaOIc99zWP6C2qftDcs_HoO0bX2IHxOolR4iC859HsMBSy9V_ceASi-RCIgHKHkp0fp8bbEYGKb51qOuS_uZk8z_UWUqASXWcnLbFmHCeDovWBvmMc2U-jwXehJ8aalbmiZM9jrwzLHUJ8hvhl-b9aWVvtIXnBzvvBQfICdAG6rAc7O16BLj-ALuLPH41J5devKz6L19_ySokyjZ4kIySnxnmpLCS_PHJ57gct_Jzgvw9sRM_4dktaul7sWtyC_LgcISK-fTSePATvyUDFG1AGlKEyygd7QlMfZhW3YH6E_Lwp8s-mypp0RwKVkBwxoJZmHuAb5a9bwcp4VPfMn1Cv6Nl5oOLdKvkh7YZ-RfeZ1gd3bh5hmFfKID2vavMjup1CoCMkZ8xX9fizBHRehhtPXoZRGd_OeFrGJ0wNUCohPZl5NE9L0eSM4a3cyFonzm3afSAKXvRCmVXgmnIr8B8yWOKzjNU2oHKntkJWGfEnMrbT-uvphMJI_P31jxPOpIH_s8y7GUSPEZa3wBPipIW5_u-ieLNnC4lklcgyKUXE1T4niot9SXnUVc2h3RdFUcaJFFRzeyjbtIzbLG-5Lts8jVRFMR3inPI9xSnlu4yTpm1zIbK45CRr4RBzL5Teaf3ehw8-Us5NXO3juMjKSIuatZuXIpHhbzifhrtIz9FYBdCXero6OMRaOe_uZbzymqvfbCP0uuyeF9y618IOa-3I2E6m8cqa7Qrj8BAhHk2jrp4WlfLdVO8a2wNdAvX6-DKMdqG8zIId0GXt6L2ifwMAAP__xAa2Qw">