Skip to content

Commit 764d356

Browse files
addaleaxaduh95
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 1289ef8 commit 764d356

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
@@ -3147,24 +3147,25 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
31473147
if (!error_code) {
31483148
// Check if src and dest are identical.
31493149
if (std::filesystem::equivalent(src_path, dest_path)) {
3150-
std::string message = "src and dest cannot be the same %s";
3151-
return THROW_ERR_FS_CP_EINVAL(env, message.c_str(), dest_path_str);
3150+
static constexpr const char* message =
3151+
"src and dest cannot be the same %s";
3152+
return THROW_ERR_FS_CP_EINVAL(env, message, dest_path_str);
31523153
}
31533154

31543155
const bool dest_is_dir =
31553156
dest_status.type() == std::filesystem::file_type::directory;
31563157
if (src_is_dir && !dest_is_dir) {
3157-
std::string message =
3158+
static constexpr const char* message =
31583159
"Cannot overwrite non-directory %s with directory %s";
31593160
return THROW_ERR_FS_CP_DIR_TO_NON_DIR(
3160-
env, message.c_str(), dest_path_str, src_path_str);
3161+
env, message, dest_path_str, src_path_str);
31613162
}
31623163

31633164
if (!src_is_dir && dest_is_dir) {
3164-
std::string message =
3165+
static constexpr const char* message =
31653166
"Cannot overwrite directory %s with non-directory %s";
31663167
return THROW_ERR_FS_CP_NON_DIR_TO_DIR(
3167-
env, message.c_str(), dest_path_str, src_path_str);
3168+
env, message, dest_path_str, src_path_str);
31683169
}
31693170
}
31703171

@@ -3173,9 +3174,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
31733174
}
31743175
// Check if dest_path is a subdirectory of src_path.
31753176
if (src_is_dir && dest_path_str.starts_with(src_path_str)) {
3176-
std::string message = "Cannot copy %s to a subdirectory of self %s";
3177-
return THROW_ERR_FS_CP_EINVAL(
3178-
env, message.c_str(), src_path_str, dest_path_str);
3177+
static constexpr const char* message =
3178+
"Cannot copy %s to a subdirectory of self %s";
3179+
return THROW_ERR_FS_CP_EINVAL(env, message, src_path_str, dest_path_str);
31793180
}
31803181

31813182
auto dest_parent = dest_path.parent_path();
@@ -3186,9 +3187,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
31863187
dest_parent.parent_path() != dest_parent) {
31873188
if (std::filesystem::equivalent(
31883189
src_path, dest_path.parent_path(), error_code)) {
3189-
std::string message = "Cannot copy %s to a subdirectory of self %s";
3190-
return THROW_ERR_FS_CP_EINVAL(
3191-
env, message.c_str(), src_path_str, dest_path_str);
3190+
static constexpr const char* message =
3191+
"Cannot copy %s to a subdirectory of self %s";
3192+
return THROW_ERR_FS_CP_EINVAL(env, message, src_path_str, dest_path_str);
31923193
}
31933194

31943195
// If equivalent fails, it's highly likely that dest_parent does not exist
@@ -3200,23 +3201,24 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
32003201
}
32013202

32023203
if (src_is_dir && !recursive) {
3203-
std::string message =
3204+
static constexpr const char* message =
32043205
"Recursive option not enabled, cannot copy a directory: %s";
3205-
return THROW_ERR_FS_EISDIR(env, message.c_str(), src_path_str);
3206+
return THROW_ERR_FS_EISDIR(env, message, src_path_str);
32063207
}
32073208

32083209
switch (src_status.type()) {
32093210
case std::filesystem::file_type::socket: {
3210-
std::string message = "Cannot copy a socket file: %s";
3211-
return THROW_ERR_FS_CP_SOCKET(env, message.c_str(), dest_path_str);
3211+
static constexpr const char* message = "Cannot copy a socket file: %s";
3212+
return THROW_ERR_FS_CP_SOCKET(env, message, dest_path_str);
32123213
}
32133214
case std::filesystem::file_type::fifo: {
3214-
std::string message = "Cannot copy a FIFO pipe: %s";
3215-
return THROW_ERR_FS_CP_FIFO_PIPE(env, message.c_str(), dest_path_str);
3215+
static constexpr const char* message = "Cannot copy a FIFO pipe: %s";
3216+
return THROW_ERR_FS_CP_FIFO_PIPE(env, message, dest_path_str);
32163217
}
32173218
case std::filesystem::file_type::unknown: {
3218-
std::string message = "Cannot copy an unknown file type: %s";
3219-
return THROW_ERR_FS_CP_UNKNOWN(env, message.c_str(), dest_path_str);
3219+
static constexpr const char* message =
3220+
"Cannot copy an unknown file type: %s";
3221+
return THROW_ERR_FS_CP_UNKNOWN(env, message, dest_path_str);
32203222
}
32213223
default:
32223224
break;

0 commit comments

Comments
 (0)