From acb4c7838c69614c19d7b4bf1082e3acf31f598e Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Fri, 15 Dec 2017 00:10:49 +0100 Subject: [PATCH 1/2] Fix intentional discarding of return value in topN This fixes a warning one gets when compiling with warnings enabled: "phobos/std/algorithm/sorting.d(3122): Warning: calling testcase.foo.__lambda1 without side effects discards return value of type bool, prepend a cast(void) if intentional" Testcase that fails compilation with `-w`: ``` import std.algorithm.sorting; auto foo() { int[] v = [ 25, 7, 9, 2, 0, 5, 21 ]; return topN!((a, b) => a > b)(v, 100); } ``` --- std/algorithm/sorting.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/algorithm/sorting.d b/std/algorithm/sorting.d index ea8771fec7f..d3a7ac6d80f 100644 --- a/std/algorithm/sorting.d +++ b/std/algorithm/sorting.d @@ -3119,7 +3119,7 @@ if (isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range) // Workaround for https://issues.dlang.org/show_bug.cgi?id=16528 // Safety checks: enumerate all potentially unsafe generic primitives // then use a @trusted implementation. - binaryFun!less(r[0], r[r.length - 1]); + cast(void) binaryFun!less(r[0], r[r.length - 1]); import std.algorithm.mutation : swapAt; r.swapAt(size_t(0), size_t(0)); static assert(is(typeof(r.length) == size_t)); From fd009c7434bf7d91fc4d9f1b65dcdcd1d948f164 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Fri, 15 Dec 2017 00:18:25 +0100 Subject: [PATCH 2/2] Expand topN testcase Passing the comparison operation as string "a < b" is tested a few lines up. This adds a test for passing a lambda function. --- std/algorithm/sorting.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/algorithm/sorting.d b/std/algorithm/sorting.d index d3a7ac6d80f..7e408002147 100644 --- a/std/algorithm/sorting.d +++ b/std/algorithm/sorting.d @@ -3137,7 +3137,7 @@ if (isRandomAccessRange!(Range) && hasLength!Range && hasSlicing!Range) topN!"a < b"(v, 100); assert(v == [ 25, 7, 9, 2, 0, 5, 21 ]); auto n = 4; - topN!"a < b"(v, n); + topN!((a, b) => a < b)(v, n); assert(v[n] == 9); }