Menu

Wrong assignmentInAssert when using lambda in predicate function

Jens Auer
2015-11-13
2015-11-13
  • Jens Auer

    Jens Auer - 2015-11-13

    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

     
  • Daniel Marjamäki

    Thanks! Can you please report this in our issue tracker?

     
  • Jens Auer

    Jens Auer - 2015-11-13
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.