Skip to content

Commit 42f7d7c

Browse files
addaleaxtargos
authored andcommitted
src: remove unnecessary std::string error messages
If we can just use the classic `THROW_...()` methods directly, without needing to allocate an `std::string` for the message/format parameter, let's just do so. PR-URL: #60057 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 30c2c0f commit 42f7d7c

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/node_file.cc

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,24 +3266,25 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
32663266
if (!error_code) {
32673267
// Check if src and dest are identical.
32683268
if (std::filesystem::equivalent(src_path, dest_path)) {
3269-
std::string message = "src and dest cannot be the same %s";
3270-
return THROW_ERR_FS_CP_EINVAL(env, message.c_str(), dest_path_str);
3269+
static constexpr const char* message =
3270+
"src and dest cannot be the same %s";
3271+
return THROW_ERR_FS_CP_EINVAL(env, message, dest_path_str);
32713272
}
32723273

32733274
const bool dest_is_dir =
32743275
dest_status.type() == std::filesystem::file_type::directory;
32753276
if (src_is_dir && !dest_is_dir) {
3276-
std::string message =
3277+
static constexpr const char* message =
32773278
"Cannot overwrite non-directory %s with directory %s";
32783279
return THROW_ERR_FS_CP_DIR_TO_NON_DIR(
3279-
env, message.c_str(), dest_path_str, src_path_str);
3280+
env, message, dest_path_str, src_path_str);
32803281
}
32813282

32823283
if (!src_is_dir && dest_is_dir) {
3283-
std::string message =
3284+
static constexpr const char* message =
32843285
"Cannot overwrite directory %s with non-directory %s";
32853286
return THROW_ERR_FS_CP_NON_DIR_TO_DIR(
3286-
env, message.c_str(), dest_path_str, src_path_str);
3287+
env, message, dest_path_str, src_path_str);
32873288
}
32883289
}
32893290

@@ -3292,9 +3293,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
32923293
}
32933294
// Check if dest_path is a subdirectory of src_path.
32943295
if (src_is_dir && dest_path_str.starts_with(src_path_str)) {
3295-
std::string message = "Cannot copy %s to a subdirectory of self %s";
3296-
return THROW_ERR_FS_CP_EINVAL(
3297-
env, message.c_str(), src_path_str, dest_path_str);
3296+
static constexpr const char* message =
3297+
"Cannot copy %s to a subdirectory of self %s";
3298+
return THROW_ERR_FS_CP_EINVAL(env, message, src_path_str, dest_path_str);
32983299
}
32993300

33003301
auto dest_parent = dest_path.parent_path();
@@ -3305,9 +3306,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
33053306
dest_parent.parent_path() != dest_parent) {
33063307
if (std::filesystem::equivalent(
33073308
src_path, dest_path.parent_path(), error_code)) {
3308-
std::string message = "Cannot copy %s to a subdirectory of self %s";
3309-
return THROW_ERR_FS_CP_EINVAL(
3310-
env, message.c_str(), src_path_str, dest_path_str);
3309+
static constexpr const char* message =
3310+
"Cannot copy %s to a subdirectory of self %s";
3311+
return THROW_ERR_FS_CP_EINVAL(env, message, src_path_str, dest_path_str);
33113312
}
33123313

33133314
// If equivalent fails, it's highly likely that dest_parent does not exist
@@ -3319,23 +3320,24 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
33193320
}
33203321

33213322
if (src_is_dir && !recursive) {
3322-
std::string message =
3323+
static constexpr const char* message =
33233324
"Recursive option not enabled, cannot copy a directory: %s";
3324-
return THROW_ERR_FS_EISDIR(env, message.c_str(), src_path_str);
3325+
return THROW_ERR_FS_EISDIR(env, message, src_path_str);
33253326
}
33263327

33273328
switch (src_status.type()) {
33283329
case std::filesystem::file_type::socket: {
3329-
std::string message = "Cannot copy a socket file: %s";
3330-
return THROW_ERR_FS_CP_SOCKET(env, message.c_str(), dest_path_str);
3330+
static constexpr const char* message = "Cannot copy a socket file: %s";
3331+
return THROW_ERR_FS_CP_SOCKET(env, message, dest_path_str);
33313332
}
33323333
case std::filesystem::file_type::fifo: {
3333-
std::string message = "Cannot copy a FIFO pipe: %s";
3334-
return THROW_ERR_FS_CP_FIFO_PIPE(env, message.c_str(), dest_path_str);
3334+
static constexpr const char* message = "Cannot copy a FIFO pipe: %s";
3335+
return THROW_ERR_FS_CP_FIFO_PIPE(env, message, dest_path_str);
33353336
}
33363337
case std::filesystem::file_type::unknown: {
3337-
std::string message = "Cannot copy an unknown file type: %s";
3338-
return THROW_ERR_FS_CP_UNKNOWN(env, message.c_str(), dest_path_str);
3338+
static constexpr const char* message =
3339+
"Cannot copy an unknown file type: %s";
3340+
return THROW_ERR_FS_CP_UNKNOWN(env, message, dest_path_str);
33393341
}
33403342
default:
33413343
break;

0 commit comments

Comments
 (0)