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

    <tr>
        <th>Summary</th>
        <td>
            [Clang-Tidy] Unnecessary `std::trunc`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-tidy,
            check-request
      </td>
    </tr>

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

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

<pre>
    Came across the [`std::trunc`](https://en.cppreference.com/w/cpp/numeric/math/trunc) method, as we had some code which was basically:
`myClass.setIntValue(std::trunc(myFloat))`. This line lead to the warning `-Wfloat-conversion`. To prevent someone fixes this by `myClass.setIntValue(static_cast<int>(std::trunc(myFloat)))`, instead of `myClass.setIntValue(static_cast<int>(myFloat))`, it would be nice if there would be a check for this, when the result of `std::trunc` is passed directly to a integer type.

I don't know how up-to-date the debate is, but if you follow the discussion [here](https://stackoverflow.com/questions/20992209/should-i-explicitly-trunc), it also seems to be disadvantageous in terms of performance. So maybe the check could be named `performance-unnecessary-trunc`.

Just to verify there is no relevant difference, I extended the example source from the `std::trunc` documentation:
```cpp
#include <cmath>
#include <initializer_list>
#include <iostream>
 
int main()
{
 const auto data = std::initializer_list<double>
    {
        +2.7, -2.9, +0.7, -0.9, +0.0, 0.0, -INFINITY, +INFINITY, -NAN, +NAN
    };
 
    std::cout << std::showpos;
    for (double const x : data)
 {
        int truncInt = std::trunc(x);
        int castInt = static_cast<int>(x);
        std::cout << "trunc(" << x << ") => trunc: " << std::trunc(x) << " truncInt: " << truncInt << " castInt: " << castInt << '\n';
    }
}
```

Results to:
```
trunc(+2.7) => trunc: +2 truncInt: +2 castInt: +2
trunc(-2.9) => trunc: -2 truncInt: -2 castInt: -2
trunc(+0.7) => trunc: +0 truncInt: +0 castInt: +0
trunc(-0.9) => trunc: -0 truncInt: +0 castInt: +0
trunc(+0) => trunc: +0 truncInt: +0 castInt: +0
trunc(+0) => trunc: +0 truncInt: +0 castInt: +0
trunc(-inf) => trunc: -inf truncInt: -2147483648 castInt: -2147483648
trunc(+inf) => trunc: +inf truncInt: -2147483648 castInt: -2147483648
trunc(-nan) => trunc: -nan truncInt: -2147483648 castInt: -2147483648
trunc(+nan) => trunc: +nan truncInt: -2147483648 castInt: -2147483648
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk-T4rYT_TTi0mVKyGDgwGGApYrfYQ6_nSSV05YstbEysuRIMn_y6VOyPfyfpDa7U9QYWuqn9566ZXHv1c4gLshkSSbrAW9Cad3ia2OWmov3QW7labHiFQIXznoPoUSIczPqgyTpC0lfgmuMIBklkzVhszKE2scBtiFsg2Yo6tphgQ6NwKGwFWGbA2EbUdeEbUxToVOCsE3FQ0nYpgNjc6gwlFYStgLu4YBQcgneVgjCSoRDqUQJB-4h514JrvUprknXhL6QjFanlebeDz2GrQm_ct0gYbM7xmxWnTba8kDYPH4yOoS3UnnQyiBo5BKCbQUfuDPK7IBkNPmtiCmJsGaPzitrujwLtcM9mtCStAahUEeMfikP-Qn-gRQPSnwT3AeSrpQJJP3y71xbutEcZXyITG3x3Uvcq2_hAhxsoyXkCEYJBFVEBxxewhxEieIdCutadTHtUKJpnXLoGx16Ng8VAspDzb1HCVI5FEGfosUclAm4QwfhVOOw38X2_xakNYRNA7wbe4DSHqCpk2ATyQO2K0rM49eOR96EyPhkGyis1vbQTVFeND7uVSzdqOZZqfrAxbvdoyu0PfSF-meDPihrPGEbRudzxug8Ti2jGYlK8FhrJVTQp-SjcHsXufYWPGLlo8K8JcHlnpvAd2gbD8pAQFf56FWNrrCu4rFD4KuFip_yTl1ntThvCa9QRmuvMpLGGBToPXcfLDJ6Y-L_Gh8iiz06VZz6_VQejAWHGiMpkKroezQK2AIeAxqJsmWBR17VGsHbxgmEwtmqOwie7bG0oqnQxJqz5ropu0_s-y7CUmWEbiQCSVei7f_0y7MxZVRQXKu_0H3Tyof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