Qt-TemporaryIterator

Do not use functions returning iterators on a temporary container

Required inputs: IR

Example

// temporary list returned by function
QList<type> getList()
{
    QList<type> list;
    ... add some items to list ...
    return list;
}

// Will cause a crash if iterated using:

for (QList<type>::iterator it = getList().begin(); it != getList().end(); ++it)
{
  ...
}

because the end iterator was returned from a different container object than the begin iterator.

This rule is based on clazy rule temporary-iterator

Possible Messages

Key

Text

Severity

Disabled

temporary_iterator

Don’t call {class_name}::{member_name}() on temporary

None

False

Options

ignored_functions

ignored_functions : set[str] = {'QHash::operator[]', 'QMap::operator[]', 'QSet::operator[]', 'QVariant::toList'}

Functions whose return values are ignored by this rule.
 

level

level : int = 0

Importance level of the rule as given for clazy. 0 is most desirable, higher values fall off in quality.
 

methods_by_type

methods_by_type

Type: dict[str, set[str]]

Default:

{
   'QHash': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'constFind', 'end', 'find', 'insert', 'insertMulti'},
   'QLinkedList': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'end'},
   'QList': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'end'},
   'QMap': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'constFind', 'end', 'equal_range', 'find', 'lowerBound', 'upperBound'},
   'QMultiHash': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'constFind', 'end', 'find', 'insert', 'insertMulti'},
   'QMultiMap': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'constFind', 'end', 'equal_range', 'find', 'lowerBound', 'upperBound'},
   'QQueue': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'end'},
   'QSet': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'constFind', 'end', 'find'},
   'QStack': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'end', 'insert'},
   'QVector': {'begin', 'cbegin', 'cend', 'constBegin', 'constEnd', 'end', 'insert'},
   'std::vector': {'begin', 'cbegin', 'cend', 'end'}
}
For each container type considered by this rule, the list of member functions that must not be called.