Repository navigation
Description
New Issue Checklist
- I've Updated SwiftLint to the latest version.
- I've searched for existing GitHub issues.
Bug Description
All five triggeringExamples of orphaned_doc_comment have the same shape: a doc comment, an ordinary // comment, then a declaration. The rule assumes that the ordinary comment detaches the doc comment from the declaration. That stopped being true in swiftlang/swift#66136 ("[AST] don't break a doc comment with a regular comment", merged 2023-05-30, shipped in Swift 5.10 / Xcode 15.3), which changed comment attachment to skip over ordinary comments instead of breaking on them:
if (SRC.isOrdinary()) {
// If there's a regular comment just skip it
LastEnd = End;
continue;
}lib/AST/RawComment.cpp#L107-L111 — before that PR this branch was Comments.clear(), i.e. the rule was correct when it was written (Swift ≤ 5.9), and is not anymore.
So today the rule warns about doc comments that are properly attached and rendered — the exact opposite of "A doc comment should be attached to a declaration".
Reproducing
Examples.swift — the rule's own triggering examples, made compilable, plus a control:
/// Baseline doc comment (control).
public var control: String! = nil
/// My great property
// Not a doc string
public var example1: String! = nil
/// Look here for more info: https://github.com.
// Not a doc string
public var example2: String! = nil
/// Look here for more info: https://github.com.
// Not a doc string
public var example3: String! = nil
/// Look here for more info: https://github.com.
// Not a doc string
/// My great property
// Not a doc string
public var example4: String! = nil
public extension Nested {
///
/// Look here for more info: https://github.com.
// Not a doc string
var example5: String! { nil }
}
public struct Nested { public init() {} }SwiftLint reports a violation for each of them:
$ swiftlint lint --no-cache --quiet --config .swiftlint.yml Examples.swift
Examples.swift:4:1: warning: Orphaned Doc Comment Violation: A doc comment should be attached to a declaration (orphaned_doc_comment)
Examples.swift:8:1: warning: Orphaned Doc Comment Violation: A doc comment should be attached to a declaration (orphaned_doc_comment)
Examples.swift:12:1: warning: Orphaned Doc Comment Violation: A doc comment should be attached to a declaration (orphaned_doc_comment)
Examples.swift:18:1: warning: Orphaned Doc Comment Violation: A doc comment should be attached to a declaration (orphaned_doc_comment)
Examples.swift:20:1: warning: Orphaned Doc Comment Violation: A doc comment should be attached to a declaration (orphaned_doc_comment)
Examples.swift:25:5: warning: Orphaned Doc Comment Violation: A doc comment should be attached to a declaration (orphaned_doc_comment)The compiler, however, attaches every single one of them. Asking it directly via the symbol graph (the same RawComment machinery that feeds SourceKit's Quick Help and DocC — swiftlang/swift#66136 had to update test/SourceKit/DocSupport precisely because of this):
$ swiftc -emit-module -module-name ODC -emit-symbol-graph -emit-symbol-graph-dir sg Examples.swift
$ jq -r '.symbols[] | "\(.names.title): \((.docComment.lines // []) | map(.text) | @json)"' sg/ODC.symbols.json
control: ["Baseline doc comment (control)."]
example1: ["My great property"]
example2: ["Look here for more info: https://github.com."]
example3: ["Look here for more info: https://github.com."]
example4: ["Look here for more info: https://github.com.","My great property"]
example5: ["","Look here for more info: https://github.com."]Note example4: both doc comments survive and are merged into one documentation block.
The rule's original motivation in #2989 — a swiftlint:disable:next between a doc comment and its declaration losing the documentation — no longer holds either:
/// The #2989 motivation.
// swiftlint:disable:next force_unwrapping
public var caseD: String! = nilcaseD: ["The #2989 motivation."] // attached, but flagged by the rule
The mirror image: a real orphan the rule misses
Under the current attachment rules, an ordinary comment is harmless, but a blank line between two doc comment groups drops the earlier group (if (!Comments.empty() && Start > LastEnd + 1) Comments.clear();):
/// Doc comment.
/// Another doc comment.
public var caseC: String! = nilcaseC: ["Another doc comment."]
/// Doc comment. is genuinely lost here, and orphaned_doc_comment reports nothing for this snippet — isOrphanedDocComment only returns true when it meets a .lineComment/.blockComment, so it walks over the newlines and the second doc comment and returns false.
So the rule is currently inverted with respect to the compiler: it flags the attached case and ignores the detached one.
Proposal
Either:
- Re-scope the rule to the current attachment rules. A doc comment is orphaned when it is superseded by a later doc comment group separated by a blank line, or when no declaration follows it at all. All five current triggering examples then become non-triggering examples.
- Or deprecate the rule, since its founding premise (an ordinary comment breaks documentation) has not been true since Swift 5.10.
Happy to send a PR for either direction — just let me know which one you'd prefer.
(local_doc_comment is unaffected — different concern.)
Environment
- SwiftLint version 0.65.0 (
OrphanedDocCommentRuleis unchanged in 0.65.1 and onmain— the only diff since is the#examplesmacro migration in Add#examplesand#correctionsmacros to DRY up rule description #6794) - Xcode 26.6 (Build 17F113), Apple Swift version 6.3.3 (swiftlang-6.3.3.1.3)
- macOS 26.5, Apple Silicon
- Installation method used: mise (prebuilt release binary)
- Configuration file:
only_rules:
- orphaned_doc_commentNo nested configurations.
Activity
SimplyDanny commentedon Aug 29, 2026
Thanks for the detailed analysis. I do partially agree with your statements. The rule should trigger on the first doc comment in
as well. However, I still think that the doc comment in
is still orphan -- not according to compiler behavior, but in terms of readability. Even though the compiler treats it as a doc comment, a reader would probably not consider it as such. There went something wrong definitely and it's worth pointing users to that.
The
case is a little special and could be considered "okay". However, there are other ways to add disable commands that would make it clearer. That's why I think that even in this case, the rule should continue triggering.