Skip to content

Commit 6df23a4

Browse files
authored
[Code style] Require explicitly specified types for public properties (#926)
This amends the code style guidelines for the project to require an explicit type for all properties whose access level is `public` or greater, and adjusts the code accordingly. ### Motivation: See the explanation in the code style document for rationale. This topic recently [came up](#915 (comment)) in a PR discussion. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 00bb5ac commit 6df23a4

File tree

6 files changed

+20
-12
lines changed

6 files changed

+20
-12
lines changed

Diff for: Documentation/StyleGuide.md

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public var errorCount: Int {
6363
}
6464
```
6565

66+
Properties, variables, and constants in Swift whose access level is `public` or
67+
greater, or which have the `@usableFromInline` attribute, must have an
68+
explicitly specified type even if they have an initialization expression and the
69+
compiler could infer their type. This is meant to protect against future changes
70+
to the code called by the initialization expression causing the inferred type of
71+
its property to change unknowingly, which could break clients. Properties with
72+
lower access levels may have an inferred type.
73+
6674
Exported C and C++ symbols that are exported should be given the prefix `swt_`
6775
and should otherwise be named using the same lowerCamelCase naming rules as in
6876
Swift. Use the `SWT_EXTERN` macro to ensure that symbols are consistently

Diff for: Sources/Testing/Events/Recorder/Event.ConsoleOutputRecorder.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension Event {
3232
/// On Windows, `GetFileType()` returns `FILE_TYPE_CHAR` for console file
3333
/// handles, and the [Console API](https://learn.microsoft.com/en-us/windows/console/)
3434
/// can be used to perform more complex console operations.
35-
public var useANSIEscapeCodes = false
35+
public var useANSIEscapeCodes: Bool = false
3636

3737
/// The supported color bit depth when adding color to the output using
3838
/// [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code).
@@ -58,7 +58,7 @@ extension Event {
5858
/// If the SF Symbols app is not installed on the system where the
5959
/// output is being rendered, the effect of setting the value of this
6060
/// property to `true` is unspecified.
61-
public var useSFSymbols = false
61+
public var useSFSymbols: Bool = false
6262
#endif
6363

6464
/// Storage for ``tagColors``.

Diff for: Sources/Testing/ExitTests/ExitTestArtifacts.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct ExitTestArtifacts: Sendable {
5555
///
5656
/// If you did not request standard output content when running an exit test,
5757
/// the value of this property is the empty array.
58-
public var standardOutputContent = [UInt8]()
58+
public var standardOutputContent: [UInt8] = []
5959

6060
/// All bytes written to the standard error stream of the exit test before
6161
/// it exited.
@@ -81,7 +81,7 @@ public struct ExitTestArtifacts: Sendable {
8181
///
8282
/// If you did not request standard error content when running an exit test,
8383
/// the value of this property is the empty array.
84-
public var standardErrorContent = [UInt8]()
84+
public var standardErrorContent: [UInt8] = []
8585

8686
@_spi(ForToolsIntegrationOnly)
8787
public init(exitCondition: ExitCondition) {

Diff for: Sources/Testing/Issues/Issue.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public struct Issue: Sendable {
9191

9292
/// Whether or not this issue is known to occur.
9393
@_spi(ForToolsIntegrationOnly)
94-
public var isKnown = false
94+
public var isKnown: Bool = false
9595

9696
/// Initialize an issue instance with the specified details.
9797
///
@@ -254,7 +254,7 @@ extension Issue {
254254
public var sourceContext: SourceContext
255255

256256
/// Whether or not this issue is known to occur.
257-
public var isKnown = false
257+
public var isKnown: Bool = false
258258

259259
/// Initialize an issue instance with the specified details.
260260
///

Diff for: Sources/Testing/Running/Configuration.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct Configuration: Sendable {
1818
// MARK: - Parallelization
1919

2020
/// Whether or not to parallelize the execution of tests and test cases.
21-
public var isParallelizationEnabled = true
21+
public var isParallelizationEnabled: Bool = true
2222

2323
/// How to symbolicate backtraces captured during a test run.
2424
///
@@ -185,7 +185,7 @@ public struct Configuration: Sendable {
185185
/// By default, events of this kind are not delivered to event handlers
186186
/// because they occur frequently in a typical test run and can generate
187187
/// significant backpressure on the event handler.
188-
public var deliverExpectationCheckedEvents = false
188+
public var deliverExpectationCheckedEvents: Bool = false
189189

190190
/// The event handler to which events should be passed when they occur.
191191
public var eventHandler: Event.Handler = { _, _ in }
@@ -237,7 +237,7 @@ public struct Configuration: Sendable {
237237
/// is provided. When the value of this property is less than `0`, some
238238
/// output is suppressed. The exact effects of this property are determined by
239239
/// the instance's event handler.
240-
public var verbosity = 0
240+
public var verbosity: Int = 0
241241

242242
// MARK: - Test selection
243243

@@ -286,7 +286,7 @@ public struct Configuration: Sendable {
286286
/// this maximum count. After this maximum is reached, all subsequent
287287
/// elements are omitted and a single placeholder child is added indicating
288288
/// the number of elements which have been truncated.
289-
public var maximumCollectionCount = 10
289+
public var maximumCollectionCount: Int = 10
290290

291291
/// The maximum depth of children that can be included in the reflection of
292292
/// a checked expectation value.
@@ -303,7 +303,7 @@ public struct Configuration: Sendable {
303303
/// Since optionals are common, the default value of this property is
304304
/// somewhat larger than it otherwise would be in an attempt to make the
305305
/// defaults useful for real-world tests.
306-
public var maximumChildDepth = 10
306+
public var maximumChildDepth: Int = 10
307307
}
308308
}
309309

Diff for: Sources/Testing/Test.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public struct Test: Sendable {
199199
/// being added to the plan. For such suites, the value of this property is
200200
/// `true`.
201201
@_spi(ForToolsIntegrationOnly)
202-
public var isSynthesized = false
202+
public var isSynthesized: Bool = false
203203

204204
/// Initialize an instance of this type representing a test suite type.
205205
init(

0 commit comments

Comments
 (0)