Skip to content

Commit 250a62a

Browse files
committed
Convert some rules to be opt-in because they are noisy for non-strict projects.
The rules that are now opt-in are: - AllPublicDeclarationsHaveDocumentation - NeverForceUnwrap - NeverUseForceTry - NeverUseImplicitlyUnwrappedOptionals The rules that identify unsafe patterns (force unwrap, force try, implicitly unwrapped optionals) aren't able to understand the context of the usage, which means the use might be safe and the rule can't tell. Projects that wish to be more strict can turn on these rules in their configuration, to get back to the original behavior. There is also no change for projects that are already using a configuration file.
1 parent 5e7c5b7 commit 250a62a

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

Sources/SwiftFormat/Pipelines+Generated.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class LintPipeline: SyntaxVisitor {
4242

4343
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
4444
visitIfEnabled(AllPublicDeclarationsHaveDocumentation.visit, in: context, for: node)
45+
visitIfEnabled(AlwaysUseLowerCamelCase.visit, in: context, for: node)
4546
visitIfEnabled(BeginDocumentationCommentWithOneLineSummary.visit, in: context, for: node)
4647
visitIfEnabled(DontRepeatTypeInStaticProperties.visit, in: context, for: node)
4748
visitIfEnabled(NoLeadingUnderscores.visit, in: context, for: node)
@@ -205,6 +206,7 @@ class LintPipeline: SyntaxVisitor {
205206
}
206207

207208
override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
209+
visitIfEnabled(AlwaysUseLowerCamelCase.visit, in: context, for: node)
208210
visitIfEnabled(AmbiguousTrailingClosureOverload.visit, in: context, for: node)
209211
visitIfEnabled(FileScopedDeclarationPrivacy.visit, in: context, for: node)
210212
visitIfEnabled(NeverForceUnwrap.visit, in: context, for: node)

Sources/SwiftFormatConfiguration/RuleRegistry+Generated.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
enum RuleRegistry {
1616
static let rules: [String: Bool] = [
17-
"AllPublicDeclarationsHaveDocumentation": true,
17+
"AllPublicDeclarationsHaveDocumentation": false,
1818
"AlwaysUseLowerCamelCase": true,
1919
"AmbiguousTrailingClosureOverload": true,
2020
"BeginDocumentationCommentWithOneLineSummary": true,
@@ -24,9 +24,9 @@ enum RuleRegistry {
2424
"FullyIndirectEnum": true,
2525
"GroupNumericLiterals": true,
2626
"IdentifiersMustBeASCII": true,
27-
"NeverForceUnwrap": true,
28-
"NeverUseForceTry": true,
29-
"NeverUseImplicitlyUnwrappedOptionals": true,
27+
"NeverForceUnwrap": false,
28+
"NeverUseForceTry": false,
29+
"NeverUseImplicitlyUnwrappedOptionals": false,
3030
"NoAccessLevelOnExtensionDeclaration": true,
3131
"NoBlockComments": true,
3232
"NoCasesWithOnlyFallthrough": true,

Sources/SwiftFormatRules/AllPublicDeclarationsHaveDocumentation.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import SwiftSyntax
1818
/// Lint: If a public declaration is missing a documentation comment, a lint error is raised.
1919
public final class AllPublicDeclarationsHaveDocumentation: SyntaxLintRule {
2020

21+
/// Identifies this rule was being opt-in. While docs on most public declarations are beneficial,
22+
/// there are a number of public decls where docs are either redundant or superfluous. This rule
23+
/// can't differentiate those situations and will make a lot of noise for projects that are
24+
/// intentionally avoiding docs on some decls.
25+
public override class var isOptIn: Bool { return true }
26+
2127
public override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
2228
diagnoseMissingDocComment(DeclSyntax(node), name: node.fullDeclName, modifiers: node.modifiers)
2329
return .skipChildren

Sources/SwiftFormatRules/NeverForceUnwrap.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import SwiftSyntax
1818
/// Lint: If a force unwrap is used, a lint warning is raised.
1919
public final class NeverForceUnwrap: SyntaxLintRule {
2020

21+
/// Identifies this rule was being opt-in. While force unwrap is an unsafe pattern (i.e. it can
22+
/// crash), there are valid contexts for force unwrap where it won't crash. This rule can't
23+
/// evaluate the context around the force unwrap to make that determination.
24+
public override class var isOptIn: Bool { return true }
25+
2126
public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
2227
// Tracks whether "XCTest" is imported in the source file before processing individual nodes.
2328
setImportsXCTest(context: context, sourceFile: node)

Sources/SwiftFormatRules/NeverUseForceTry.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import SwiftSyntax
2323
/// TODO: Create exception for NSRegularExpression
2424
public final class NeverUseForceTry: SyntaxLintRule {
2525

26+
/// Identifies this rule was being opt-in. While force try is an unsafe pattern (i.e. it can
27+
/// crash), there are valid contexts for force try where it won't crash. This rule can't
28+
/// evaluate the context around the force try to make that determination.
29+
public override class var isOptIn: Bool { return true }
30+
2631
public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
2732
setImportsXCTest(context: context, sourceFile: node)
2833
return .visitChildren

Sources/SwiftFormatRules/NeverUseImplicitlyUnwrappedOptionals.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import SwiftSyntax
2525
/// Lint: Declaring a property with an implicitly unwrapped type yields a lint error.
2626
public final class NeverUseImplicitlyUnwrappedOptionals: SyntaxLintRule {
2727

28+
/// Identifies this rule was being opt-in. While accessing implicitly unwrapped optionals is an
29+
/// unsafe pattern (i.e. it can crash), there are valid contexts for using implicitly unwrapped
30+
/// optionals where it won't crash. This rule can't evaluate the context around the usage to make
31+
/// that determination.
32+
public override class var isOptIn: Bool { return true }
33+
2834
// Checks if "XCTest" is an import statement
2935
public override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
3036
setImportsXCTest(context: context, sourceFile: node)

0 commit comments

Comments
 (0)