From 321dd76d5fe169750889776391ac0256647f9381 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 7 Sep 2026 01:36:36 +0200 Subject: [PATCH] Suppress a clang-tidy false positive on an intentional defensive copy performance-unnecessary-copy-initialization suggests copy_for_patch could be a reference since it's never modified -- but the copy is the point: it guards against a hypothetical regression where patch() mutates its receiver, which a reference could never catch (the follow-up assertion would just compare `original` to itself). Signed-off-by: Niels Lohmann --- tests/src/unit-json_patch.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/src/unit-json_patch.cpp b/tests/src/unit-json_patch.cpp index 56fc1faf4..1db531309 100644 --- a/tests/src/unit-json_patch.cpp +++ b/tests/src/unit-json_patch.cpp @@ -743,8 +743,16 @@ TEST_CASE("JSON patch") // patch() never modifies the object it is called on -- it always // operates on (and returns) a separate copy, so the original is - // left completely untouched, regardless of success or failure - json copy_for_patch = original; + // left completely untouched, regardless of success or failure. + // copy_for_patch is intentionally a real copy, not a reference + // to `original`: the whole point of this check is to catch a + // hypothetical future regression where patch() *does* mutate its + // receiver. Using a reference here would make the assertion + // below compare `original` to itself -- trivially true even if + // such a bug existed -- which is exactly what a static analyzer + // can't see when it suggests "this copy is never modified, use + // a reference instead". + json copy_for_patch = original; // NOLINT(performance-unnecessary-copy-initialization) CHECK_THROWS_AS(copy_for_patch.patch(patch), json::other_error&); CHECK(copy_for_patch == original);