diff --git a/include/nlohmann/detail/view/errors.hpp b/include/nlohmann/detail/view/errors.hpp index 6ce25316b..597073e1a 100644 --- a/include/nlohmann/detail/view/errors.hpp +++ b/include/nlohmann/detail/view/errors.hpp @@ -259,7 +259,8 @@ std::string diagnostics(const error_site site) const bool found = edited ? value_path(d, site.value(), tokens) : value_path(d, site.value(), tokens); if (!found || tokens.empty()) { - return ""; + // context is still empty; returning it keeps the copy elided (-Wnrvo) + return context; } std::string path; for (const std::string& token : tokens) diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index cfb879a09..4cfe38d6f 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -4765,7 +4765,8 @@ std::string diagnostics(const error_site site) const bool found = edited ? value_path(d, site.value(), tokens) : value_path(d, site.value(), tokens); if (!found || tokens.empty()) { - return ""; + // context is still empty; returning it keeps the copy elided (-Wnrvo) + return context; } std::string path; for (const std::string& token : tokens)