https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109305
--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Charles-Henri Gros from comment #7) > For context, we're trying to detect cases where using "auto" unintentionally > creates a copy (it's regrettably common). > Here the copy is necessary to get a non-const value; that's definitely > something we could consider. Thank you for your feedback. Yes, it's necessary. If the copy was replaced with a reference to __str's existing allocator, the next line of code would not compile: include/bits/basic_string.h:1597:57: error: binding reference of type ‘std::allocator_traits<std::allocator<char> >::allocator_type&’ {aka ‘std::allocator<char>&’} to ‘const std::allocator<char>’ discards qualifiers 1597 | auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1); | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~ Maybe you can use that to detect unnecessary copies: would changing auto to auto&& still compile? (That's still incomplete, it might compile, but behave incorrectly, but at least checking if it compiles would be a start!)