@@ -4762,8 +4762,9 @@ class AsyncConverter : private SourceEntityWalker {
4762
4762
SmallString<0 > Buffer;
4763
4763
llvm::raw_svector_ostream OS;
4764
4764
4765
- // Decls where any force-unwrap of that decl should be unwrapped, eg. for a
4766
- // previously optional closure paramter has become a non-optional local
4765
+ // Decls where any force unwrap or optional chain of that decl should be
4766
+ // elided, e.g for a previously optional closure parameter that has become a
4767
+ // non-optional local.
4767
4768
llvm::DenseSet<const Decl *> Unwraps;
4768
4769
// Decls whose references should be replaced with, either because they no
4769
4770
// longer exist or are a different type. Any replaced code should ideally be
@@ -4874,11 +4875,19 @@ class AsyncConverter : private SourceEntityWalker {
4874
4875
OS << PLACEHOLDER_END;
4875
4876
});
4876
4877
}
4877
- } else if (auto *FTE = dyn_cast<ForceValueExpr>(E)) {
4878
- if (auto *D = FTE->getReferencedDecl ().getDecl ()) {
4878
+ } else if (isa<ForceValueExpr>(E) || isa<BindOptionalExpr>(E)) {
4879
+ // Remove a force unwrap or optional chain of a returned success value,
4880
+ // as it will no longer be optional. For force unwraps, this is always a
4881
+ // valid transform. For optional chains, it is a locally valid transform
4882
+ // within the optional chain e.g foo?.x -> foo.x, but may change the type
4883
+ // of the overall chain, which could cause errors elsewhere in the code.
4884
+ // However this is generally more useful to the user than just leaving
4885
+ // 'foo' as a placeholder. Note this is only the case when no other
4886
+ // optionals are involved in the chain, e.g foo?.x?.y -> foo.x?.y is
4887
+ // completely valid.
4888
+ if (auto *D = E->getReferencedDecl ().getDecl ()) {
4879
4889
if (Unwraps.count (D))
4880
- return addCustom (FTE->getStartLoc (),
4881
- FTE->getEndLoc ().getAdvancedLoc (1 ),
4890
+ return addCustom (E->getStartLoc (), E->getEndLoc ().getAdvancedLoc (1 ),
4882
4891
[&]() { OS << newNameFor (D, true ); });
4883
4892
}
4884
4893
} else if (NestedExprCount == 0 ) {
0 commit comments