-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
Showing
4 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===--- FlatMapDiagnostics.swift -----------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// RUN: rm -rf %t && mkdir -p %t | ||
// RUN: %gyb %s -o %t/FlatMapDiagnostics.swift | ||
// RUN: %target-swift-frontend -typecheck -verify %t/FlatMapDiagnostics.swift | ||
|
||
|
||
% for Type in [ | ||
% 'Sequence', | ||
% 'Collection', | ||
% 'LazySequenceProtocol', | ||
% 'LazyCollectionProtocol']: | ||
|
||
func testGeneric${Type}<T : ${Type}>(xs: T) { | ||
_ = xs.flatMap { $0 } // expected-warning {{Please use map instead.}} | ||
} | ||
|
||
% end | ||
|
||
func testArray(xs: [Int]) { | ||
_ = xs.flatMap { $0 } // expected-warning {{Please use map instead.}} | ||
_ = xs.lazy.flatMap { $0 } // expected-warning {{Please use map instead.}} | ||
} | ||
|
||
func testGenericLazyBidirectionalCollection< | ||
T : LazyCollectionProtocol & BidirectionalCollection | ||
>(xs: T) where T.Elements : BidirectionalCollection { | ||
_ = xs.flatMap { $0 } // expected-warning {{Please use map instead.}} | ||
} | ||
|