From f3fedae7c851a09be46335ade50d8af9a39a9931 Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Fri, 18 Nov 2022 17:12:46 -0800 Subject: [PATCH] Added readability-redundant-string-cstr (#31707) * Added readability-redundant-string-cstr * Auto fix * No need to wrap std::string --- .clang-tidy | 1 + src/core/lib/json/json_writer.cc | 2 +- src/core/lib/surface/completion_queue.cc | 2 +- src/cpp/server/external_connection_acceptor_impl.cc | 2 +- .../tsi/alts/handshaker/alts_concurrent_connectivity_test.cc | 3 +-- test/cpp/end2end/rls_end2end_test.cc | 4 ++-- test/cpp/end2end/xds/xds_end2end_test_lib.cc | 2 +- test/cpp/interop/interop_client.cc | 4 ++-- test/cpp/qps/qps_json_driver.cc | 4 ++-- test/cpp/qps/qps_worker.cc | 2 +- test/cpp/qps/server_async.cc | 5 ++--- test/cpp/qps/server_callback.cc | 5 ++--- test/cpp/qps/server_sync.cc | 5 ++--- test/cpp/util/grpc_tool.cc | 4 ++-- 14 files changed, 21 insertions(+), 24 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 94de6eb495b..03f4fcd290f 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -120,6 +120,7 @@ Checks: '-*, readability-redundant-control-flow, readability-redundant-function-ptr-dereference, readability-redundant-smartptr-get, + readability-redundant-string-cstr, readability-redundant-string-init, readability-simplify-boolean-expr, readability-static-definition-in-anonymous-namespace, diff --git a/src/core/lib/json/json_writer.cc b/src/core/lib/json/json_writer.cc index ef85eac6ecd..2964f88172a 100644 --- a/src/core/lib/json/json_writer.cc +++ b/src/core/lib/json/json_writer.cc @@ -283,7 +283,7 @@ void JsonWriter::ValueString(const std::string& string) { void JsonWriter::DumpObject(const Json::Object& object) { ContainerBegins(Json::Type::OBJECT); for (const auto& p : object) { - ObjectKey(p.first.data()); + ObjectKey(p.first); DumpValue(p.second); } ContainerEnds(Json::Type::OBJECT); diff --git a/src/core/lib/surface/completion_queue.cc b/src/core/lib/surface/completion_queue.cc index 25a2aae7700..b085cb8d8ce 100644 --- a/src/core/lib/surface/completion_queue.cc +++ b/src/core/lib/surface/completion_queue.cc @@ -768,7 +768,7 @@ static void cq_end_op_for_pluck( if (GRPC_TRACE_FLAG_ENABLED(grpc_api_trace) || (GRPC_TRACE_FLAG_ENABLED(grpc_trace_operation_failures) && !error.ok())) { - std::string errmsg = grpc_core::StatusToString(error).c_str(); + std::string errmsg = grpc_core::StatusToString(error); GRPC_API_TRACE( "cq_end_op_for_pluck(cq=%p, tag=%p, error=%s, " "done=%p, done_arg=%p, storage=%p)", diff --git a/src/cpp/server/external_connection_acceptor_impl.cc b/src/cpp/server/external_connection_acceptor_impl.cc index 2be1a9f18d1..cdc66b24598 100644 --- a/src/cpp/server/external_connection_acceptor_impl.cc +++ b/src/cpp/server/external_connection_acceptor_impl.cc @@ -92,7 +92,7 @@ void ExternalConnectionAcceptorImpl::Start() { } void ExternalConnectionAcceptorImpl::SetToChannelArgs(ChannelArguments* args) { - args->SetPointer(name_.c_str(), &handler_); + args->SetPointer(name_, &handler_); } } // namespace internal diff --git a/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc b/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc index c9dbcf57eec..10857eaa291 100644 --- a/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc +++ b/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc @@ -114,8 +114,7 @@ class FakeHandshakeServer { 0 /* expected max concurrent rpcs unset */); } grpc::ServerBuilder builder; - builder.AddListeningPort(address_.c_str(), - grpc::InsecureServerCredentials()); + builder.AddListeningPort(address_, grpc::InsecureServerCredentials()); builder.RegisterService(service_.get()); // TODO(apolcyn): when removing the global concurrent handshake limiting // queue, set MAX_CONCURRENT_STREAMS on this server. diff --git a/test/cpp/end2end/rls_end2end_test.cc b/test/cpp/end2end/rls_end2end_test.cc index bc750153f0e..98b7359be9f 100644 --- a/test/cpp/end2end/rls_end2end_test.cc +++ b/test/cpp/end2end/rls_end2end_test.cc @@ -201,8 +201,8 @@ class RlsEnd2endTest : public ::testing::Test { nullptr)); call_creds->Unref(); channel_creds->Unref(); - channel_ = grpc::CreateCustomChannel( - absl::StrCat("fake:///", kServerName).c_str(), std::move(creds), args); + channel_ = grpc::CreateCustomChannel(absl::StrCat("fake:///", kServerName), + std::move(creds), args); stub_ = grpc::testing::EchoTestService::NewStub(channel_); } diff --git a/test/cpp/end2end/xds/xds_end2end_test_lib.cc b/test/cpp/end2end/xds/xds_end2end_test_lib.cc index 09c9dd47351..a5a5a70c36b 100644 --- a/test/cpp/end2end/xds/xds_end2end_test_lib.cc +++ b/test/cpp/end2end/xds/xds_end2end_test_lib.cc @@ -793,7 +793,7 @@ std::shared_ptr XdsEnd2endTest::CreateChannel( // same thing for the response generator to use for the xDS // channel and the xDS resource-does-not-exist timeout value. args->SetString(GRPC_ARG_TEST_ONLY_DO_NOT_USE_IN_PROD_XDS_BOOTSTRAP_CONFIG, - bootstrap_.c_str()); + bootstrap_); args->SetPointerWithVtable( GRPC_ARG_TEST_ONLY_DO_NOT_USE_IN_PROD_XDS_CLIENT_CHANNEL_ARGS, &xds_channel_args_, &kChannelArgsArgVtable); diff --git a/test/cpp/interop/interop_client.cc b/test/cpp/interop/interop_client.cc index 199f94762c2..32ab4e9215c 100644 --- a/test/cpp/interop/interop_client.cc +++ b/test/cpp/interop/interop_client.cc @@ -224,7 +224,7 @@ bool InteropClient::DoComputeEngineCreds( gpr_log(GPR_DEBUG, "Got username %s", response.username().c_str()); gpr_log(GPR_DEBUG, "Got oauth_scope %s", response.oauth_scope().c_str()); GPR_ASSERT(!response.username().empty()); - GPR_ASSERT(response.username().c_str() == default_service_account); + GPR_ASSERT(response.username() == default_service_account); GPR_ASSERT(!response.oauth_scope().empty()); const char* oauth_scope_str = response.oauth_scope().c_str(); GPR_ASSERT(absl::StrContains(oauth_scope, oauth_scope_str)); @@ -314,7 +314,7 @@ bool InteropClient::DoGoogleDefaultCredentials( gpr_log(GPR_DEBUG, "Got username %s", response.username().c_str()); GPR_ASSERT(!response.username().empty()); - GPR_ASSERT(response.username().c_str() == default_service_account); + GPR_ASSERT(response.username() == default_service_account); gpr_log(GPR_DEBUG, "Large unary rpc with GoogleDefaultCredentials done."); return true; } diff --git a/test/cpp/qps/qps_json_driver.cc b/test/cpp/qps/qps_json_driver.cc index 26cc83eb261..c6cf60dcf2f 100644 --- a/test/cpp/qps/qps_json_driver.cc +++ b/test/cpp/qps/qps_json_driver.cc @@ -256,7 +256,7 @@ static bool QpsDriver() { json = std::string(data, data + len); delete[] data; } else if (scjson) { - json = absl::GetFlag(FLAGS_scenarios_json).c_str(); + json = absl::GetFlag(FLAGS_scenarios_json); } else if (absl::GetFlag(FLAGS_quit)) { return RunQuit(absl::GetFlag(FLAGS_credential_type), per_worker_credential_types); @@ -264,7 +264,7 @@ static bool QpsDriver() { // Parse into an array of scenarios Scenarios scenarios; - ParseJson(json.c_str(), "grpc.testing.Scenarios", &scenarios); + ParseJson(json, "grpc.testing.Scenarios", &scenarios); bool success = true; // Make sure that there is at least some valid scenario here diff --git a/test/cpp/qps/qps_worker.cc b/test/cpp/qps/qps_worker.cc index 66fcddcbff3..8a93d57c1a9 100644 --- a/test/cpp/qps/qps_worker.cc +++ b/test/cpp/qps/qps_worker.cc @@ -284,7 +284,7 @@ QpsWorker::QpsWorker(int driver_port, int server_port, if (driver_port >= 0) { std::string server_address = grpc_core::JoinHostPort("::", driver_port); builder->AddListeningPort( - server_address.c_str(), + server_address, GetCredentialsProvider()->GetServerCredentials(credential_type)); } builder->RegisterService(impl_.get()); diff --git a/test/cpp/qps/server_async.cc b/test/cpp/qps/server_async.cc index d57e5a8c366..108acdeba59 100644 --- a/test/cpp/qps/server_async.cc +++ b/test/cpp/qps/server_async.cc @@ -81,9 +81,8 @@ class AsyncQpsServerTest final : public grpc::testing::Server { // Negative port number means inproc server, so no listen port needed if (port_num >= 0) { std::string server_address = grpc_core::JoinHostPort("::", port_num); - builder->AddListeningPort(server_address.c_str(), - Server::CreateServerCredentials(config), - &port_num); + builder->AddListeningPort( + server_address, Server::CreateServerCredentials(config), &port_num); } register_service(builder.get(), &async_service_); diff --git a/test/cpp/qps/server_callback.cc b/test/cpp/qps/server_callback.cc index f7ceb437b40..f2583042096 100644 --- a/test/cpp/qps/server_callback.cc +++ b/test/cpp/qps/server_callback.cc @@ -103,9 +103,8 @@ class CallbackServer final : public grpc::testing::Server { // Negative port number means inproc server, so no listen port needed if (port_num >= 0) { std::string server_address = grpc_core::JoinHostPort("::", port_num); - builder->AddListeningPort(server_address.c_str(), - Server::CreateServerCredentials(config), - &port_num); + builder->AddListeningPort( + server_address, Server::CreateServerCredentials(config), &port_num); } ApplyConfigToBuilder(config, builder.get()); diff --git a/test/cpp/qps/server_sync.cc b/test/cpp/qps/server_sync.cc index 094e1c1c825..7fec46b1b0d 100644 --- a/test/cpp/qps/server_sync.cc +++ b/test/cpp/qps/server_sync.cc @@ -161,9 +161,8 @@ class SynchronousServer final : public grpc::testing::Server { // Negative port number means inproc server, so no listen port needed if (port_num >= 0) { std::string server_address = grpc_core::JoinHostPort("::", port_num); - builder->AddListeningPort(server_address.c_str(), - Server::CreateServerCredentials(config), - &port_num); + builder->AddListeningPort( + server_address, Server::CreateServerCredentials(config), &port_num); } ApplyConfigToBuilder(config, builder.get()); diff --git a/test/cpp/util/grpc_tool.cc b/test/cpp/util/grpc_tool.cc index 1c3dbedd161..07eea786b8d 100644 --- a/test/cpp/util/grpc_tool.cc +++ b/test/cpp/util/grpc_tool.cc @@ -240,7 +240,7 @@ std::shared_ptr CreateCliChannel( } if (!absl::GetFlag(FLAGS_default_service_config).empty()) { args.SetString(GRPC_ARG_SERVICE_CONFIG, - absl::GetFlag(FLAGS_default_service_config).c_str()); + absl::GetFlag(FLAGS_default_service_config)); } // See |GRPC_ARG_MAX_METADATA_SIZE| in |grpc_types.h|. // Set to large enough size (10M) that should work for most use cases. @@ -319,7 +319,7 @@ int GrpcToolMainLib(int argc, const char** argv, const CliCredentials& cred, const bool ok = cmd->function(&grpc_tool, argc, argv, cred, callback); return ok ? 0 : 1; } else { - Usage("Invalid command '" + std::string(command.c_str()) + "'"); + Usage("Invalid command '" + command + "'"); } return 1; }