realm / realm/SwiftLint

`nesting` rule no longer catches a class nested in a function

Open
#3,496 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
Describe the bug

Updated issue - I think the behaviour I'm seeing is that a class in a function is not triggering when expected.

Existing nesting rule behaviour has changed so that existing violations for a class nested in a function are no longer triggered. Previously was on 0.40.3 and issue appeared after updating to 0.42.

class Nested {
    func functionWithNestedClass() {
        class ClassNestedInFunction { // expected violation - class is 2 levels deep
            class AnotherNestedClass {} // Nesting Violation: Types should be nested at most 1 level deep (nesting)
        }    
    }
}
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint --path NestedCode.swift

Linting Swift files at paths NestedCode.swift
Linting 'NestedCode.swift' (1/1)
.../NestedCode.swift:21:1: warning: Line Length Violation: Line should be 120 characters or less: currently 131 characters (line_length)
.../NestedCode.swift:12:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
.../NestedCode.swift:12:9: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
.../NestedCode.swift:21:17: warning: Nesting Violation: Functions should be nested at most 2 levels deep (nesting)
.../NestedCode.swift:25:13: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
Done linting! Found 5 violations, 0 serious in 1 file.
Environment
  • SwiftLint version (run swiftlint version to be sure)? 0.42
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Package install and Portable
  • Paste your configuration file:

Issue is reproducible without configuration file. Tested with:

nesting:
  type_level: 1
  function_level: 2
  check_nesting_in_closures_and_statements: true
  always_allow_one_type_in_functions: false

Which matches the documented defaults and reproduces the same behaviour as with no configuration file.

  • Are you using nested configurations? No
    If so, paste their relative paths and respective contents.
  • Which Xcode version are you using (check xcodebuild -version)? Xcode 12.3
  • Do you have a sample that shows the issue?
class Nesting { // level 0
    typealias FooBar = String
    class Nested { // level 1
        class NestedAgain { // level 2 - Nesting Violation: Types should be nested at most 1 level deep (nesting)
            func bar() {}
        }
        func foo() {}
    }

    func functionWithNestedClass() {
        class ClassNestedInFunction { // expected violation - class is 2 levels deep
            class AnotherNestedClass {} // actual violation - Nesting Violation: Types should be nested at most 1 level deep (nesting)
        }
    }

    func function() { // level 1
        func nestedFunction() { // level 2
            func anotherNestedFunction() { // level 3
                func yetAnotherNestedFunction() {} // level 4 - Nesting Violation: Functions should be nested at most 2 levels deep (nesting)
            }
        }
        class FunctionNested { // level 2 - would expect violation here but a class inside a function appears to get the same permitted level as a function
            class NestedAgain { // level 3 - Nesting Violation: Types should be nested at most 1 level deep (nesting)
            }
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the nesting rule and the provided NestedCode.swift sample, then run swiftlint lint --path NestedCode.swift with the documented configuration. Done means a class nested in a function is reported when it exceeds the configured type nesting level, while the existing nesting checks continue to report the other shown violations.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.