Commit 6a36073a authored by Tom Jackson's avatar Tom Jackson Committed by facebook-github-bot-9

Prevent accidental moves in filter()

Summary: Per Boris's report.

Preparing to re-push this after fixing downstream errors.

Reviewed By: @ot

Differential Revision: D2361333
parent c0cb9812
......@@ -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));
}
});
......@@ -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;
......
......@@ -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 =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment