Skip to content

Commit

Permalink
Prevent accidental moves in filter()
Browse files Browse the repository at this point in the history
Summary: Per Boris's report.

Preparing to re-push this after fixing downstream errors.

Reviewed By: @ot

Differential Revision: D2361333
  • Loading branch information
ddrcoder authored and facebook-github-bot-9 committed Aug 21, 2015
1 parent c0cb981 commit 6a36073
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions folly/gen/Base-inl.h
Expand Up @@ -559,7 +559,8 @@ class Filter : public Operator<Filter<Predicate>> {
template <class Body>
void foreach(Body&& body) const {
source_.foreach([&](Value value) {
if (pred_(std::forward<Value>(value))) {
// NB: Argument not forwarded to avoid accidental move-construction
if (pred_(value)) {
body(std::forward<Value>(value));
}
});
Expand All @@ -568,7 +569,8 @@ class Filter : public Operator<Filter<Predicate>> {
template <class Handler>
bool apply(Handler&& handler) const {
return source_.apply([&](Value value) -> bool {
if (pred_(std::forward<Value>(value))) {
// NB: Argument not forwarded to avoid accidental move-construction
if (pred_(value)) {
return handler(std::forward<Value>(value));
}
return true;
Expand Down
9 changes: 9 additions & 0 deletions folly/gen/test/BaseTest.cpp
Expand Up @@ -289,6 +289,15 @@ TEST(Gen, FilterDefault) {
}
}

TEST(Gen, FilterSink) {
auto actual
= seq(1, 2)
| map([](int x) { return vector<int>{x}; })
| filter([](vector<int> v) { return !v.empty(); })
| as<vector>();
EXPECT_FALSE(from(actual) | rconcat | isEmpty);
}

TEST(Gen, Contains) {
{
auto gen =
Expand Down

0 comments on commit 6a36073

Please sign in to comment.