Repository navigation
Report the existing value when AddCustomContext rejects a duplicate key - #2375
Open
mohitduhan19 wants to merge 2 commits into
Open
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. The warning reported the rejected new value instead of the stored one, and reading key/value after std::move relies on emplace not consuming its arguments on failure (clang-tidy bugprone-use-after-move, see google#1925). Read the key and existing value through the iterator returned by emplace instead.
The test fails without the previous commit, because the warning reported the rejected value "qux" instead of the stored value "bar".
This branch has not been deployed
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.Replaces #2374, which I closed because its commits carried a co-author trailer that cannot be covered by the CLA.
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