swiftlang / swiftlang/swift-syntax
[SR-8510] 'guard' is easy to misspell as 'gaurd', results in large number of follow-on errors, and could have a fixit.
- Dominant language
- Swift
- Stars
- 3.7k
- Forks
- 553
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 16
Description
| | |
|------------------|-----------------|
|Previous ID | SR-8510 |
|Radar | rdar://problem/32419789 |
|Original Reporter | @huonw |
|Type | Bug |
|Status | In Progress |
|Resolution | |
Environment
Master at 2018-08-09
Additional Detail from JIRA
| | |
|------------------|-----------------|
|Votes | 1 |
|Component/s | Compiler |
|Labels | Bug, DiagnosticsQoI, Parser, StarterBug |
|Assignee | mkita (JIRA) |
|Priority | Medium |
md5: 83854736e7d37441acce9e16b297a24a
**Issue Description:**
In the following code, the guard statement is misspelled, and it results in 6 errors, plus notes and warnings! It'd be neat if the compiler detected when the "consecutive statements" error starts with the identifier `gaurd`, and assumed that `guard` was what was meant (along with a fixit so the user swaps it). I think that only doing this when that error occurs is important, because someone could legitimately have `var gaurd = 1; gaurd = 2; print(gaurd)`, and none of those should change to `guard`.
```swift
func foo() -> Int? { return 0 }
func bar() {
gaurd let x = foo() else { return }
print(x)
}
```
```
guard.swift:3:10: error: consecutive statements on a line must be separated by ';'
gaurd let x = foo() else { return }
^
;
guard.swift:3:24: error: consecutive statements on a line must be separated by ';'
gaurd let x = foo() else { return }
^
;
guard.swift:3:25: error: expected expression
gaurd let x = foo() else { return }
^
guard.swift:3:5: error: use of unresolved identifier 'gaurd'
gaurd let x = foo() else { return }
^~~~~
guard.swift:3:30: error: closure expression is unused
gaurd let x = foo() else { return }
^
guard.swift:3:30: note: did you mean to use a 'do' statement?
gaurd let x = foo() else { return }
^
do
guard.swift:4:11: warning: expression implicitly coerced from 'Int?' to 'Any'
print(x)
^
guard.swift:4:11: note: provide a default value to avoid this warning
print(x)
^
?? <#default value#>
guard.swift:4:11: note: force-unwrap the value to avoid this warning
print(x)
^
!
guard.swift:4:11: note: explicitly cast to 'Any' with 'as Any' to silence this warning
print(x)
^
as Any
```
Possible new version:
```
guard.swift:3:10: error: consecutive statements on a line must be separated by ';'; did you misspell 'guard'?
gaurd let x = foo() else { return }
^~~~~
guard
```
Contributor guide
Assessment
This issue has not been assessed yet.