Skip to content

Commit 98e6caf

Browse files
authored
[stdlib] Adding a deprecated version of flatMap to warn misuses. (#7823)
Due to implicit promotion to optional it is possible to call flatMap with a closure, that does not return an optional. This way the code works, but is unnecessary inefficient. Such uses of flatMap can and should be replaced with map.
1 parent f90a7f0 commit 98e6caf

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

stdlib/public/core/SequenceAlgorithms.swift.gyb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,13 @@ extension Sequence {
717717
}
718718
return result
719719
}
720+
721+
@available(*, deprecated, message: "This call uses implicit promotion to optional. Please use map instead.")
722+
public func flatMap<T>(
723+
_ transform: (${GElement}) throws -> T
724+
) rethrows -> [T] {
725+
return try map(transform)
726+
}
720727
}
721728

722729
extension Sequence {

test/Constraints/closures.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ func returnsArray() -> [Int] { return [] }
513513

514514
returnsArray().flatMap { $0 }.flatMap { }
515515
// expected-warning@-1 {{expression of type 'Int' is unused}}
516-
// expected-warning@-2 {{result of call to 'flatMap' is unused}}
516+
// expected-warning@-2 {{Please use map instead.}}
517+
// expected-warning@-3 {{result of call to 'flatMap' is unused}}
517518

518519
// rdar://problem/30271695
519520
_ = ["hi"].flatMap { $0.isEmpty ? nil : $0 }

test/DebugInfo/generic_arg5.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public func foo<Type>(_ values : [S<Type>])
1717
// CHECK: ![[TY]] = !DICompositeType({{.*}}identifier: "_T012generic_arg51SVyAA3fooySayACyxGGlFQq_GD")
1818
// CHECK: ![[EXPR]] = !DIExpression(DW_OP_deref)
1919
let _ = values.flatMap { arg in
20-
return arg
20+
return .some(arg)
2121
}
2222

2323
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- FlatMapDiagnostics.swift -----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// RUN: rm -rf %t && mkdir -p %t
13+
// RUN: %gyb %s -o %t/FlatMapDiagnostics.swift
14+
// RUN: %target-swift-frontend -typecheck -verify %t/FlatMapDiagnostics.swift
15+
16+
17+
% for Type in [
18+
% 'Sequence',
19+
% 'Collection',
20+
% 'LazySequenceProtocol',
21+
% 'LazyCollectionProtocol']:
22+
23+
func testGeneric${Type}<T : ${Type}>(xs: T) {
24+
_ = xs.flatMap { $0 } // expected-warning {{Please use map instead.}}
25+
}
26+
27+
% end
28+
29+
func testArray(xs: [Int]) {
30+
_ = xs.flatMap { $0 } // expected-warning {{Please use map instead.}}
31+
_ = xs.lazy.flatMap { $0 } // expected-warning {{Please use map instead.}}
32+
}
33+
34+
func testGenericLazyBidirectionalCollection<
35+
T : LazyCollectionProtocol & BidirectionalCollection
36+
>(xs: T) where T.Elements : BidirectionalCollection {
37+
_ = xs.flatMap { $0 } // expected-warning {{Please use map instead.}}
38+
}
39+

0 commit comments

Comments
 (0)