Skip to content

Commit 52eb8d6

Browse files
committed
Fixes for Qt code
Since Qt objects are based on reference counting/garbage collection, they can have some unexpected behaviors, specifically when dealing with lists. I have fixed many instance of different Qt related issues after being identified using Clazy. https://github.com/KDE/clazy/blob/1.11/docs/checks/README-range-loop-detach.md https://github.com/KDE/clazy/blob/1.11/docs/checks/README-detaching-temporary.md https://github.com/KDE/clazy/blob/1.11/docs/checks/README-use-static-qregularexpression.md https://github.com/KDE/clazy/blob/1.11/docs/checks/README-qproperty-without-notify.md
1 parent 88a3ba8 commit 52eb8d6

22 files changed

Lines changed: 77 additions & 65 deletions

src/kicad/itemmodel/componentlibtreeview.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void ComponentLibTreeView::remove()
130130
return;
131131
}
132132
QList<QPersistentModelIndex> pindex;
133-
for (QModelIndex selected : selection)
133+
for (QModelIndex selected : qAsConst(selection))
134134
{
135135
const QModelIndex &indexComponent = _sortProxy->mapToSource(selected);
136136
if (!indexComponent.isValid() || indexComponent.column() != 0)

src/kicad/itemmodel/componentpinsitemmodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ QString ComponentPinsItemModel::toNumeric(const QString &str)
300300
{
301301
QString sortPatern = str;
302302

303-
QRegularExpression numPattern("([^0-9]*)([0-9]+)([^0-9]*)", QRegularExpression::CaseInsensitiveOption);
303+
static QRegularExpression numPattern("([^0-9]*)([0-9]+)([^0-9]*)", QRegularExpression::CaseInsensitiveOption);
304304

305305
QRegularExpressionMatchIterator numMatchIt = numPattern.globalMatch(str);
306306
if (numMatchIt.hasNext())
@@ -322,7 +322,7 @@ void ComponentPinsItemModel::updateHigherPin()
322322
_higherPin.clear();
323323
if (_component != nullptr)
324324
{
325-
for (Pin *pin : _component->pins())
325+
for (Pin *pin : qAsConst(_component->pins()))
326326
{
327327
QString numPin = toNumeric(pin->padName());
328328
if (numPin > higherNumPin)
@@ -331,7 +331,7 @@ void ComponentPinsItemModel::updateHigherPin()
331331
higherNumPin = numPin;
332332
}
333333
}
334-
QRegularExpression higherNumPinPattern("([A-Z]*0*)([1-9][0-9]*)", QRegularExpression::CaseInsensitiveOption);
334+
static QRegularExpression higherNumPinPattern("([A-Z]*0*)([1-9][0-9]*)", QRegularExpression::CaseInsensitiveOption);
335335
QRegularExpressionMatchIterator higherNumPinMatchIt = higherNumPinPattern.globalMatch(_higherPin);
336336
if (higherNumPinMatchIt.hasNext())
337337
{

src/kicad/itemmodel/componentpinstableview.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void ComponentPinsTableView::remove()
131131
if (!selection.empty())
132132
{
133133
QList<QPersistentModelIndex> pindex;
134-
for (QModelIndex selected : selection)
134+
for (QModelIndex selected : qAsConst(selection))
135135
{
136136
const QModelIndex &indexComponent = _sortProxy->mapToSource(selected);
137137
if (!indexComponent.isValid())
@@ -179,7 +179,8 @@ void ComponentPinsTableView::updateSelect(const QItemSelection &selected, const
179179
Q_UNUSED(deselected)
180180

181181
QSet<Pin *> selectedPins;
182-
for (const QModelIndex &index : selectionModel()->selectedIndexes())
182+
const auto& selected_idx = selectionModel()->selectedIndexes();
183+
for (const QModelIndex &index : selected_idx)
183184
{
184185
if (!index.isValid())
185186
{

src/kicad/ksseditor/ksssyntax.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ KSSSyntax::KSSSyntax(QTextDocument *parent)
4646
<< "label"
4747
<< "rect"
4848
<< "priority";
49-
for (const QString &pattern : keywordPatterns)
49+
for (const QString &pattern : qAsConst(keywordPatterns))
5050
{
5151
rule.pattern.setPattern("\\b(" + pattern + ")\\b");
5252
rule.format = keywordFormat;
@@ -89,7 +89,7 @@ KSSSyntax::KSSSyntax(QTextDocument *parent)
8989
<< "faledge"
9090
<< "nologic";
9191

92-
for (const QString &pattern : enumvaluesPatterns)
92+
for (const QString &pattern : qAsConst(enumvaluesPatterns))
9393
{
9494
rule.pattern.setPattern("\\b(" + pattern + ")\\b");
9595
rule.format = enumvaluesFormat;
@@ -117,7 +117,7 @@ void KSSSyntax::highlightBlock(const QString &text)
117117
PartToHighlight highlight;
118118

119119
partsToHighlight.clear();
120-
for (const HighlightingRule &rule : highlightingRules)
120+
for (const HighlightingRule &rule : qAsConst(highlightingRules))
121121
{
122122
QRegularExpressionMatch match = rule.pattern.match(text);
123123
if (match.hasMatch())

src/kicad/pinruler/pinclass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ void PinClass::sortPins()
122122
return;
123123
}
124124

125-
QRegularExpression pattern(_sortPattern, QRegularExpression::CaseInsensitiveOption);
126-
QRegularExpression numPattern("([^0-9]*)([0-9]+)([^0-9]*)", QRegularExpression::CaseInsensitiveOption);
125+
static QRegularExpression pattern(_sortPattern, QRegularExpression::CaseInsensitiveOption);
126+
static QRegularExpression numPattern("([^0-9]*)([0-9]+)([^0-9]*)", QRegularExpression::CaseInsensitiveOption);
127127

128-
for (PinClassItem *pinItem : _pins)
128+
for (PinClassItem *pinItem : qAsConst(_pins))
129129
{
130130
QString sortPatern;
131131
QString pinName = pinItem->pin()->name();
@@ -220,7 +220,7 @@ void PinClass::setPos(const QPoint &basePos)
220220
break;
221221
}
222222
sortPins();
223-
for (PinClassItem *pinItem : _pins)
223+
for (PinClassItem *pinItem : qAsConst(_pins))
224224
{
225225
pinItem->pin()->setAngle(angle);
226226
pinItem->pin()->setPos(pinPos + translate);

src/kicad/pinruler/pinruler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void PinRuler::organize(Component *component)
5959
component->clearDraws();
6060

6161
PinClass *defaultClass = pinClass("default");
62-
for (Pin *pin : component->pins())
62+
for (Pin *pin : qAsConst(component->pins()))
6363
{
6464
PinClassItem *pinClassItem = new PinClassItem(pin);
6565
const QList<PinRule *> &rules = _ruleSet->rulesForPin(pin->name());
@@ -131,7 +131,7 @@ void PinRuler::organize(Component *component)
131131
QSize leftSize = QSize(0, 0);
132132
QSize rightSize = QSize(0, 0);
133133
QSize removedSize = QSize(0, 0);
134-
for (PinClass *mpinClass : _pinClasses)
134+
for (PinClass *mpinClass : qAsConst(_pinClasses))
135135
{
136136
if (mpinClass->pins().count() == 0)
137137
{
@@ -337,7 +337,7 @@ void PinRuler::organize(Component *component)
337337
component->refText()->setTextHJustify(DrawText::TextHLeft);
338338
component->refText()->setDirection(DrawText::DirectionHorizontal);
339339

340-
for (PinClass *mpinClass : _pinClasses)
340+
for (PinClass *mpinClass : qAsConst(_pinClasses))
341341
{
342342
delete mpinClass;
343343
mpinClass = nullptr;

src/kicad/pinruler/rulesparser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ void RulesParser::skipSpaceAndComments()
176176

177177
QString RulesParser::getSelector()
178178
{
179-
QRegularExpression rule(R"((\.?[a-zA-Z\(\[\.][/a-zA-Z0-9\+\-\[\]\(\)\_\|\\\*\.\^$\?:]*))");
180-
QRegularExpressionMatch ruleMath = rule.match(_data.mid(_id));
179+
static QRegularExpression rule(R"((\.?[a-zA-Z\(\[\.][/a-zA-Z0-9\+\-\[\]\(\)\_\|\\\*\.\^$\?:]*))");
180+
static QRegularExpressionMatch ruleMath = rule.match(_data.mid(_id));
181181
if (ruleMath.hasMatch() && ruleMath.capturedStart() != 0)
182182
{
183183
return QString();
@@ -209,7 +209,7 @@ QString RulesParser::getSelector()
209209

210210
QString RulesParser::getPropertyName()
211211
{
212-
QRegularExpression rule("([a-zA-Z][a-zA-Z0-9\\_\\-]*)[\t ]*:[\t ]*", QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption);
212+
static QRegularExpression rule("([a-zA-Z][a-zA-Z0-9\\_\\-]*)[\t ]*:[\t ]*", QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption);
213213
QRegularExpressionMatch ruleMath = rule.match(_data.mid(_id));
214214
if (ruleMath.hasMatch() && ruleMath.capturedStart() != 0)
215215
{
@@ -221,7 +221,7 @@ QString RulesParser::getPropertyName()
221221

222222
QString RulesParser::getPropertyValue()
223223
{
224-
QRegularExpression rule(R"lit("?([a-zA-Z0-9/^\?\$\|:\_\-\+\\\[\]\(\)\.\* ]*)"?;?)lit",
224+
static QRegularExpression rule(R"lit("?([a-zA-Z0-9/^\?\$\|:\_\-\+\\\[\]\(\)\.\* ]*)"?;?)lit",
225225
QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption);
226226
QRegularExpressionMatch ruleMath = rule.match(_data.mid(_id));
227227
if (ruleMath.hasMatch() && ruleMath.capturedStart() != 0)

src/kicad/viewer/componentitem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void ComponentItem::setComponent(Component *component, int unit)
5959
_pinItemMap.clear();
6060
_unit = unit;
6161

62-
for (Pin *pin : component->pins())
62+
for (Pin *pin : qAsConst(component->pins()))
6363
{
6464
if (pin->unit() == _unit || pin->unit() == 0)
6565
{
@@ -68,7 +68,7 @@ void ComponentItem::setComponent(Component *component, int unit)
6868
_pinItemMap.insert(pin, pinItem);
6969
}
7070
}
71-
for (Draw *draw : component->draws())
71+
for (Draw *draw : qAsConst(component->draws()))
7272
{
7373
if (draw->unit() == _unit || draw->unit() == 0)
7474
{
@@ -117,7 +117,7 @@ void ComponentItem::setShowElectricalType(bool showElectricalType)
117117
{
118118
if (showElectricalType != _showElectricalType)
119119
{
120-
for (PinItem *pinItem : _pinItemMap)
120+
for (PinItem *pinItem : qAsConst(_pinItemMap))
121121
{
122122
pinItem->setShowElectricalType(showElectricalType);
123123
}

src/kicad/viewer/componentviewer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ void ComponentViewer::selectedItem()
166166
{
167167
QList<Pin *> selectedPins;
168168

169-
for (QGraphicsItem *item : scene()->selectedItems())
169+
const auto& selected = scene()->selectedItems();
170+
for (QGraphicsItem *item : selected)
170171
{
171172
PinItem *pinItem = qgraphicsitem_cast<PinItem *>(item);
172173
selectedPins.append(pinItem->pin());

src/kicad/viewer/drawpolyitem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void DrawPolyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
4949
}
5050

5151
QPolygon poly;
52-
for (QPoint pt : _drawPoly->points())
52+
for (QPoint pt : qAsConst(_drawPoly->points()))
5353
{
5454
poly.append(pt / ComponentItem::ratio);
5555
}
@@ -67,7 +67,7 @@ void DrawPolyItem::setDraw(DrawPoly *draw)
6767
_drawPoly = draw;
6868
QRect mrect(0, 0, 1, 1);
6969

70-
for (QPoint pt : _drawPoly->points())
70+
for (QPoint pt : qAsConst(_drawPoly->points()))
7171
{
7272
mrect = mrect.united(QRect(pt / ComponentItem::ratio, QSize(1, 1)));
7373
}

0 commit comments

Comments
 (0)