Skip to content

Commit f60f4eb

Browse files
committed
Make linter emit warnings instead of errors by default
When running `swift-format lint` in an Xcode run script phase and it exits with a non-zero exit code, the build will fail. This started happening since we treated all linter findings as errors in swiftlang#943. To fix this: - Diagnose all findings as warnings again and exit with code 0 even if there are findings - Resurrect `--strict` to treat all findings as errors and exit with 1 if there were any findings. rdar://148389716
1 parent 3dc9b78 commit f60f4eb

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Sources/swift-format/Frontend/Frontend.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,21 @@ class Frontend {
200200
/// Creates a new frontend with the given options.
201201
///
202202
/// - Parameter lintFormatOptions: Options that apply during formatting or linting.
203-
init(configurationOptions: ConfigurationOptions, lintFormatOptions: LintFormatOptions) {
203+
init(
204+
configurationOptions: ConfigurationOptions,
205+
lintFormatOptions: LintFormatOptions,
206+
treatWarningsAsErrors: Bool = false
207+
) {
204208
self.configurationOptions = configurationOptions
205209
self.lintFormatOptions = lintFormatOptions
206210

207211
self.diagnosticPrinter = StderrDiagnosticPrinter(
208212
colorMode: lintFormatOptions.colorDiagnostics.map { $0 ? .on : .off } ?? .auto
209213
)
210-
self.diagnosticsEngine = DiagnosticsEngine(diagnosticsHandlers: [diagnosticPrinter.printDiagnostic])
214+
self.diagnosticsEngine = DiagnosticsEngine(
215+
diagnosticsHandlers: [diagnosticPrinter.printDiagnostic],
216+
treatWarningsAsErrors: treatWarningsAsErrors
217+
)
211218
self.configurationProvider = ConfigurationProvider(diagnosticsEngine: self.diagnosticsEngine)
212219
}
213220

Sources/swift-format/Subcommands/Lint.swift

+6-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension SwiftFormatCommand {
2828

2929
@Flag(
3030
name: .shortAndLong,
31-
help: "Fail on warnings. Deprecated: All findings are treated as errors now."
31+
help: "Treat all findings as errors instead of warnings."
3232
)
3333
var strict: Bool = false
3434

@@ -37,15 +37,11 @@ extension SwiftFormatCommand {
3737

3838
func run() throws {
3939
try performanceMeasurementOptions.printingInstructionCountIfRequested {
40-
let frontend = LintFrontend(configurationOptions: configurationOptions, lintFormatOptions: lintOptions)
41-
42-
if strict {
43-
frontend.diagnosticsEngine.emitWarning(
44-
"""
45-
Running swift-format with --strict is deprecated and will be removed in the future.
46-
"""
47-
)
48-
}
40+
let frontend = LintFrontend(
41+
configurationOptions: configurationOptions,
42+
lintFormatOptions: lintOptions,
43+
treatWarningsAsErrors: strict
44+
)
4945
frontend.run()
5046

5147
if frontend.diagnosticsEngine.hasErrors {

Sources/swift-format/Utilities/DiagnosticsEngine.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,28 @@ final class DiagnosticsEngine {
2626
/// A Boolean value indicating whether any warnings were emitted by the diagnostics engine.
2727
private(set) var hasWarnings: Bool
2828

29+
/// Whether to upgrade all warnings to errors.
30+
private let treatWarningsAsErrors: Bool
31+
2932
/// Creates a new diagnostics engine with the given diagnostic handlers.
3033
///
3134
/// - Parameter diagnosticsHandlers: An array of functions, each of which takes a `Diagnostic` as
3235
/// its sole argument and returns `Void`. The functions are called whenever a diagnostic is
3336
/// received by the engine.
34-
init(diagnosticsHandlers: [(Diagnostic) -> Void]) {
37+
init(diagnosticsHandlers: [(Diagnostic) -> Void], treatWarningsAsErrors: Bool = false) {
3538
self.handlers = diagnosticsHandlers
3639
self.hasErrors = false
3740
self.hasWarnings = false
41+
self.treatWarningsAsErrors = treatWarningsAsErrors
3842
}
3943

4044
/// Emits the diagnostic by passing it to the registered handlers, and tracks whether it was an
4145
/// error or warning diagnostic.
4246
private func emit(_ diagnostic: Diagnostic) {
47+
var diagnostic = diagnostic
48+
if treatWarningsAsErrors, diagnostic.severity == .warning {
49+
diagnostic.severity = .error
50+
}
4351
switch diagnostic.severity {
4452
case .error: self.hasErrors = true
4553
case .warning: self.hasWarnings = true
@@ -135,7 +143,7 @@ final class DiagnosticsEngine {
135143
/// diagnostics engine and returns it.
136144
private func diagnosticMessage(for finding: Finding) -> Diagnostic {
137145
return Diagnostic(
138-
severity: .error,
146+
severity: .warning,
139147
location: finding.location.map(Diagnostic.Location.init),
140148
category: "\(finding.category)",
141149
message: "\(finding.message.text)"

0 commit comments

Comments
 (0)