Nikolai - 10 months ago 51

C++ Question

Can someone explain to me why the following code for a unit test gives the error unreadVariable for n and k in cppcheck?

Combinations is a template class that calculates all combinations of n choose k but this should not matter here.

`TEST(Combinations, ChooseOne)`

{

const UINT8 n = 3;

const UINT8 k = 1;

Combinations<n, k> comb;

comb.calc();

std::vector< std::vector<UINT8> > _vui8Expect = { { 2 }, { 1 }, { 0 } };

EXPECT_THAT(comb.result, ::testing::ContainerEq(_vui8Expect));

}

I can change the code to the following and not get a cppcheck error anymore. But I do not like this, because it make the code less verbose. n, k are well defined quantities in statistics and they make it more clear in the call what is going on.

`TEST(Combinations, ChooseOne)`

{

Combinations<3, 1> comb;

comb.calc();

std::vector< std::vector<UINT8> > _vui8Expect = { { 2 }, { 1 }, { 0 } };

EXPECT_THAT(comb.result, ::testing::ContainerEq(_vui8Expect));

}

Answer Source

This is a known issue: http://trac.cppcheck.net/ticket/7542

So unless it will be fixed, the cppcheck will report this false positive.