From: Tom Jackson Date: Fri, 21 Aug 2015 17:28:33 +0000 (-0700) Subject: Prevent accidental moves in filter() X-Git-Tag: v0.55.0~9 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=6a36073a00ac3a1fd23be79ad720a963f86bf9e3;hp=c0cb9812a705e799cb4985d6435d7ab60ff67a61 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 --- diff --git a/folly/gen/Base-inl.h b/folly/gen/Base-inl.h index 0c4e92f4..23b91973 100644 --- a/folly/gen/Base-inl.h +++ b/folly/gen/Base-inl.h @@ -559,7 +559,8 @@ class Filter : public Operator> { template void foreach(Body&& body) const { source_.foreach([&](Value value) { - if (pred_(std::forward(value))) { + // NB: Argument not forwarded to avoid accidental move-construction + if (pred_(value)) { body(std::forward(value)); } }); @@ -568,7 +569,8 @@ class Filter : public Operator> { template bool apply(Handler&& handler) const { return source_.apply([&](Value value) -> bool { - if (pred_(std::forward(value))) { + // NB: Argument not forwarded to avoid accidental move-construction + if (pred_(value)) { return handler(std::forward(value)); } return true; diff --git a/folly/gen/test/BaseTest.cpp b/folly/gen/test/BaseTest.cpp index 7741b053..e97cf6c3 100644 --- a/folly/gen/test/BaseTest.cpp +++ b/folly/gen/test/BaseTest.cpp @@ -289,6 +289,15 @@ TEST(Gen, FilterDefault) { } } +TEST(Gen, FilterSink) { + auto actual + = seq(1, 2) + | map([](int x) { return vector{x}; }) + | filter([](vector v) { return !v.empty(); }) + | as(); + EXPECT_FALSE(from(actual) | rconcat | isEmpty); +} + TEST(Gen, Contains) { { auto gen =