Prefer Type Checking
Prefer a is X
to a as? X != nil
- Identifier:
prefer_type_checking
- Enabled by default: Yes
- Supports autocorrection: Yes
- Kind: idiomatic
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning
Non Triggering Examples
let foo = bar as? Foo
bar is Foo
2*x is X
if foo is Bar {
doSomeThing()
}
if let bar = foo as? Bar {
foo.run()
}
bar as Foo != nil
nil != bar as Foo
bar as Foo? != nil
bar as? Foo? != nil
Triggering Examples
bar ↓as? Foo != nil
2*x as? X != nil
if foo ↓as? Bar != nil {
doSomeThing()
}
nil != bar ↓as? Foo
nil != 2*x ↓as? X