Skip to content

bugprone-unused-return-value reported on iterator increment #84705

@firewave

Description

@firewave

With bugprone-unused-return-value.CheckFunctions set to ::.*.

#include <string>

void f()
{
    std::string s;
    for (std::string::const_iterator it = s.cbegin(); it != s.cend(); ++it) {}
    for (std::string::const_iterator it = s.cbegin(); it != s.cend(); it++) {}
}
test.cpp:6:71: error: the value returned by this function should not be disregarded; neglecting it may lead to errors [bugprone-unused-return-value,-warnings-as-errors]
    6 |     for (std::string::const_iterator it = s.cbegin(); it != s.cend(); ++it) {}
      |                                                                       ^~~~
test.cpp:6:71: note: cast the expression to void to silence this warning
test.cpp:7:71: error: the value returned by this function should not be disregarded; neglecting it may lead to errors [bugprone-unused-return-value,-warnings-as-errors]
    7 |     for (std::string::const_iterator it = s.cbegin(); it != s.cend(); it++) {}
      |                                                                       ^~~~
test.cpp:7:71: note: cast the expression to void to silence this warning

The result is actually used by the loop condition.

Some context on why I am trying to match all functions which might seem excessive.
This is actually something I wanted for quite a while. The idea was a bit more limited in that I wanted to find functions within test code which do not check the results. But simply enabling it in the context of the test folder actually has the same effect and actually goes a bit further.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions