Skip to content

unused_import false positive for import OSLog when using os.Logger #6942

Open

Description

@iliaskarim

New Issue Checklist

Reproduced on 0.62.2. The same comparison is still in UnusedImportRule on 0.65.1: used modules are SourceKit's defining module name, subtracted from the import list. No open issue mentions OSLog / os.Logger. This is the same gap as #5167 (import SwiftUI vs Combine.ObservableObject) and #5528 (@_exported import).

Bug Description

unused_import reports import OSLog as unused in a file that uses os.Logger. Deleting that import makes the file fail to compile, which is the opposite of the rule description ("All imported modules should be required to make the file compile").

os.Logger is declared in the Swift module os (os.swiftinterface, -module-name os, public struct Logger). The compiler mangles it as $s2os6LoggerV.... import OSLog is a different module, and it is still what brings Logger into scope. SourceKit cursor info reports key.modulename as os. The rule inserts that name into the used-module set and does imports.subtracting(usedModules), so OSLog is left over. Nothing in the file is attributed to OSLog.

import OSLog

let logger = os.Logger(subsystem: "example", category: "bug")
$ swiftc -typecheck Example.swift
# succeeds

$ # delete `import OSLog`
$ swiftc -typecheck Example.swift
Example.swift:1:14: error: cannot find type 'os' in scope

swiftlint analyze (0.62.2, macOS, Xcode 26.6) on a file that contains the snippet above:

Example.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)

import Foundation alone does not provide os.Logger. allowed_transitive_imports can paper over this, but the default rule should not flag an import whose removal breaks the file:

unused_import:
  allowed_transitive_imports:
    - module: OSLog
      allowed_transitive_imports:
        - os

Environment

  • SwiftLint version: 0.62.2 (rule source checked against 0.65.1)
  • Xcode version: Xcode 26.6 (Build version 17F113), Swift 6.3.3
  • Installation method used: Homebrew
  • Configuration file:
analyzer_rules:
  - unused_import

Nested configurations: no.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions