Skip to content

Commit be3b7a2

Browse files
JohnCoconutminggo
authored andcommitted
add new clang-tidy option (#19699)
* readability-redundant-string-cstr
1 parent d5e0213 commit be3b7a2

File tree

22 files changed

+50
-49
lines changed

22 files changed

+50
-49
lines changed

.clang-tidy

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Checks: >
1111
readability-container-size-empty,
1212
readability-delete-null-pointer,
1313
readability-redundant-control-flow,
14+
readability-redundant-string-cstr,
1415
1516
WarningsAsErrors: '*'
1617
HeaderFilterRegex: '/(?!external)/.*'

cocos/audio/linux/SimpleAudioEngine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void SimpleAudioEngine::stopBackgroundMusic(bool releaseData)
8484
{
8585
AudioEngine::stop(g_SimpleAudioEngineLinux->musicid);
8686
if (releaseData) {
87-
AudioEngine::uncache(g_SimpleAudioEngineLinux->musicpath.c_str());
87+
AudioEngine::uncache(g_SimpleAudioEngineLinux->musicpath);
8888
}
8989
}
9090

cocos/base/CCDataVisitor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void PrettyPrinter::visit(const __Set *p)
203203
if (i > 0) {
204204
_result += "\n";
205205
}
206-
_result += _indentStr.c_str();
206+
_result += _indentStr;
207207
PrettyPrinter v(_indentLevel);
208208
//FIXME:james (*it)->acceptVisitor(v);
209209
_result += v.getResult();

cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ Offset<NodeAction> FlatBuffersSerialize::createNodeAction(const tinyxml2::XMLEle
577577
}
578578
else if (name == "ActivedAnimationName")
579579
{
580-
currentAnimationName = value.c_str();
580+
currentAnimationName = value;
581581
}
582582

583583
attribute = attribute->Next();

cocos/network/SocketIO.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ void SIOClientImpl::send(SocketIOPacket *packet)
720720
if (_connected)
721721
{
722722
CCLOGINFO("-->SEND:%s", req.data());
723-
_ws->send(req.data());
723+
_ws->send(req);
724724
}
725725
else
726726
CCLOGINFO("Cant send the message (%s) because disconnected", req.c_str());
@@ -757,7 +757,7 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
757757
if (_version == SocketIOPacket::SocketIOVersion::V10x)
758758
{
759759
std::string s = "5";//That's a ping https://github.com/Automattic/engine.io-parser/blob/1b8e077b2218f4947a69f5ad18be2a512ed54e93/lib/index.js#L21
760-
_ws->send(s.data());
760+
_ws->send(s);
761761
}
762762

763763
Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(SIOClientImpl::heartbeat), this, (_heartbeat * .9f), false);

cocos/platform/linux/CCDevice-linux.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ class BitmapDC
349349
std::string lowerCasePath = fontPath;
350350
std::transform(lowerCasePath.begin(), lowerCasePath.end(), lowerCasePath.begin(), ::tolower);
351351
if ( lowerCasePath.find(".ttf") != std::string::npos ) {
352-
fontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fontPath.c_str());
352+
fontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fontPath);
353353

354354
FILE *f = fopen(fontPath.c_str(), "r");
355355
if ( f ) {

cocos/scripting/js-bindings/manual/3d/jsb_cocos2dx_3d_manual.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ bool jsval_to_TerrainData(JSContext* cx, JS::HandleValue v, Terrain::TerrainData
239239
JS::ToNumber(cx, js_mapHeight, &mapHeight);
240240
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
241241

242-
ret->_heightMapSrc = heightMap.c_str();
242+
ret->_heightMapSrc = heightMap;
243243
char* tmp = (char*)malloc(sizeof(char) * (alphaMap.size() + 1));
244244
strcpy(tmp, alphaMap.c_str());
245245
tmp[alphaMap.size()] = '\0';

cocos/scripting/lua-bindings/manual/CCLuaStack.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L)
801801
std::string filename = zip->getFirstFilename();
802802
while (filename.length()) {
803803
ssize_t bufferSize = 0;
804-
unsigned char *zbuffer = zip->getFileData(filename.c_str(), &bufferSize);
804+
unsigned char *zbuffer = zip->getFileData(filename, &bufferSize);
805805
if (bufferSize) {
806806
// remove .lua or .luac extension
807807
size_t pos = filename.find_last_of('.');

cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3779,7 +3779,7 @@ static int tolua_cocos2dx_FileUtils_getStringFromFile(lua_State* tolua_S)
37793779
if (ok)
37803780
{
37813781
std::string fullPathName = FileUtils::getInstance()->fullPathForFilename(arg0);
3782-
__String* contentsOfFile = __String::createWithContentsOfFile(fullPathName.c_str());
3782+
__String* contentsOfFile = __String::createWithContentsOfFile(fullPathName);
37833783
if (nullptr != contentsOfFile)
37843784
{
37853785
const char* tolua_ret = contentsOfFile->getCString();

cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_physics_manual.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
135135
auto iter = g_luaType.find(hashName);
136136
std::string className = "";
137137
if(iter != g_luaType.end()){
138-
className = iter->second.c_str();
138+
className = iter->second;
139139
} else {
140140
className = "cc.Scene";
141141
}

extensions/assets-manager/AssetsManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void AssetsManager::downloadAndUncompress()
226226
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this] {
227227

228228
// Record new version code.
229-
UserDefault::getInstance()->setStringForKey(this->keyOfVersion().c_str(), this->_version.c_str());
229+
UserDefault::getInstance()->setStringForKey(this->keyOfVersion().c_str(), this->_version);
230230

231231
// Unrecord downloaded version code.
232232
UserDefault::getInstance()->setStringForKey(this->keyOfDownloadedVersion().c_str(), "");

tests/cpp-tests/Classes/Box2DTestBed/Box2dView.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool Box2dTestBed::initWithEntryID(int entryId)
9292
view->setScale(15);
9393
view->setAnchorPoint( Vec2(0,0) );
9494
view->setPosition(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/3);
95-
auto label = Label::createWithTTF(view->title().c_str(), "fonts/arial.ttf", 28);
95+
auto label = Label::createWithTTF(view->title(), "fonts/arial.ttf", 28);
9696
addChild(label, 1);
9797
label->setPosition(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height-50);
9898

tests/cpp-tests/Classes/DataVisitorTest/DataVisitorTest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ void PrettyPrinterDemo::onEnter()
6969
TestCase::onEnter();
7070
auto s = Director::getInstance()->getWinSize();
7171

72-
auto label = Label::createWithTTF(title().c_str(), "fonts/arial.ttf", 28);
72+
auto label = Label::createWithTTF(title(), "fonts/arial.ttf", 28);
7373
label->setPosition(s.width/2, s.height * 4/5);
7474
this->addChild(label, 1);
7575

7676
std::string strSubtitle = subtitle();
7777
if(strSubtitle.empty() == false)
7878
{
79-
auto subLabel = Label::createWithTTF(strSubtitle.c_str(), "fonts/Thonburi.ttf", 16);
79+
auto subLabel = Label::createWithTTF(strSubtitle, "fonts/Thonburi.ttf", 16);
8080
subLabel->setPosition(s.width/2, s.height * 3/5);
8181
this->addChild(subLabel, 1);
8282
}

tests/cpp-tests/Classes/ExtensionsTest/NetworkTest/SocketIOTest.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void SocketIOTest::testevent(SIOClient *client, const std::string& data) {
120120
std::stringstream s;
121121
s << client->getTag() << " received event testevent with data: " << data.c_str();
122122

123-
_sioClientStatus->setString(s.str().c_str());
123+
_sioClientStatus->setString(s.str());
124124

125125
}
126126

@@ -131,7 +131,7 @@ void SocketIOTest::echotest(SIOClient *client, const std::string& data) {
131131
std::stringstream s;
132132
s << client->getTag() << " received event echotest with data: " << data.c_str();
133133

134-
_sioClientStatus->setString(s.str().c_str());
134+
_sioClientStatus->setString(s.str());
135135

136136
}
137137

@@ -143,7 +143,7 @@ void SocketIOTest::message(network::SIOClient* client, const std::string& data)
143143

144144
std::stringstream s;
145145
s << client->getTag() << " received message with content: " << data.c_str();
146-
_sioClientStatus->setString(s.str().c_str());
146+
_sioClientStatus->setString(s.str());
147147

148148
}
149149

@@ -153,7 +153,7 @@ void SocketIOTest::json(network::SIOClient* client, const std::string& data)
153153

154154
std::stringstream s;
155155
s << client->getTag() << " received json message with content: " << data.c_str();
156-
_sioClientStatus->setString(s.str().c_str());
156+
_sioClientStatus->setString(s.str());
157157

158158
}
159159

@@ -163,7 +163,7 @@ void SocketIOTest::connect(network::SIOClient* client, const std::string& data)
163163

164164
std::stringstream s;
165165
s << client->getTag() << " connected!";
166-
_sioClientStatus->setString(s.str().c_str());
166+
_sioClientStatus->setString(s.str());
167167

168168
}
169169

@@ -173,7 +173,7 @@ void SocketIOTest::disconnect(network::SIOClient* client, const std::string& dat
173173

174174
std::stringstream s;
175175
s << client->getTag() << " disconnected by server!";
176-
_sioClientStatus->setString(s.str().c_str());
176+
_sioClientStatus->setString(s.str());
177177

178178
this->closedSocketAction(client);
179179

@@ -271,7 +271,7 @@ void SocketIOTest::onMenuTestClientDisconnectClicked(cocos2d::Ref *sender)
271271
s << "Socket.io Test Client not initialized!";
272272
}
273273

274-
_sioClientStatus->setString(s.str().c_str());
274+
_sioClientStatus->setString(s.str());
275275

276276
}
277277

@@ -288,7 +288,7 @@ void SocketIOTest::onMenuTestEndpointDisconnectClicked(cocos2d::Ref *sender)
288288
s << "Socket.io Test Endpoint not initialized!";
289289
}
290290

291-
_sioClientStatus->setString(s.str().c_str());
291+
_sioClientStatus->setString(s.str());
292292

293293
}
294294

@@ -300,7 +300,7 @@ void SocketIOTest::onClose(network::SIOClient* client)
300300

301301
std::stringstream s;
302302
s << client->getTag() << " closed!";
303-
_sioClientStatus->setString(s.str().c_str());
303+
_sioClientStatus->setString(s.str());
304304

305305
this->closedSocketAction(client);
306306

@@ -312,5 +312,5 @@ void SocketIOTest::onError(network::SIOClient* client, const std::string& data)
312312

313313
std::stringstream s;
314314
s << client->getTag() << " received error with content: " << data.c_str();
315-
_sioClientStatus->setString(s.str().c_str());
315+
_sioClientStatus->setString(s.str());
316316
}

tests/cpp-tests/Classes/ExtensionsTest/NetworkTest/WebSocketDelayTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,6 @@ void WebSocketDelayTest::onMenuSendTextClicked(cocos2d::Ref *sender)
213213
{
214214
std::string warningStr = "send text websocket instance wasn't ready...";
215215
log("%s", warningStr.c_str());
216-
_sendTextStatus->setString(warningStr.c_str());
216+
_sendTextStatus->setString(warningStr);
217217
}
218218
}

tests/cpp-tests/Classes/ExtensionsTest/NetworkTest/WebSocketTest.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void WebSocketTest::onMessage(network::WebSocket* ws, const network::WebSocket::
197197
std::string textStr = std::string("response text msg: ")+data.bytes+", "+times;
198198
log("%s", textStr.c_str());
199199

200-
_sendTextStatus->setString(textStr.c_str());
200+
_sendTextStatus->setString(textStr);
201201
}
202202
else
203203
{
@@ -220,7 +220,7 @@ void WebSocketTest::onMessage(network::WebSocket* ws, const network::WebSocket::
220220

221221
binaryStr += std::string(", ")+times;
222222
log("%s", binaryStr.c_str());
223-
_sendBinaryStatus->setString(binaryStr.c_str());
223+
_sendBinaryStatus->setString(binaryStr);
224224
}
225225
}
226226

