Redundant Type Annotation

Variables should not have redundant type annotation

  • Identifier: redundant_type_annotation
  • Enabled by default: No
  • Supports autocorrection: Yes
  • Kind: idiomatic
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning
    ignore_attributes [“IBInspectable”]

Non Triggering Examples

var url = URL()
var url: CustomStringConvertible = URL()
var one: Int = 1, two: Int = 2, three: Int
guard let url = URL() else { return }
if let url = URL() { return }
let alphanumerics = CharacterSet.alphanumerics
var set: Set<Int> = Set([])
var set: Set<Int> = Set.init([])
var set = Set<Int>([])
var set = Set<Int>.init([])
guard var set: Set<Int> = Set([]) else { return }
if var set: Set<Int> = Set.init([]) { return }
guard var set = Set<Int>([]) else { return }
if var set = Set<Int>.init([]) { return }
var one: A<T> = B()
var one: A = B<T>()
var one: A<T> = B<T>()
let a = A.b.c.d
let a: B = A.b.c.d
enum Direction {
    case up
    case down
}

var direction: Direction = .up
enum Direction {
    case up
    case down
}

var direction = Direction.up
@IgnoreMe var a: Int = Int(5)
var a: Int {
    @IgnoreMe let i: Int = Int(1)
    return i
}

Triggering Examples

var url:URL=URL()
var url:URL = URL(string: "")
var url: URL = URL()
let url: URL = URL()
lazy var url: URL = URL()
let url: URL = URL()!
var one: Int = 1, two: Int = Int(5), three: Int
guard let url: URL = URL() else { return }
if let url: URL = URL() { return }
let alphanumerics: CharacterSet = CharacterSet.alphanumerics
var set: Set<Int> = Set<Int>([])
var set: Set<Int> = Set<Int>.init([])
var set: Set = Set<Int>([])
var set: Set = Set<Int>.init([])
guard var set: Set = Set<Int>([]) else { return }
if var set: Set = Set<Int>.init([]) { return }
guard var set: Set<Int> = Set<Int>([]) else { return }
if var set: Set<Int> = Set<Int>.init([]) { return }
var set: Set = Set<Int>([]), otherSet: Set<Int>
var num: Int = Int.random(0..<10)
let a: A = A.b.c.d
let a: A = A.f().b
class ViewController: UIViewController {
  func someMethod() {
    let myVar: Int = Int(5)
  }
}
var isEnabled: Bool = true
let a: [Int] = [Int]()
let a: A.B = A.B()
enum Direction {
    case up
    case down
}

var direction: Direction = Direction.up
@DontIgnoreMe var a: Int = Int(5)
@IgnoreMe
var a: Int {
    let i: Int = Int(1)
    return i
}