From fb6a31574693c3419fa46fff192b929e7c5c1068 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 31 Oct 2026 23:34:30 +0100 Subject: [PATCH] json_view: return the empty context so clang can elide its copy clang 22 with -Weverything (ci_test_standards_clang) reports -Wnrvo for 'return context;' in diagnostics(), because the early 'return "";' after context is declared rules out the named return value optimization. context is still empty there, so return it instead. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/errors.hpp | 3 ++- single_include/nlohmann/json_view.hpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) 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)