@@ -285,7 +285,7 @@ void WebSocketTest::onMenuSendTextClicked(cocos2d::Ref *sender)
285285
{
286286
std::string warningStr = "send text websocket instance wasn't ready...";
287287
log("%s", warningStr.c_str());
288-
_sendTextStatus->setString(warningStr.c_str());
288+
_sendTextStatus->setString(warningStr);
289289
}
290290
}
291291

@@ -307,7 +307,7 @@ void WebSocketTest::onMenuSendMultipleTextClicked(cocos2d::Ref *sender)
307307
{
308308
std::string warningStr = "send text websocket instance wasn't ready...";
309309
log("%s", warningStr.c_str());
310-
_sendTextStatus->setString(warningStr.c_str());
310+
_sendTextStatus->setString(warningStr);
311311
}
312312
}
313313

@@ -327,7 +327,7 @@ void WebSocketTest::onMenuSendBinaryClicked(cocos2d::Ref *sender)
327327
{
328328
std::string warningStr = "send binary websocket instance wasn't ready...";
329329
log("%s", warningStr.c_str());
330-
_sendBinaryStatus->setString(warningStr.c_str());
330+
_sendBinaryStatus->setString(warningStr);
331331
}
332332
}
333333

tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ void TextWritePlist::onEnter()
563563
else
564564
log("write plist file failed");
565565

566-
auto label = Label::createWithTTF(fullPath.c_str(), "fonts/Thonburi.ttf", 6);
566+
auto label = Label::createWithTTF(fullPath, "fonts/Thonburi.ttf", 6);
567567
this->addChild(label);
568568
auto winSize = Director::getInstance()->getWinSize();
569569
label->setPosition(winSize.width/2, winSize.height/3);
@@ -593,7 +593,7 @@ std::string TextWritePlist::title() const
593593

594594
std::string TextWritePlist::subtitle() const
595595
{
596-
std::string writablePath = FileUtils::getInstance()->getWritablePath().c_str();
596+
std::string writablePath = FileUtils::getInstance()->getWritablePath();
597597
return ("See plist file at your writablePath");
598598
}
599599

