Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code analysis warning string_concat.hpp C26800: Use of a moved from object #3805

Closed
1 of 2 tasks
igirock opened this issue Oct 24, 2022 · 2 comments · Fixed by #3889
Closed
1 of 2 tasks

Code analysis warning string_concat.hpp C26800: Use of a moved from object #3805

igirock opened this issue Oct 24, 2022 · 2 comments · Fixed by #3889
Assignees
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@igirock
Copy link

igirock commented Oct 24, 2022

Description

A code analysis warning is reported for string_concat.hpp line 141.
##[warning]Include\nlohmann\detail\string_concat.hpp(141,0): Warning C26800: Use of a moved from object: ''(*<args_2>)'' (lifetime.1).

template<typename OutStringType = std::string, typename... Args>
inline OutStringType concat(Args && ... args)
{
OutStringType str;
str.reserve(concat_length(std::forward<Args>(args)...));
concat_into(str, std::forward<Args>(args)...);
return str;
}

As far as I understand, the issue seems to be that std::forward is used two times here.
It could be possible that the concat_length function moves the "args", and then they would be empty for the call on line 141.

Possible solution:

template<typename StringType, typename... Args>
inline std::size_t concat_length(const StringType& str, Args&& ... rest);

I think the concat_length function does not need a forwarding reference, as you never want to "move" there.
So i think changing it to something like this would fix it:

template<typename StringType, typename... Args>
inline std::size_t concat_length(const StringType& str, const Args& ... rest);

And then simply drop the std::forward on the call.

Reproduction steps

Do a Code Analysis build with MSVC 17.3 and use "Microsoft Native Recommended Rules"

Expected vs. actual results

No code analysis warning

Minimal code example

No response

Error messages

##[warning]Include\nlohmann\detail\string_concat.hpp(141,0): Warning C26800: Use of a moved from object: ''(*<args_2>)'' (lifetime.1).

Compiler and operating system

Win10; MSVC 17.3 (v143 platform toolset); c++ language standard: /std:c++latest

Library version

3.11.2 (include version)

Validation

@gregmarr
Copy link
Contributor

@falbrechtskirchinger Agreed, don't see any reason to accept rvalue references in concat_length, nothing is moved from, plain references should be fine.

@falbrechtskirchinger
Copy link
Contributor

Indeed, that's silly. Reminds me that I saw a similar issue and don't recall if I fixed it or not. I'll check over the weekend.

@nlohmann nlohmann self-assigned this Dec 18, 2022
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Dec 18, 2022
@nlohmann nlohmann added this to the Release 3.11.3 milestone Dec 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants