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”]
    ignore_properties false
    consider_default_literal_types_redundant false

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
//
// ignore_attributes: ["IgnoreMe"]
//

@IgnoreMe var a: Int = Int(5)

//
// ignore_attributes: ["IgnoreMe"]
//

var a: Int {
    @IgnoreMe let i: Int = Int(1)
    return i
}

var bol: Bool = true
var dbl: Double = 0.0
var int: Int = 0
var str: String = "str"
//
// ignore_properties: true
//

struct Foo {
    var url: URL = URL()
    let myVar: Int? = 0, s: String = ""
}

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)
  }
}
//
// ignore_properties: true
//

class ViewController: UIViewController {
  func someMethod() {
    let myVar: Int = Int(5)
  }
}

let a: [Int] = [Int]()
let a: A.B = A.B()
enum Direction {
    case up
    case down
}

var direction: Direction = Direction.up
//
// ignore_attributes: ["IgnoreMe"]
//

@DontIgnoreMe var a: Int = Int(5)

//
// ignore_attributes: ["IgnoreMe"]
//

@IgnoreMe
var a: Int {
    let i: Int = Int(1)
    return i
}

//
// consider_default_literal_types_redundant: true
//

var bol: Bool = true

//
// consider_default_literal_types_redundant: true
//

var dbl: Double = 0.0

//
// consider_default_literal_types_redundant: true
//

var int: Int = 0

//
// consider_default_literal_types_redundant: true
//

var str: String = "str"