@@ -617,7 +617,7 @@ void TestWriteString::onEnter()
617617
// writeTest
618618
std::string writeDataStr = "the string data will be write into a file";
619619
std::string fullPath = writablePath + fileName;
620-
if (FileUtils::getInstance()->writeStringToFile(writeDataStr, fullPath.c_str()))
620+
if (FileUtils::getInstance()->writeStringToFile(writeDataStr, fullPath))
621621
{
622622
log("see the plist file at %s", fullPath.c_str());
623623
writeResult->setString("write success:" + writeDataStr);
@@ -793,7 +793,7 @@ void TestWriteData::onEnter()
793793
Data writeData;
794794
writeData.copy((unsigned char *)writeDataStr.c_str(), writeDataStr.size());
795795
std::string fullPath = writablePath + fileName;
796-
if (FileUtils::getInstance()->writeDataToFile(writeData, fullPath.c_str()))
796+
if (FileUtils::getInstance()->writeDataToFile(writeData, fullPath))
797797
{
798798
log("see the plist file at %s", fullPath.c_str());
799799
writeResult->setString("write success:" + writeDataStr);
@@ -881,7 +881,7 @@ void TestWriteValueMap::onEnter()
881881
// end with /
882882
std::string writablePath = FileUtils::getInstance()->getWritablePath();
883883
std::string fullPath = writablePath + "testWriteValueMap.plist";
884-
if (FileUtils::getInstance()->writeValueMapToFile(valueMap, fullPath.c_str()))
884+
if (FileUtils::getInstance()->writeValueMapToFile(valueMap, fullPath))
885885
{
886886
log("see the plist file at %s", fullPath.c_str());
887887
writeResult->setString("write success");
@@ -892,7 +892,7 @@ void TestWriteValueMap::onEnter()
892892
writeResult->setString("write failed");
893893
}
894894

895-
ValueMap readValueMap = FileUtils::getInstance()->getValueMapFromFile(fullPath.c_str());
895+
ValueMap readValueMap = FileUtils::getInstance()->getValueMapFromFile(fullPath);
896896
std::string readDataStr = "read data:\n";
897897
// read value map data
898898
ValueMap readMapInMap = readValueMap["data0"].asValueMap();
@@ -984,7 +984,7 @@ void TestWriteValueVector::onEnter()
984984
// end with /
985985
std::string writablePath = FileUtils::getInstance()->getWritablePath();
986986
std::string fullPath = writablePath + "testWriteValueVector.plist";
987-
if (FileUtils::getInstance()->writeValueVectorToFile(array, fullPath.c_str()))
987+
if (FileUtils::getInstance()->writeValueVectorToFile(array, fullPath))
988988
{
989989
log("see the plist file at %s", fullPath.c_str());
990990
writeResult->setString("write success");
@@ -995,7 +995,7 @@ void TestWriteValueVector::onEnter()
995995
writeResult->setString("write failed");
996996
}
997997

998-
ValueVector readArray = FileUtils::getInstance()->getValueVectorFromFile(fullPath.c_str());
998+
ValueVector readArray = FileUtils::getInstance()->getValueVectorFromFile(fullPath);
999999
std::string readDataStr = "read data:\n";
10001000
// read value map data
10011001
ValueMap readMapInArray = readArray.at(0).asValueMap();

tests/cpp-tests/Classes/InputTest/MouseTest.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,31 @@ void MouseEventTest::onMouseDown(Event *event)
7474
EventMouse* e = (EventMouse*)event;
7575
std::string str = "Mouse Down detected, Key: ";
7676
str += tostr(static_cast<int>(e->getMouseButton()));
77-
_labelAction->setString(str.c_str());
77+
_labelAction->setString(str);
7878
}
7979

8080
void MouseEventTest::onMouseUp(Event *event)
8181
{
8282
EventMouse* e = (EventMouse*)event;
8383
std::string str = "Mouse Up detected, Key: ";
8484
str += tostr(static_cast<int>(e->getMouseButton()));
85-
_labelAction->setString(str.c_str());
85+
_labelAction->setString(str);
8686
}
8787

8888
void MouseEventTest::onMouseMove(Event *event)
8989
{
9090
EventMouse* e = (EventMouse*)event;
9191
std::string str = "MousePosition X:";
9292
str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
93-
_labelPosition->setString(str.c_str());
93+
_labelPosition->setString(str);
9494
}
9595

9696
void MouseEventTest::onMouseScroll(Event *event)
9797
{
9898
EventMouse* e = (EventMouse*)event;
9999
std::string str = "Mouse Scroll detected, X: ";
100100
str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
101-
_labelAction->setString(str.c_str());
101+
_labelAction->setString(str);
102102
}
103103

104104
std::string MouseEventTest::title() const

tests/cpp-tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void CustomEventTest::onEnter()
392392
char* buf = static_cast<char*>(event->getUserData());
393393
str += buf;
394394
str += " times";
395-
statusLabel->setString(str.c_str());
395+
statusLabel->setString(str);
396396
});
397397

398398
_eventDispatcher->addEventListenerWithFixedPriority(_listener, 1);
@@ -418,7 +418,7 @@ void CustomEventTest::onEnter()
418418
char* buf = static_cast<char*>(event->getUserData());
419419
str += buf;
420420
str += " times";
421-
statusLabel2->setString(str.c_str());
421+
statusLabel2->setString(str);
422422
});
423423

424424
_eventDispatcher->addEventListenerWithFixedPriority(_listener2, 1);

0 commit comments

Comments
 (0)