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

Rewrite code where MSVS gives nodiscard warnings #39941

Merged
merged 3 commits into from
Apr 27, 2020

Conversation

AMurkin
Copy link
Contributor

@AMurkin AMurkin commented Apr 26, 2020

Summary

SUMMARY: None

Purpose of change

This, I hope, fixes bugs detected by MSVS warnings: warning C4834: discarding return value of function with 'nodiscard' attribute.

Describe the solution

  • Wrapped std::remove_if() in vector.erase(). This actually erases elements.
  • Rewrite the lambda function + std::any_of() as a for loop. The logic should not change here, but it eliminates the usage std::any_of()'s side effect to achieve the same.
  • Change std::find_if() to a std::copy_if(). This gets all matches into a target and not only the first match, which was a bug.

Testing

None. But it compiles.

Copy link
Contributor

@ifreund ifreund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for fixing these!

src/npcmove.cpp Outdated
Comment on lines 476 to 478
if( is_too_close ) {
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but we could move this check before the if ( guy.lock() ) { ... check to save a little on performance since I think this is a hot code path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wont change anything since is_too_close is updated only after locking each guy.
If I understand your suggestion correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could start out true though if dist <= def_radius is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw it. I will amend.


res.push_back( r );
return true;
std::copy_if( recipes.begin(), recipes.end(), std::back_inserter( res ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and idiomatic 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a similar code turned out to be a few lines below.

@ifreund ifreund added <Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` labels Apr 26, 2020
@AMurkin AMurkin force-pushed the fix-nodiscard-warnings branch from 39cd8a2 to 4a5c381 Compare April 26, 2020 20:16
@ifreund ifreund self-assigned this Apr 26, 2020
src/game.cpp Outdated Show resolved Hide resolved
Co-Authored-By: Isaac Freund <[email protected]>
src/recipe_dictionary.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@ifreund ifreund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ifreund ifreund merged commit 7a0d2aa into CleverRaven:master Apr 27, 2020
fengjixuchui added a commit to fengjixuchui/Cataclysm-DDA that referenced this pull request Apr 27, 2020
Rewrite code where MSVS gives nodiscard warnings (CleverRaven#39941)
@AMurkin AMurkin deleted the fix-nodiscard-warnings branch April 27, 2020 07:55
Drewscriver pushed a commit to Drewscriver/Cataclysm-DDA that referenced this pull request Apr 30, 2020
* Rewrite code where MSVS gives nodiscard warnings

* Till the end

Co-Authored-By: Isaac Freund <[email protected]>

* Remove unused variable

Co-authored-by: Isaac Freund <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
<Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants