I have code like
assert( std::all_of(first, last,
{
auto const tmp = x.someValue();
auto const expected = someOtherValue;
return tmp == expected;
} );
This creates a assignmentInLambda violation because cppcheck thinks that there is a side-effect in the assert statement. I think the check just searches for assignments in the expression. I don't if it possible to really detect that these are variables local to the lambda body when you consider captures by value and reference with or without mutable lambdas. This could get complicated:
assert( std::all_of(first, last, =value, &ref mutable
{
value = 10;
ref = 11;
} );
If this is not possible, I think a heuristic would be to not consider assignments to const/constexpr variables as side-effect, and to not consider any local variables in the lambda body.
Cheers,
Jens
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have code like
assert( std::all_of(first, last,
{
auto const tmp = x.someValue();
auto const expected = someOtherValue;
return tmp == expected;
} );
This creates a assignmentInLambda violation because cppcheck thinks that there is a side-effect in the assert statement. I think the check just searches for assignments in the expression. I don't if it possible to really detect that these are variables local to the lambda body when you consider captures by value and reference with or without mutable lambdas. This could get complicated:
assert( std::all_of(first, last, =value, &ref mutable
{
value = 10;
ref = 11;
} );
If this is not possible, I think a heuristic would be to not consider assignments to const/constexpr variables as side-effect, and to not consider any local variables in the lambda body.
Cheers,
Jens
Thanks! Can you please report this in our issue tracker?
Opened issue 7130: http://trac.cppcheck.net/ticket/7130