Why can't clang tidy detect dead code in lambda function?

Viewed 111

Clang tidy can't detect dead code when I use a lambda function; but, if I replace the lambda with a normal function, Clang tidy pin-points the error like the message below:

warning GC1A2668F: Value stored to 'ret' is never read [clang-analyzer-deadcode.DeadStores]

Is there any way to enforce clang tidy to check dead code even in a lambda function?

Here is my code.(waring: code is meaningless and just for dead code testing). You can use the USE_LAMBDA define in order to switch back and forth between lambda and normal function.

#include <iostream>
#include <algorithm>

using namespace std;

int GetRet()
{
    return 5;
}

#define USE_LAMBDA 1

#if USE_LAMBDA
int main()
{
    int ary[10]{};

    for_each(begin(ary), end(ary), [](int a)
    {
            int ret = GetRet();

            if (a > 5 && ret < 1)
            {
                ret = 6;
            }
    });
    
    return 0;
}

#else

void Check(int a)
{
    int ret = GetRet();

    if (a > 5 && ret < 1)
    {
        ret = 6;
    }
}

int main()
{
    int ary[10]{};

    for_each(begin(ary), end(ary), Check);

    return 0;
}
#endif
0 Answers
Related