Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive inert_defer with condition compilation #4615

Closed
2 tasks done
yuri-qualtie opened this issue Dec 2, 2022 · 3 comments · Fixed by #4617
Closed
2 tasks done

False positive inert_defer with condition compilation #4615

yuri-qualtie opened this issue Dec 2, 2022 · 3 comments · Fixed by #4617
Labels
bug Unexpected and reproducible misbehavior.

Comments

@yuri-qualtie
Copy link

yuri-qualtie commented Dec 2, 2022

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
    }
}
@processed-bits
Copy link

Also false positive:

let task = Task {
	#if DEBUG
		let stopwatch = Stopwatch()
		defer { Logger.default.debug("\(name ?? "Tool"): \(stopwatch).") }
	#endif
	// …
}

@SimplyDanny SimplyDanny added the bug Unexpected and reproducible misbehavior. label Dec 3, 2022
@SimplyDanny
Copy link
Collaborator

The rule seems to be obsolete now given that the compiler warns already:
Screenshot 2022-12-04 at 09 22 43

@SimplyDanny
Copy link
Collaborator

The the rule will be considered deprecated in the next release. Nevertheless, the bug has also been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants