mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Merge pull request #22802 from xcadaverx/xcadaverx/SR-9851_Incorrect_fix_it_logical_not_optional_bool
[Sema] SR-9851 - Add parens when needed for nil coalescing fixits
This commit is contained in:
@@ -546,7 +546,7 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
|
||||
}
|
||||
|
||||
// Suggest a default value via ?? <default value>
|
||||
static void offerDefaultValueUnwrapFixit(TypeChecker &TC, DeclContext *DC, Expr *expr) {
|
||||
static void offerDefaultValueUnwrapFixit(TypeChecker &TC, DeclContext *DC, Expr *expr, Expr *rootExpr) {
|
||||
auto diag =
|
||||
TC.diagnose(expr->getLoc(), diag::unwrap_with_default_value);
|
||||
|
||||
@@ -554,7 +554,7 @@ static void offerDefaultValueUnwrapFixit(TypeChecker &TC, DeclContext *DC, Expr
|
||||
bool needsParensInside =
|
||||
exprNeedsParensBeforeAddingNilCoalescing(TC, DC, expr);
|
||||
bool needsParensOutside =
|
||||
exprNeedsParensAfterAddingNilCoalescing(TC, DC, expr, expr);
|
||||
exprNeedsParensAfterAddingNilCoalescing(TC, DC, expr, rootExpr);
|
||||
|
||||
llvm::SmallString<2> insertBefore;
|
||||
llvm::SmallString<32> insertAfter;
|
||||
@@ -618,7 +618,7 @@ public:
|
||||
int referencesCount() { return count; }
|
||||
};
|
||||
|
||||
static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type baseType,
|
||||
static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Expr *rootExpr, Type baseType,
|
||||
Type unwrappedType) {
|
||||
|
||||
assert(!baseType->hasTypeVariable() &&
|
||||
@@ -673,14 +673,14 @@ static bool diagnoseUnwrap(ConstraintSystem &CS, Expr *expr, Type baseType,
|
||||
}
|
||||
diag.flush();
|
||||
|
||||
offerDefaultValueUnwrapFixit(CS.TC, varDecl->getDeclContext(),
|
||||
initializer);
|
||||
offerDefaultValueUnwrapFixit(CS.TC, varDecl->getDeclContext(),
|
||||
initializer, rootExpr);
|
||||
offerForceUnwrapFixit(CS, initializer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
offerDefaultValueUnwrapFixit(CS.TC, CS.DC, expr);
|
||||
offerDefaultValueUnwrapFixit(CS.TC, CS.DC, expr, rootExpr);
|
||||
offerForceUnwrapFixit(CS, expr);
|
||||
return true;
|
||||
}
|
||||
@@ -690,6 +690,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
|
||||
return false;
|
||||
|
||||
auto *anchor = getAnchor();
|
||||
auto *rootExpr = getParentExpr();
|
||||
|
||||
if (auto assignExpr = dyn_cast<AssignExpr>(anchor))
|
||||
anchor = assignExpr->getSrc();
|
||||
@@ -699,7 +700,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
|
||||
|
||||
auto *tryExpr = dyn_cast<OptionalTryExpr>(unwrapped);
|
||||
if (!tryExpr) {
|
||||
return diagnoseUnwrap(getConstraintSystem(), unwrapped, getBaseType(),
|
||||
return diagnoseUnwrap(getConstraintSystem(), unwrapped, rootExpr, getBaseType(),
|
||||
getUnwrappedType());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user