Skip to content

Commit

Permalink
Add handling of force unwrapped initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
tonell_m committed Dec 12, 2023
1 parent 715c814 commit d607c4c
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ private extension VariableDeclSyntax {
let typeAnnotation = binding.typeAnnotation,
let type = typeAnnotation.type.as(IdentifierTypeSyntax.self),
let typeName = type.typeName,
let initializer = binding.initializer?.value
var initializer = binding.initializer?.value
else {
return false
}

if let forceUnwrap = initializer.as(ForceUnwrapExprSyntax.self) {
initializer = forceUnwrap.expression
}

// If the initializer is a function call (generally a constructor or static builder),
// check if the base type is the same as the one from the type annotation.
if let functionCall = initializer.as(FunctionCallExprSyntax.self) {
Expand Down Expand Up @@ -167,6 +171,7 @@ extension RedundantTypeAnnotationRule {
Example("var url↓: URL = URL()"),
Example("let url↓: URL = URL()"),
Example("lazy var url↓: URL = URL()"),
Example("let url↓: URL = URL()!"),
Example("let alphanumerics↓: CharacterSet = CharacterSet.alphanumerics"),
Example("""
class ViewController: UIViewController {
Expand All @@ -184,7 +189,7 @@ extension RedundantTypeAnnotationRule {
var direction↓: Direction = Direction.up
"""),
Example("var num: Int = Int.random(0..<10")
Example("var num: Int = Int.random(0..<10"),
],
corrections: [
Example("var url↓: URL = URL()"): Example("var url = URL()"),
Expand Down

0 comments on commit d607c4c

Please sign in to comment.