[Sema] Add diagnostics handling for nested expressions

This commit is contained in:
Minhyuk Kim
2021-06-17 11:10:07 +09:00
parent c3ab99a405
commit f85cb50af2
2 changed files with 51 additions and 15 deletions

View File

@@ -2888,21 +2888,27 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
// If the subexpr is an "as?" cast, we can rewrite it to
// be an "is" test.
bool isIsTest = false;
if (isa<ConditionalCheckedCastExpr>(initExpr) &&
!initExpr->isImplicit()) {
noParens = isIsTest = true;
ConditionalCheckedCastExpr *CCE = nullptr;
// initExpr can be wrapped inside parens or try expressions.
if (auto ccExpr = dyn_cast<ConditionalCheckedCastExpr>(
initExpr->getValueProvidingExpr())) {
if (!ccExpr->isImplicit()) {
CCE = ccExpr;
noParens = true;
}
}
// In cases where the value is optional, the cast expr is
// wrapped inside OptionalEvaluationExpr. Unwrap it to get
// ConditionalCheckedCastExpr.
if (auto oeExpr =
dyn_cast<OptionalEvaluationExpr>(initExpr)) {
if (auto oeExpr = dyn_cast<OptionalEvaluationExpr>(
initExpr->getValueProvidingExpr())) {
if (auto ccExpr = dyn_cast<ConditionalCheckedCastExpr>(
oeExpr->getSubExpr())) {
oeExpr->getSubExpr()->getValueProvidingExpr())) {
if (!ccExpr->isImplicit()) {
initExpr = ccExpr;
noParens = isIsTest = true;
CCE = ccExpr;
noParens = true;
}
}
}
@@ -2914,10 +2920,9 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
diagIF.fixItReplaceChars(introducerLoc,
initExpr->getStartLoc(),
&"("[noParens]);
if (isIsTest) {
if (CCE) {
// If this was an "x as? T" check, rewrite it to "x is T".
auto CCE = cast<ConditionalCheckedCastExpr>(initExpr);
diagIF.fixItReplace(SourceRange(CCE->getLoc(),
CCE->getQuestionLoc()),
"is");