Nesting

Types should be nested at most 1 level deep, and functions should be nested at most 2 levels deep.

  • Identifier: nesting
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: metrics
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    type_level
    KeyValue
    warning 1
    function_level
    KeyValue
    warning 2
    check_nesting_in_closures_and_statements true
    always_allow_one_type_in_functions false
    ignore_typealiases_and_associatedtypes false

Non Triggering Examples

actor Example_0 {
    actor Example_1 {}
}
var example: Int {
    actor Example_0 {
        actor Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        actor Example_0 {
            actor Example_1 {}
        }
    }
}
extension Example_0 {
    actor Example_1 {}
}
class Example_0 {
    class Example_1 {}
}
var example: Int {
    class Example_0 {
        class Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        class Example_0 {
            class Example_1 {}
        }
    }
}
extension Example_0 {
    class Example_1 {}
}
struct Example_0 {
    struct Example_1 {}
}
var example: Int {
    struct Example_0 {
        struct Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        struct Example_0 {
            struct Example_1 {}
        }
    }
}
extension Example_0 {
    struct Example_1 {}
}
enum Example_0 {
    enum Example_1 {}
}
var example: Int {
    enum Example_0 {
        enum Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        enum Example_0 {
            enum Example_1 {}
        }
    }
}
extension Example_0 {
    enum Example_1 {}
}
func f_0() {
    func f_1() {
        func f_2() {}
    }
}
var example: Int {
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        func f_0() {
            func f_1() {
                func f_2() {}
            }
        }
    }
}
extension Example_0 {
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
switch example {
case .exampleCase:
    actor Example_0 {
        actor Example_1 {}
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
var exampleClosure: () -> Void = {
    actor Example_0 {
        actor Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
exampleFunc(closure: {
    actor Example_0 {
        actor Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
})
switch example {
case .exampleCase:
    class Example_0 {
        class Example_1 {}
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
var exampleClosure: () -> Void = {
    class Example_0 {
        class Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
exampleFunc(closure: {
    class Example_0 {
        class Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
})
switch example {
case .exampleCase:
    struct Example_0 {
        struct Example_1 {}
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
var exampleClosure: () -> Void = {
    struct Example_0 {
        struct Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
exampleFunc(closure: {
    struct Example_0 {
        struct Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
})
switch example {
case .exampleCase:
    enum Example_0 {
        enum Example_1 {}
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
var exampleClosure: () -> Void = {
    enum Example_0 {
        enum Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
}
exampleFunc(closure: {
    enum Example_0 {
        enum Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {}
        }
    }
})
actor Example_0 {
    protocol Example_1 {}
}
var example: Int {
    actor Example_0 {
        protocol Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        actor Example_0 {
            protocol Example_1 {}
        }
    }
}
extension Example_0 {
    protocol Example_1 {}
}
class Example_0 {
    protocol Example_1 {}
}
var example: Int {
    class Example_0 {
        protocol Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        class Example_0 {
            protocol Example_1 {}
        }
    }
}
extension Example_0 {
    protocol Example_1 {}
}
struct Example_0 {
    protocol Example_1 {}
}
var example: Int {
    struct Example_0 {
        protocol Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        struct Example_0 {
            protocol Example_1 {}
        }
    }
}
extension Example_0 {
    protocol Example_1 {}
}
enum Example_0 {
    protocol Example_1 {}
}
var example: Int {
    enum Example_0 {
        protocol Example_1 {}
    }
    return 5
}
var example: Int = 5 {
    didSet {
        enum Example_0 {
            protocol Example_1 {}
        }
    }
}
extension Example_0 {
    protocol Example_1 {}
}
actor Example_0 {
    func f_0() {
        actor Example_1 {
            func f_1() {
                func f_2() {}
            }
        }
        protocol P {}
    }
}
actor Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            actor Example_1 {
                func f_1() {
                    func f_2() {}
                }
            }
            protocol P {}
        default:
            exampleFunc(closure: {
                actor Example_1 {
                    func f_1() {
                        func f_2() {}
                    }
                }
                protocol P {}
            })
        }
    }
}
class Example_0 {
    func f_0() {
        class Example_1 {
            func f_1() {
                func f_2() {}
            }
        }
        protocol P {}
    }
}
class Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            class Example_1 {
                func f_1() {
                    func f_2() {}
                }
            }
            protocol P {}
        default:
            exampleFunc(closure: {
                class Example_1 {
                    func f_1() {
                        func f_2() {}
                    }
                }
                protocol P {}
            })
        }
    }
}
struct Example_0 {
    func f_0() {
        struct Example_1 {
            func f_1() {
                func f_2() {}
            }
        }
        protocol P {}
    }
}
struct Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            struct Example_1 {
                func f_1() {
                    func f_2() {}
                }
            }
            protocol P {}
        default:
            exampleFunc(closure: {
                struct Example_1 {
                    func f_1() {
                        func f_2() {}
                    }
                }
                protocol P {}
            })
        }
    }
}
enum Example_0 {
    func f_0() {
        enum Example_1 {
            func f_1() {
                func f_2() {}
            }
        }
        protocol P {}
    }
}
enum Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            enum Example_1 {
                func f_1() {
                    func f_2() {}
                }
            }
            protocol P {}
        default:
            exampleFunc(closure: {
                enum Example_1 {
                    func f_1() {
                        func f_2() {}
                    }
                }
                protocol P {}
            })
        }
    }
}

Triggering Examples

actor Example_0 {
    actor Example_1 {
        actor Example_2 {}
    }
}
var example: Int {
    actor Example_0 {
        actor Example_1 {
            actor Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        actor Example_0 {
            actor Example_1 {
                actor Example_2 {}
            }
        }
    }
}
extension Example_0 {
    actor Example_1 {
        actor Example_2 {}
    }
}
class Example_0 {
    class Example_1 {
        class Example_2 {}
    }
}
var example: Int {
    class Example_0 {
        class Example_1 {
            class Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        class Example_0 {
            class Example_1 {
                class Example_2 {}
            }
        }
    }
}
extension Example_0 {
    class Example_1 {
        class Example_2 {}
    }
}
struct Example_0 {
    struct Example_1 {
        struct Example_2 {}
    }
}
var example: Int {
    struct Example_0 {
        struct Example_1 {
            struct Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        struct Example_0 {
            struct Example_1 {
                struct Example_2 {}
            }
        }
    }
}
extension Example_0 {
    struct Example_1 {
        struct Example_2 {}
    }
}
enum Example_0 {
    enum Example_1 {
        enum Example_2 {}
    }
}
var example: Int {
    enum Example_0 {
        enum Example_1 {
            enum Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        enum Example_0 {
            enum Example_1 {
                enum Example_2 {}
            }
        }
    }
}
extension Example_0 {
    enum Example_1 {
        enum Example_2 {}
    }
}
func f_0() {
    func f_1() {
        func f_2() {
            func f_3() {}
        }
    }
}
var example: Int {
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        func f_0() {
            func f_1() {
                func f_2() {
                    func f_3() {}
                }
            }
        }
    }
}
extension Example_0 {
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
switch example {
case .exampleCase:
    actor Example_0 {
        actor Example_1 {
            actor Example_2 {}
        }
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
var exampleClosure: () -> Void = {
    actor Example_0 {
        actor Example_1 {
            actor Example_2 {}
        }
        }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
exampleFunc(closure: {
    actor Example_0 {
        actor Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
})
switch example {
case .exampleCase:
    class Example_0 {
        class Example_1 {
            class Example_2 {}
        }
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
var exampleClosure: () -> Void = {
    class Example_0 {
        class Example_1 {
            class Example_2 {}
        }
        }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
exampleFunc(closure: {
    class Example_0 {
        class Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
})
switch example {
case .exampleCase:
    struct Example_0 {
        struct Example_1 {
            struct Example_2 {}
        }
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
var exampleClosure: () -> Void = {
    struct Example_0 {
        struct Example_1 {
            struct Example_2 {}
        }
        }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
exampleFunc(closure: {
    struct Example_0 {
        struct Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
})
switch example {
case .exampleCase:
    enum Example_0 {
        enum Example_1 {
            enum Example_2 {}
        }
    }
default:
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
var exampleClosure: () -> Void = {
    enum Example_0 {
        enum Example_1 {
            enum Example_2 {}
        }
        }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
}
exampleFunc(closure: {
    enum Example_0 {
        enum Example_1 {}
    }
    func f_0() {
        func f_1() {
            func f_2() {
                func f_3() {}
            }
        }
    }
})
actor Example_0 {
    actor Example_1 {
        protocol Example_2 {}
    }
}
var example: Int {
    actor Example_0 {
        actor Example_1 {
            protocol Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        actor Example_0 {
            actor Example_1 {
                protocol Example_2 {}
            }
        }
    }
}
extension Example_0 {
    actor Example_1 {
        protocol Example_2 {}
    }
}
class Example_0 {
    class Example_1 {
        protocol Example_2 {}
    }
}
var example: Int {
    class Example_0 {
        class Example_1 {
            protocol Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        class Example_0 {
            class Example_1 {
                protocol Example_2 {}
            }
        }
    }
}
extension Example_0 {
    class Example_1 {
        protocol Example_2 {}
    }
}
struct Example_0 {
    struct Example_1 {
        protocol Example_2 {}
    }
}
var example: Int {
    struct Example_0 {
        struct Example_1 {
            protocol Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        struct Example_0 {
            struct Example_1 {
                protocol Example_2 {}
            }
        }
    }
}
extension Example_0 {
    struct Example_1 {
        protocol Example_2 {}
    }
}
enum Example_0 {
    enum Example_1 {
        protocol Example_2 {}
    }
}
var example: Int {
    enum Example_0 {
        enum Example_1 {
            protocol Example_2 {}
        }
    }
    return 5
}
var example: Int = 5 {
    didSet {
        enum Example_0 {
            enum Example_1 {
                protocol Example_2 {}
            }
        }
    }
}
extension Example_0 {
    enum Example_1 {
        protocol Example_2 {}
    }
}
actor Example_0 {
    func f_0() {
        actor Example_1 {
            func f_1() {
                func f_2() {
                    actor Example_2 {}
                    func f_3() {}
                    protocol P {}
                }
            }
        }
    }
}
actor Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            actor Example_1 {
                func f_1() {
                    func f_2() {
                        actor Example_2 {}
                        func f_3() {}
                        protocol P {}
                    }
                }
            }
        default:
            exampleFunc(closure: {
                actor Example_1 {
                    func f_1() {
                        func f_2() {
                            actor Example_2 {}
                            func f_3() {}
                            protocol P {}
                        }
                    }
                }
            })
        }
    }
}
class Example_0 {
    func f_0() {
        class Example_1 {
            func f_1() {
                func f_2() {
                    class Example_2 {}
                    func f_3() {}
                    protocol P {}
                }
            }
        }
    }
}
class Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            class Example_1 {
                func f_1() {
                    func f_2() {
                        class Example_2 {}
                        func f_3() {}
                        protocol P {}
                    }
                }
            }
        default:
            exampleFunc(closure: {
                class Example_1 {
                    func f_1() {
                        func f_2() {
                            class Example_2 {}
                            func f_3() {}
                            protocol P {}
                        }
                    }
                }
            })
        }
    }
}
struct Example_0 {
    func f_0() {
        struct Example_1 {
            func f_1() {
                func f_2() {
                    struct Example_2 {}
                    func f_3() {}
                    protocol P {}
                }
            }
        }
    }
}
struct Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            struct Example_1 {
                func f_1() {
                    func f_2() {
                        struct Example_2 {}
                        func f_3() {}
                        protocol P {}
                    }
                }
            }
        default:
            exampleFunc(closure: {
                struct Example_1 {
                    func f_1() {
                        func f_2() {
                            struct Example_2 {}
                            func f_3() {}
                            protocol P {}
                        }
                    }
                }
            })
        }
    }
}
enum Example_0 {
    func f_0() {
        enum Example_1 {
            func f_1() {
                func f_2() {
                    enum Example_2 {}
                    func f_3() {}
                    protocol P {}
                }
            }
        }
    }
}
enum Example_0 {
    func f_0() {
        switch example {
        case .exampleCase:
            enum Example_1 {
                func f_1() {
                    func f_2() {
                        enum Example_2 {}
                        func f_3() {}
                        protocol P {}
                    }
                }
            }
        default:
            exampleFunc(closure: {
                enum Example_1 {
                    func f_1() {
                        func f_2() {
                            enum Example_2 {}
                            func f_3() {}
                            protocol P {}
                        }
                    }
                }
            })
        }
    }
}