From 3fb2041ee866fa2299f34c225069d600d11d1422 Mon Sep 17 00:00:00 2001 From: John Zhang Date: Wed, 8 May 2019 15:56:17 +0800 Subject: [PATCH 1/2] add new clang-tidy option * readability-delete-null-pointer --- .clang-tidy | 1 + extensions/Particle3D/PU/CCPUEmitter.cpp | 66 +++++++-------------- extensions/Particle3D/PU/CCPUForceField.cpp | 10 +--- 3 files changed, 25 insertions(+), 52 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c50417b3abfd..89efd35edeb9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -9,6 +9,7 @@ Checks: > performance-move-const-arg, performance-type-promotion-in-math-fn, readability-container-size-empty, + readability-delete-null-pointer, WarningsAsErrors: '*' HeaderFilterRegex: '/(?!external)/.*' diff --git a/extensions/Particle3D/PU/CCPUEmitter.cpp b/extensions/Particle3D/PU/CCPUEmitter.cpp index 3d5c998ccdd4..1befea3ac8a5 100755 --- a/extensions/Particle3D/PU/CCPUEmitter.cpp +++ b/extensions/Particle3D/PU/CCPUEmitter.cpp @@ -131,41 +131,30 @@ PUEmitter::~PUEmitter() { _particleSystem = nullptr; - if (_dynEmissionRate) - delete _dynEmissionRate; + delete _dynEmissionRate; - if (_dynTotalTimeToLive) - delete _dynTotalTimeToLive; + delete _dynTotalTimeToLive; - if (_dynParticleMass) - delete _dynParticleMass; + delete _dynParticleMass; - if (_dynVelocity) - delete _dynVelocity; + delete _dynVelocity; - if (_dynDuration) - delete _dynDuration; + delete _dynDuration; - if (_dynRepeatDelay) - delete _dynRepeatDelay; + delete _dynRepeatDelay; - if (_dynParticleAllDimensions) - delete _dynParticleAllDimensions; + delete _dynParticleAllDimensions; - if (_dynParticleWidth) - delete _dynParticleWidth; + delete _dynParticleWidth; - if (_dynParticleHeight) - delete _dynParticleHeight; + delete _dynParticleHeight; - if (_dynParticleDepth) - delete _dynParticleDepth; + delete _dynParticleDepth; //if (mEmissionRateCameraDependency) // mCameraDependencyFactory.destroy(mEmissionRateCameraDependency); - if (_dynAngle) - delete _dynAngle; + delete _dynAngle; } void PUEmitter::updateEmitter(Particle3D* /*particle*/, float /*deltaTime*/) @@ -516,48 +505,42 @@ void PUEmitter::setEmitsName(const std::string& emitsName) //----------------------------------------------------------------------- void PUEmitter::setDynEmissionRate(PUDynamicAttribute* dynEmissionRate) { - if (_dynEmissionRate) - delete _dynEmissionRate; + delete _dynEmissionRate; _dynEmissionRate = dynEmissionRate; } //----------------------------------------------------------------------- void PUEmitter::setDynTotalTimeToLive(PUDynamicAttribute* dynTotalTimeToLive) { - if (_dynTotalTimeToLive) - delete _dynTotalTimeToLive; + delete _dynTotalTimeToLive; _dynTotalTimeToLive = dynTotalTimeToLive; } //----------------------------------------------------------------------- void PUEmitter::setDynParticleMass(PUDynamicAttribute* dynParticleMass) { - if (_dynParticleMass) - delete _dynParticleMass; + delete _dynParticleMass; _dynParticleMass = dynParticleMass; } //----------------------------------------------------------------------- void PUEmitter::setDynAngle(PUDynamicAttribute* dynAngle) { - if (_dynAngle) - delete _dynAngle; + delete _dynAngle; _dynAngle = dynAngle; } //----------------------------------------------------------------------- void PUEmitter::setDynVelocity(PUDynamicAttribute* dynVelocity) { - if (_dynVelocity) - delete _dynVelocity; + delete _dynVelocity; _dynVelocity = dynVelocity; } //----------------------------------------------------------------------- void PUEmitter::setDynDuration(PUDynamicAttribute* dynDuration) { - if (_dynDuration) - delete _dynDuration; + delete _dynDuration; _dynDuration = dynDuration; _dynDurationSet = true; @@ -571,8 +554,7 @@ void PUEmitter::setDynDurationSet(bool durationSet) //----------------------------------------------------------------------- void PUEmitter::setDynRepeatDelay(PUDynamicAttribute* dynRepeatDelay) { - if (_dynRepeatDelay) - delete _dynRepeatDelay; + delete _dynRepeatDelay; _dynRepeatDelay = dynRepeatDelay; _dynRepeatDelaySet = true; @@ -586,8 +568,7 @@ void PUEmitter::setDynRepeatDelaySet(bool repeatDelaySet) //----------------------------------------------------------------------- void PUEmitter::setDynParticleAllDimensions(PUDynamicAttribute* dynParticleAllDimensions) { - if (_dynParticleAllDimensions) - delete _dynParticleAllDimensions; + delete _dynParticleAllDimensions; _dynParticleAllDimensions = dynParticleAllDimensions; _dynParticleAllDimensionsSet = true; @@ -600,8 +581,7 @@ void PUEmitter::setDynParticleAllDimensionsSet(bool particleAllDimensionsSet) //----------------------------------------------------------------------- void PUEmitter::setDynParticleWidth(PUDynamicAttribute* dynParticleWidth) { - if (_dynParticleWidth) - delete _dynParticleWidth; + delete _dynParticleWidth; _dynParticleWidth = dynParticleWidth; _dynParticleWidthSet = true; @@ -614,8 +594,7 @@ void PUEmitter::setDynParticleWidthSet(bool particleWidthSet) //----------------------------------------------------------------------- void PUEmitter::setDynParticleHeight(PUDynamicAttribute* dynParticleHeight) { - if (_dynParticleHeight) - delete _dynParticleHeight; + delete _dynParticleHeight; _dynParticleHeight = dynParticleHeight; _dynParticleHeightSet = true; @@ -628,8 +607,7 @@ void PUEmitter::setDynParticleHeightSet(bool particleHeightSet) //----------------------------------------------------------------------- void PUEmitter::setDynParticleDepth(PUDynamicAttribute* dynParticleDepth) { - if (_dynParticleDepth) - delete _dynParticleDepth; + delete _dynParticleDepth; _dynParticleDepth = dynParticleDepth; _dynParticleDepthSet = true; diff --git a/extensions/Particle3D/PU/CCPUForceField.cpp b/extensions/Particle3D/PU/CCPUForceField.cpp index 590733bbb5fa..05c8ae4227f5 100644 --- a/extensions/Particle3D/PU/CCPUForceField.cpp +++ b/extensions/Particle3D/PU/CCPUForceField.cpp @@ -166,10 +166,7 @@ PUForceField::PUForceField(void) : //----------------------------------------------------------------------- PUForceField::~PUForceField(void) { - if (_forceFieldCalculationFactory) - { - delete _forceFieldCalculationFactory; - } + delete _forceFieldCalculationFactory; } //----------------------------------------------------------------------- void PUForceField::initialise(ForceFieldType type, @@ -236,10 +233,7 @@ PUForceFieldCalculationFactory* PUForceField::getForceFieldCalculationFactory() //----------------------------------------------------------------------- void PUForceField::setForceFieldCalculationFactory(PUForceFieldCalculationFactory* forceFieldCalculationFactory) { - if (_forceFieldCalculationFactory) - { - delete _forceFieldCalculationFactory; - } + delete _forceFieldCalculationFactory; _forceFieldCalculationFactory = forceFieldCalculationFactory; } //----------------------------------------------------------------------- From ff4e727e746e97aeb71dee3efbcf109b0207327d Mon Sep 17 00:00:00 2001 From: John Zhang Date: Wed, 8 May 2019 23:27:21 +0800 Subject: [PATCH 2/2] more fix for readability-delete-null-pointer --- cocos/2d/CCFontAtlas.cpp | 3 +-- cocos/3d/CCTerrain.cpp | 8 ++++---- cocos/base/CCDirector.cpp | 3 +-- cocos/base/CCUserDefault.cpp | 16 ++++++---------- cocos/base/ZipUtils.cpp | 2 +- cocos/platform/CCFileUtils.cpp | 3 +-- .../js-bindings/manual/cocos2d_specifics.cpp | 6 ++---- .../manual/component/CCComponentJS.cpp | 5 +---- 8 files changed, 17 insertions(+), 29 deletions(-) diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index 7253947e5488..00286da8e199 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -433,8 +433,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text) tempDef.V = tempDef.V / scaleFactor; } else{ - if(bitmap) - delete[] bitmap; + delete[] bitmap; if (tempDef.xAdvance) tempDef.validDefinition = true; else diff --git a/cocos/3d/CCTerrain.cpp b/cocos/3d/CCTerrain.cpp index 19335b2acc8d..decffa720a24 100644 --- a/cocos/3d/CCTerrain.cpp +++ b/cocos/3d/CCTerrain.cpp @@ -1583,10 +1583,10 @@ void Terrain::QuadTree::preCalculateAABB(const Mat4 & worldTransform) Terrain::QuadTree::~QuadTree() { - if(_tl) delete _tl; - if(_tr) delete _tr; - if(_bl) delete _bl; - if(_br) delete _br; + delete _tl; + delete _tr; + delete _bl; + delete _br; } Terrain::TerrainData::TerrainData(const std::string& heightMapsrc , const std::string& textureSrc, const Size & chunksize, float height, float scale) diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 7bb44f5943e3..ad32de7129b5 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -1343,8 +1343,7 @@ void Director::createStatsLabel() Image* image = new (std::nothrow) Image(); bool isOK = image ? image->initWithImageData(data, dataLength) : false; if (! isOK) { - if(image) - delete image; + delete image; CCLOGERROR("%s", "Fails: init fps_images"); return; } diff --git a/cocos/base/CCUserDefault.cpp b/cocos/base/CCUserDefault.cpp index 37467017beab..b1cc83235bb7 100644 --- a/cocos/base/CCUserDefault.cpp +++ b/cocos/base/CCUserDefault.cpp @@ -195,7 +195,7 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue) ret = (! strcmp(value, "true")); } - if (doc) delete doc; + delete doc; return ret; } @@ -225,10 +225,7 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) ret = atoi(value); } - if(doc) - { - delete doc; - } + delete doc; return ret; @@ -271,7 +268,7 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue) ret = utils::atof(value); } - if (doc) delete doc; + delete doc; return ret; } @@ -301,7 +298,7 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul ret = string(value); } - if (doc) delete doc; + delete doc; return ret; } @@ -336,7 +333,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) } } - if (doc) delete doc; + delete doc; return ret; } @@ -447,8 +444,7 @@ void UserDefault::destroyInstance() void UserDefault::setDelegate(UserDefault *delegate) { - if (_userDefault) - delete _userDefault; + delete _userDefault; _userDefault = delegate; } diff --git a/cocos/base/ZipUtils.cpp b/cocos/base/ZipUtils.cpp index 7ee1d647d7eb..101cc8090f90 100644 --- a/cocos/base/ZipUtils.cpp +++ b/cocos/base/ZipUtils.cpp @@ -523,7 +523,7 @@ ZipFile *ZipFile::createWithBuffer(const void* buffer, uLong size) if (zip && zip->initWithBuffer(buffer, size)) { return zip; } else { - if (zip) delete zip; + delete zip; return nullptr; } } diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp index 3adc71c5ff30..e4cecfe52175 100644 --- a/cocos/platform/CCFileUtils.cpp +++ b/cocos/platform/CCFileUtils.cpp @@ -547,8 +547,7 @@ void FileUtils::destroyInstance() void FileUtils::setDelegate(FileUtils *delegate) { - if (s_sharedFileUtils) - delete s_sharedFileUtils; + delete s_sharedFileUtils; s_sharedFileUtils = delegate; } diff --git a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp index 979314ed1b0e..f66698c7eff6 100644 --- a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp +++ b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp @@ -5608,8 +5608,7 @@ void js_cocos2d_PolygonInfo_finalize(JSFreeOp *fop, JSObject *obj) { if (proxy) { cocos2d::PolygonInfo *nobj = static_cast(proxy->ptr); - if (nobj) - delete nobj; + delete nobj; jsb_remove_proxy(proxy); } } @@ -5766,8 +5765,7 @@ void js_cocos2d_AutoPolygon_finalize(JSFreeOp *fop, JSObject *obj) { auto proxy = jsb_get_js_proxy(jsobj); if (proxy) { cocos2d::AutoPolygon *nobj = static_cast(proxy->ptr); - if (nobj) - delete nobj; + delete nobj; jsb_remove_proxy(proxy); } } diff --git a/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp b/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp index 60a5a36be224..e48147af1d0a 100644 --- a/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp +++ b/cocos/scripting/js-bindings/manual/component/CCComponentJS.cpp @@ -104,10 +104,7 @@ ComponentJS::~ComponentJS() jsb_remove_proxy(proxy); } // Delete rooted object - if (jsObj != nullptr) - { - delete jsObj; - } + delete jsObj; } void* ComponentJS::getScriptObject() const