mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user