@@ -4762,8 +4762,9 @@ class AsyncConverter : private SourceEntityWalker {
47624762 SmallString<0 > Buffer;
47634763 llvm::raw_svector_ostream OS;
47644764
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.
47674768 llvm::DenseSet<const Decl *> Unwraps;
47684769 // Decls whose references should be replaced with, either because they no
47694770 // longer exist or are a different type. Any replaced code should ideally be
@@ -4874,11 +4875,19 @@ class AsyncConverter : private SourceEntityWalker {
48744875 OS << PLACEHOLDER_END;
48754876 });
48764877 }
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 ()) {
48794889 if (Unwraps.count (D))
4880- return addCustom (FTE->getStartLoc (),
4881- FTE->getEndLoc ().getAdvancedLoc (1 ),
4890+ return addCustom (E->getStartLoc (), E->getEndLoc ().getAdvancedLoc (1 ),
48824891 [&]() { OS << newNameFor (D, true ); });
48834892 }
48844893 } else if (NestedExprCount == 0 ) {
0 commit comments