[CSDiagnostics] Diagnose invalid optional unwrap via fixes

Detect and fix situations when (force) unwrap is used on
a non-optional type, this helps to diagnose invalid unwraps
precisely and provide fix-its.

Resolves: [SR-8977](https://bugs.swift.org/browse/SR-8977)
Resolves: rdar://problem/45218255
This commit is contained in:
Pavel Yaskevich
2018-12-05 08:10:07 -08:00
parent e043e2b2b3
commit 202234f325
8 changed files with 64 additions and 5 deletions

View File

@@ -1226,3 +1226,17 @@ bool AutoClosureForwardingFailure::diagnoseAsError() {
.fixItInsertAfter(argExpr->getEndLoc(), "()");
return true;
}
bool NonOptionalUnwrapFailure::diagnoseAsError() {
auto *anchor = getAnchor();
auto diagnostic = diag::invalid_optional_chain;
if (isa<ForceValueExpr>(anchor))
diagnostic = diag::invalid_force_unwrap;
emitDiagnostic(anchor->getLoc(), diagnostic, BaseType)
.highlight(anchor->getSourceRange())
.fixItRemove(anchor->getEndLoc());
return true;
}