Skip to content

False positive inert_defer with condition compilation #4615

@yuri-qualtie

Description

@yuri-qualtie

New Issue Checklist

Describe the bug

Rule inert_defer is false positive when defer is wrapped in condition compilation. For example: #if os(macOS)

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint Test.swift

Linting Swift files at paths Test.swift
Linting 'Test.swift' (1/1)
.../Test.swift:6:9: warning: Inert Defer Violation: If defer is at the end of its parent scope, it will be executed right where it is anyway. (inert_defer)
Done linting! Found 1 violation, 0 serious in 1 file.

Environment

  • SwiftLint version - 0.50.1

  • Installation method used - Homebrew

  • Paste your configuration file - default

  • Xcode version - Xcode 14.0.1 Build version 14A400

  • Run - swiftlint lint --path Test.swift --no-cache --enable-all-rules

../Test.swift:4:5: warning: Explicit ACL Violation: All declarations should specify Access Control Level keywords explicitly. (explicit_acl)
../Test.swift:6:9: warning: Inert Defer Violation: If defer is at the end of its parent scope, it will be executed right where it is anyway. (inert_defer)
../Test.swift:3:8: warning: Missing Docs Violation: public declarations should be documented. (missing_docs)
../Test.swift:3:8: warning: Required Deinit Violation: Classes should have an explicit deinit method. (required_deinit)
public class Test {
    func hello(name: String) -> String {
        #if os(macOS)
        // this trigger violation: Inert Defer Violation: If defer is at the end of its parent scope, it will be executed right where it is anyway. (inert_defer)
        defer {
            print(self)
        }
        #endif

        print(name)

        return "Hello " + name
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUnexpected and reproducible misbehavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions