mirror of
https://github.com/nlohmann/json.git
synced 2026-09-07 16:57:59 +00:00
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 <mail@nlohmann.me>
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user