Skip to content

Commit

Permalink
Misc optimisations identified by clazy:
Browse files Browse the repository at this point in the history
- call reserve on container classes where applicable
- make sure Q_FOREACH uses references
- remove dynamic casts to base classes
- fix some implicit bool conversions
  • Loading branch information
nyalldawson committed Oct 5, 2015
1 parent 890dce8 commit 0caf944
Show file tree
Hide file tree
Showing 93 changed files with 170 additions and 96 deletions.
2 changes: 1 addition & 1 deletion python/core/geometry/qgsgeometry.sip
Expand Up @@ -532,7 +532,7 @@ class QgsGeometry
public:
Error();
Error( QString m );
Error( QString m, QgsPoint p );
Error( QString m, const QgsPoint& p );

QString what();
QgsPoint where();
Expand Down
4 changes: 2 additions & 2 deletions python/core/qgslegendsettings.sip
Expand Up @@ -47,10 +47,10 @@ class QgsLegendSettings
int columnCount() const;
void setColumnCount( int c );

int splitLayer() const;
bool splitLayer() const;
void setSplitLayer( bool s );

int equalColumnWidth() const;
bool equalColumnWidth() const;
void setEqualColumnWidth( bool s );

QColor fontColor() const;
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -190,7 +190,7 @@ class QgsVectorLayer : QgsMapLayer
struct RangeData
{
RangeData();
RangeData( QVariant theMin, QVariant theMax, QVariant theStep );
RangeData( const QVariant& theMin, const QVariant& theMax, const QVariant& theStep );

QVariant mMin;
QVariant mMax;
Expand Down
3 changes: 2 additions & 1 deletion python/core/symbology-ng/qgsheatmaprenderer.sip
Expand Up @@ -43,7 +43,8 @@ class QgsHeatmapRenderer : QgsFeatureRendererV2
* @see setInvertRamp
* @see colorRamp
*/
double invertRamp() const;
bool invertRamp() const;

/** Sets whether the ramp is inverted
* @param invert set to true to invert color ramp
* @see invertRamp
Expand Down
2 changes: 1 addition & 1 deletion python/core/symbology-ng/qgsrendererv2.sip
Expand Up @@ -274,7 +274,7 @@ class QgsFeatureRendererV2
bool drawVertexMarker );

//! render editing vertex marker at specified point
void renderVertexMarker( QPointF& pt, QgsRenderContext& context );
void renderVertexMarker( const QPointF& pt, QgsRenderContext& context );
//! render editing vertex marker for a polyline
void renderVertexMarkerPolyline( QPolygonF& pts, QgsRenderContext& context );
//! render editing vertex marker for a polygon
Expand Down
2 changes: 1 addition & 1 deletion python/core/symbology-ng/qgssymbollayerv2utils.sip
Expand Up @@ -169,7 +169,7 @@ class QgsSymbolLayerV2Utils


static void labelTextToSld( QDomDocument &doc, QDomElement &element, QString label,
QFont font, QColor color = QColor(), double size = -1 );
const QFont& font, QColor color = QColor(), double size = -1 );

/** Create ogr feature style string for pen */
static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
Expand Down
2 changes: 1 addition & 1 deletion python/core/symbology-ng/qgssymbolv2.sip
Expand Up @@ -156,7 +156,7 @@ class QgsSymbolV2
* @note added in QGIS 2.9
* @see setClipFeaturesToExtent
*/
double clipFeaturesToExtent() const;
bool clipFeaturesToExtent() const;

QSet<QString> usedAttributes() const;

Expand Down
1 change: 1 addition & 0 deletions src/analysis/raster/qgsrelief.cpp
Expand Up @@ -674,6 +674,7 @@ QList< QgsRelief::ReliefColor > QgsRelief::calculateOptimizedReliefClasses()
colorList.push_back( QColor( 255, 133, 92 ) );
colorList.push_back( QColor( 204, 204, 204 ) );

resultList.reserve( classBreaks.size() );
for ( int i = 1; i < classBreaks.size(); ++i )
{
double minElevation = minMax[0] + classBreaks[i - 1] * frequencyClassRange;
Expand Down
1 change: 1 addition & 0 deletions src/analysis/vector/qgstransectsample.cpp
Expand Up @@ -614,6 +614,7 @@ QgsGeometry* QgsTransectSample::clipBufferLine( const QgsGeometry* stratumGeom,
}

int size = bufferPolygon.size();
mpl.reserve( size );
for ( int j = 0; j < size; ++j )
{
mpl.append( bufferPolygon[j] );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributetabledialog.cpp
Expand Up @@ -329,7 +329,7 @@ void QgsAttributeTableDialog::columnBoxInit()

QList<QgsField> fields = mLayer->fields().toList();

Q_FOREACH ( const QgsField field, fields )
Q_FOREACH ( const QgsField& field, fields )
{
int idx = mLayer->fieldNameIndex( field.name() );
if ( idx < 0 )
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsconfigureshortcutsdialog.cpp
Expand Up @@ -75,6 +75,7 @@ void QgsConfigureShortcutsDialog::populateActions()
QList<QAction*> actions = QgsShortcutsManager::instance()->listActions();

QList<QTreeWidgetItem *> items;
items.reserve( actions.count() );
for ( int i = 0; i < actions.count(); ++i )
{
QString actionText = actions[i]->text();
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsdiagramproperties.cpp
Expand Up @@ -683,6 +683,9 @@ void QgsDiagramProperties::apply()
QList<QColor> categoryColors;
QList<QString> categoryAttributes;
QList<QString> categoryLabels;
categoryColors.reserve( mDiagramAttributesTreeWidget->topLevelItemCount() );
categoryAttributes.reserve( mDiagramAttributesTreeWidget->topLevelItemCount() );
categoryLabels.reserve( mDiagramAttributesTreeWidget->topLevelItemCount() );
for ( int i = 0; i < mDiagramAttributesTreeWidget->topLevelItemCount(); ++i )
{
QColor color = mDiagramAttributesTreeWidget->topLevelItem( i )->background( 1 ).color();
Expand Down
1 change: 1 addition & 0 deletions src/app/qgslabeldialog.cpp
Expand Up @@ -61,6 +61,7 @@ void QgsLabelDialog::init()
const QgsFields& myFields = mLabel->fields();
QStringList myFieldStringList;
myFieldStringList.append( "" );
myFieldStringList.reserve( 1 + myFields.count() );
for ( int i = 0; i < myFields.count(); ++i )
{
myFieldStringList.append( myFields[i].name() );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdfeature.cpp
Expand Up @@ -223,7 +223,7 @@ void QgsMapToolAddFeature::cadCanvasReleaseEvent( QgsMapMouseEvent* e )
QgsCurveV2* curveToAdd = 0;
if ( hasCurvedSegments && providerSupportsCurvedSegments )
{
curveToAdd = dynamic_cast<QgsCurveV2*>( captureCurve()->clone() );
curveToAdd = captureCurve()->clone();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdpart.cpp
Expand Up @@ -134,7 +134,7 @@ void QgsMapToolAddPart::cadCanvasReleaseEvent( QgsMapMouseEvent * e )
QgsCurveV2* curveToAdd = 0;
if ( hasCurvedSegments && providerSupportsCurvedSegments )
{
curveToAdd = dynamic_cast<QgsCurveV2*>( captureCurve()->clone() );
curveToAdd = captureCurve()->clone();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdring.cpp
Expand Up @@ -92,7 +92,7 @@ void QgsMapToolAddRing::cadCanvasReleaseEvent( QgsMapMouseEvent * e )
QgsCurveV2* curveToAdd = 0;
if ( hasCurvedSegments && providerSupportsCurvedSegments )
{
curveToAdd = dynamic_cast<QgsCurveV2*>( captureCurve()->clone() );
curveToAdd = captureCurve()->clone();
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -449,6 +449,7 @@ QVariant QgsMergeAttributesDialog::sumAttribute( int col )
QVariant QgsMergeAttributesDialog::concatenationAttribute( int col )
{
QStringList concatString;
concatString.reserve( mFeatureList.size() );
for ( int i = 0; i < mFeatureList.size(); ++i )
{
concatString << mTableWidget->item( i + 1, col )->text();
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -1911,6 +1911,7 @@ void QgsOptions::on_pbnExportScales_clicked()
}

QStringList myScales;
myScales.reserve( mListGlobalScales->count() );
for ( int i = 0; i < mListGlobalScales->count(); ++i )
{
myScales.append( mListGlobalScales->item( i )->text() );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -686,6 +686,7 @@ void QgsProjectProperties::apply()

//save project scales
QStringList myScales;
myScales.reserve( lstScales->count() );
for ( int i = 0; i < lstScales->count(); ++i )
{
myScales.append( lstScales->item( i )->text() );
Expand Down
11 changes: 6 additions & 5 deletions src/core/auth/qgsauthcertutils.cpp
Expand Up @@ -49,7 +49,7 @@ QString QgsAuthCertUtils::getSslProtocolName( QSsl::SslProtocol protocol )
QMap<QString, QSslCertificate> QgsAuthCertUtils::mapDigestToCerts( QList<QSslCertificate> certs )
{
QMap<QString, QSslCertificate> digestmap;
Q_FOREACH ( QSslCertificate cert, certs )
Q_FOREACH ( const QSslCertificate& cert, certs )
{
digestmap.insert( shaHexForCert( cert ), cert );
}
Expand All @@ -59,7 +59,7 @@ QMap<QString, QSslCertificate> QgsAuthCertUtils::mapDigestToCerts( QList<QSslCer
QMap<QString, QList<QSslCertificate> > QgsAuthCertUtils::certsGroupedByOrg( QList<QSslCertificate> certs )
{
QMap< QString, QList<QSslCertificate> > orgcerts;
Q_FOREACH ( QSslCertificate cert, certs )
Q_FOREACH ( const QSslCertificate& cert, certs )
{
QString org( cert.subjectInfo( QSslCertificate::Organization ) );
if ( org.isEmpty() )
Expand All @@ -73,7 +73,7 @@ QMap<QString, QList<QSslCertificate> > QgsAuthCertUtils::certsGroupedByOrg( QLis
QMap<QString, QgsAuthConfigSslServer> QgsAuthCertUtils::mapDigestToSslConfigs( QList<QgsAuthConfigSslServer> configs )
{
QMap<QString, QgsAuthConfigSslServer> digestmap;
Q_FOREACH ( QgsAuthConfigSslServer config, configs )
Q_FOREACH ( const QgsAuthConfigSslServer& config, configs )
{
digestmap.insert( shaHexForCert( config.sslCertificate() ), config );
}
Expand All @@ -83,7 +83,7 @@ QMap<QString, QgsAuthConfigSslServer> QgsAuthCertUtils::mapDigestToSslConfigs( Q
QMap<QString, QList<QgsAuthConfigSslServer> > QgsAuthCertUtils::sslConfigsGroupedByOrg( QList<QgsAuthConfigSslServer> configs )
{
QMap< QString, QList<QgsAuthConfigSslServer> > orgconfigs;
Q_FOREACH ( QgsAuthConfigSslServer config, configs )
Q_FOREACH ( const QgsAuthConfigSslServer& config, configs )
{
QString org( config.sslCertificate().subjectInfo( QSslCertificate::Organization ) );
if ( org.isEmpty() )
Expand Down Expand Up @@ -371,6 +371,7 @@ QString QgsAuthCertUtils::getColonDelimited( const QString &txt )
// 64321c05b0ebab8e2b67ec0d7d9e2b6d4bc3c303
// -> 64:32:1c:05:b0:eb:ab:8e:2b:67:ec:0d:7d:9e:2b:6d:4b:c3:c3:03
QStringList sl;
sl.reserve( txt.size() );
for ( int i = 0; i < txt.size(); i += 2 )
{
sl << txt.mid( i, ( i + 2 > txt.size() ) ? -1 : 2 );
Expand Down Expand Up @@ -593,7 +594,7 @@ QList<QgsAuthCertUtils::CertUsageType> QgsAuthCertUtils::certificateUsageTypes(
}

QList<QCA::ConstraintType> certconsts = qcacert.constraints();
Q_FOREACH ( QCA::ConstraintType certconst, certconsts )
Q_FOREACH ( const QCA::ConstraintType& certconst, certconsts )
{
if ( certconst.known() == QCA::KeyCertificateSign )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/auth/qgsauthconfig.cpp
Expand Up @@ -285,7 +285,7 @@ const QgsPkiBundle QgsPkiBundle::fromPkcs12Paths( const QString &bundlepath,
if ( cert_chain.size() > 1 )
{
QList<QSslCertificate> ca_chain;
Q_FOREACH ( QCA::Certificate ca_cert, cert_chain )
Q_FOREACH ( const QCA::Certificate& ca_cert, cert_chain )
{
if ( ca_cert != cert_chain.primary() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/auth/qgsauthmanager.cpp
Expand Up @@ -2328,7 +2328,7 @@ const QList<QSslCertificate> QgsAuthManager::getExtraFileCAs()
filecerts = QgsAuthCertUtils::certsFromFile( cafile );
}
// only CAs or certs capable of signing other certs are allowed
Q_FOREACH ( QSslCertificate cert, filecerts )
Q_FOREACH ( const QSslCertificate& cert, filecerts )
{
if ( !allowinvalid.toBool() && !cert.isValid() )
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -235,10 +235,10 @@ QString QgsComposerLegend::wrapChar() const {return mSettings.wrapChar(); }
int QgsComposerLegend::columnCount() const { return mSettings.columnCount(); }
void QgsComposerLegend::setColumnCount( int c ) { mSettings.setColumnCount( c ); }

int QgsComposerLegend::splitLayer() const { return mSettings.splitLayer(); }
bool QgsComposerLegend::splitLayer() const { return mSettings.splitLayer(); }
void QgsComposerLegend::setSplitLayer( bool s ) { mSettings.setSplitLayer( s ); }

int QgsComposerLegend::equalColumnWidth() const { return mSettings.equalColumnWidth(); }
bool QgsComposerLegend::equalColumnWidth() const { return mSettings.equalColumnWidth(); }
void QgsComposerLegend::setEqualColumnWidth( bool s ) { mSettings.setEqualColumnWidth( s ); }


Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerlegend.h
Expand Up @@ -150,10 +150,10 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
int columnCount() const;
void setColumnCount( int c );

int splitLayer() const;
bool splitLayer() const;
void setSplitLayer( bool s );

int equalColumnWidth() const;
bool equalColumnWidth() const;
void setEqualColumnWidth( bool s );

void setComposerMap( const QgsComposerMap* map );
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -1425,6 +1425,7 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
{
QDomElement layerSetElem = layerSetNodeList.at( 0 ).toElement();
QDomNodeList layerIdNodeList = layerSetElem.elementsByTagName( "Layer" );
layerSet.reserve( layerIdNodeList.size() );
for ( int i = 0; i < layerIdNodeList.size(); ++i )
{
const QDomElement& layerIdElement = layerIdNodeList.at( i ).toElement();
Expand Down
2 changes: 1 addition & 1 deletion src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -4127,7 +4127,7 @@ QString QgsDxfExport::layerName( const QString &id, const QgsFeature &f ) const

QString QgsDxfExport::dxfEncoding( const QString &name )
{
Q_FOREACH ( QByteArray codec, QTextCodec::availableCodecs() )
Q_FOREACH ( const QByteArray& codec, QTextCodec::availableCodecs() )
{
if ( name != codec )
continue;
Expand Down
5 changes: 4 additions & 1 deletion src/core/geometry/qgscurvepolygonv2.cpp
Expand Up @@ -358,7 +358,9 @@ QgsPolygonV2* QgsCurvePolygonV2::surfaceToPolygon() const
QgsPolygonV2* polygon = new QgsPolygonV2();
polygon->setExteriorRing( exteriorRing()->curveToLine() );
QList<QgsCurveV2*> interiors;
for ( int i = 0, n = numInteriorRings(); i < n; ++i )
int n = numInteriorRings();
interiors.reserve( n );
for ( int i = 0; i < n; ++i )
{
interiors.append( interiorRing( i )->curveToLine() );
}
Expand Down Expand Up @@ -681,6 +683,7 @@ double QgsCurvePolygonV2::vertexAngle( const QgsVertexId& vertex ) const
{
if ( !mExteriorRing || vertex.ring < 0 || vertex.ring >= 1 + mInteriorRings.size() )
{
//makes no sense - conversion of false to double!
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscurvepolygonv2.h
Expand Up @@ -100,7 +100,7 @@ class CORE_EXPORT QgsCurvePolygonV2: public QgsSurfaceV2

virtual int vertexCount( int /*part*/ = 0, int ring = 0 ) const override;
virtual int ringCount( int /*part*/ = 0 ) const override { return ( mExteriorRing != 0 ) + mInteriorRings.size(); }
virtual int partCount() const override { return ringCount() > 0; }
virtual int partCount() const override { return ringCount() > 0 ? 1 : 0; }
virtual QgsPointV2 vertexAt( const QgsVertexId& id ) const override;

protected:
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgscurvev2.h
Expand Up @@ -92,8 +92,8 @@ class CORE_EXPORT QgsCurveV2: public QgsAbstractGeometryV2
QgsAbstractGeometryV2* segmentize() const override;

virtual int vertexCount( int /*part*/ = 0, int /*ring*/ = 0 ) const override { return numPoints(); }
virtual int ringCount( int /*part*/ = 0 ) const override { return numPoints() > 0; }
virtual int partCount() const override { return numPoints() > 0; }
virtual int ringCount( int /*part*/ = 0 ) const override { return numPoints() > 0 ? 1 : 0; }
virtual int partCount() const override { return numPoints() > 0 ? 1 : 0; }
virtual QgsPointV2 vertexAt( const QgsVertexId& id ) const override;
};

Expand Down
5 changes: 4 additions & 1 deletion src/core/geometry/qgsgeometry.cpp
Expand Up @@ -1415,6 +1415,7 @@ QList<QgsGeometry*> QgsGeometry::asGeometryCollection() const
if ( gc )
{
int numGeom = gc->numGeometries();
geometryList.reserve( numGeom );
for ( int i = 0; i < numGeom; ++i )
{
geometryList.append( new QgsGeometry( gc->geometryN( i )->clone() ) );
Expand Down Expand Up @@ -1683,7 +1684,7 @@ int QgsGeometry::vertexNrFromVertexId( const QgsVertexId& id ) const
{
if ( !d || !d->geometry )
{
return false;
return -1;
}

QList< QList< QList< QgsPointV2 > > > coords;
Expand Down Expand Up @@ -1911,6 +1912,7 @@ QgsPolyline QgsGeometry::smoothLine( const QgsPolyline& polyline, const unsigned
for ( unsigned int iteration = 0; iteration < iterations; ++iteration )
{
QgsPolyline outputLine = QgsPolyline();
outputLine.reserve( 2 * ( result.count() - 1 ) );
for ( int i = 0; i < result.count() - 1; i++ )
{
const QgsPoint& p1 = result.at( i );
Expand All @@ -1933,6 +1935,7 @@ QgsPolygon QgsGeometry::smoothPolygon( const QgsPolygon& polygon, const unsigned
for ( unsigned int iteration = 0; iteration < iterations; ++iteration )
{
QgsPolyline outputRing = QgsPolyline();
outputRing.reserve( 2 * ( resultRing.count() - 1 ) + 1 );
for ( int i = 0; i < resultRing.count() - 1; ++i )
{
const QgsPoint& p1 = resultRing.at( i );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgsgeometry.h
Expand Up @@ -575,7 +575,7 @@ class CORE_EXPORT QgsGeometry
public:
Error() : message( "none" ), hasLocation( false ) {}
explicit Error( QString m ) : message( m ), hasLocation( false ) {}
Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
Error( QString m, const QgsPoint& p ) : message( m ), location( p ), hasLocation( true ) {}

QString what() { return message; }
QgsPoint where() { return location; }
Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgsgeometryeditutils.cpp
Expand Up @@ -42,6 +42,7 @@ int QgsGeometryEditUtils::addRing( QgsAbstractGeometryV2* geom, QgsCurveV2* ring
}
else if ( multiGeom )
{
polygonList.reserve( multiGeom->numGeometries() );
for ( int i = 0; i < multiGeom->numGeometries(); ++i )
{
polygonList.append( dynamic_cast< QgsCurvePolygonV2* >( multiGeom->geometryN( i ) ) );
Expand Down
2 changes: 2 additions & 0 deletions src/core/geometry/qgsgeos.cpp
Expand Up @@ -561,6 +561,7 @@ int QgsGeos::splitLinearGeometry( GEOSGeometry* splitLine, QList<QgsAbstractGeom
if ( splitType == GEOS_MULTILINESTRING )
{
int nGeoms = GEOSGetNumGeometries_r( geosinit.ctxt, splitGeom );
lineGeoms.reserve( nGeoms );
for ( int i = 0; i < nGeoms; ++i )
lineGeoms << GEOSGeom_clone_r( geosinit.ctxt, GEOSGetGeometryN_r( geosinit.ctxt, splitGeom, i ) );

Expand Down Expand Up @@ -930,6 +931,7 @@ QgsLineStringV2* QgsGeos::sequenceToLinestring( const GEOSGeometry* geos, bool h
const GEOSCoordSequence* cs = GEOSGeom_getCoordSeq_r( geosinit.ctxt, geos );
unsigned int nPoints;
GEOSCoordSeq_getSize_r( geosinit.ctxt, cs, &nPoints );
pts.reserve( nPoints );
for ( unsigned int i = 0; i < nPoints; ++i )
{
pts.push_back( coordSeqPoint( cs, i, hasZ, hasM ) );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgspolygonv2.cpp
Expand Up @@ -107,5 +107,5 @@ unsigned char* QgsPolygonV2::asWkb( int& binarySize ) const

QgsPolygonV2* QgsPolygonV2::surfaceToPolygon() const
{
return dynamic_cast<QgsPolygonV2*>( clone() );
return clone();
}

0 comments on commit 0caf944

Please sign in to comment.