Repository navigation
Report the existing value when AddCustomContext rejects a duplicate key - #2374
Closed
mohitduhan19 wants to merge 2 commits into
Closed
mohitduhan19 wants to merge 2 commits into
mohitduhan19 wants to merge 2 commits into
Conversation
AddCustomContext moved key and value into std::map::emplace and then
printed them in the duplicate-key warning. That has two problems:
* The warning printed the rejected new value instead of the value that
is already stored, e.g. adding ("foo", "bar") then ("foo", "qux")
reported that "foo" already exists with value "qux".
* Reading key/value after std::move relies on emplace not consuming its
arguments when insertion fails, which the standard does not guarantee
(flagged by clang-tidy bugprone-use-after-move, see google#1925).
Read the key and existing value through the iterator returned by
emplace instead, and check the warning text in the DuplicateKey test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gj2XnTKgWqWhmcDNH9gkv8
The test fails without the previous commit, because the warning reported the rejected value "qux" instead of the stored value "bar". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gj2XnTKgWqWhmcDNH9gkv8
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
Superseded by #2375 (same change, recreated so every commit author is covered by the CLA). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AddCustomContextmoveskeyandvalueintostd::map::emplaceand then prints them in the duplicate-key warning. Two problems with that:key/valueafterstd::moverelies onemplaceleaving its arguments untouched when insertion fails, which the standard does not guarantee (an implementation may construct the node first). This is thebugprone-use-after-movefinding insrc/benchmark.ccfrom the clang-tidy list in [BUG] clang-tidy warnings and errors #1925. The moves were added in core(perf): use string move constructor for AddCustomContext #2005.The fix reads the key and the existing value through the iterator returned by
emplace, so it keeps the moves from #2005 and never touches the moved-from strings.Testing
AddCustomContext.DuplicateKeynow captures stderr and checks the warning text. It fails on currentmain(prints"qux") and passes with this change.ctest: 88/88 pass with clang 18 and gcc (Linux).clang-format --dry-run --Werrorclean on both files;clang-tidyno longer reportsbugprone-use-after-moveinsrc/benchmark.cc.AI usage disclosure (per AGENTS.md)
I used Claude (Anthropic's AI assistant) to help find this clang-tidy finding, write the fix and the test, and run the test suite. I have reviewed the change and understand it, and I'm happy to discuss or revise it.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Gj2XnTKgWqWhmcDNH9gkv8