Skip to content

Make linter emit warnings instead of errors by default #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Sources/swift-format/Frontend/Frontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,21 @@ class Frontend {
/// Creates a new frontend with the given options.
///
/// - Parameter lintFormatOptions: Options that apply during formatting or linting.
init(configurationOptions: ConfigurationOptions, lintFormatOptions: LintFormatOptions) {
init(
configurationOptions: ConfigurationOptions,
lintFormatOptions: LintFormatOptions,
treatWarningsAsErrors: Bool = false
) {
self.configurationOptions = configurationOptions
self.lintFormatOptions = lintFormatOptions

self.diagnosticPrinter = StderrDiagnosticPrinter(
colorMode: lintFormatOptions.colorDiagnostics.map { $0 ? .on : .off } ?? .auto
)
self.diagnosticsEngine = DiagnosticsEngine(diagnosticsHandlers: [diagnosticPrinter.printDiagnostic])
self.diagnosticsEngine = DiagnosticsEngine(
diagnosticsHandlers: [diagnosticPrinter.printDiagnostic],
treatWarningsAsErrors: treatWarningsAsErrors
)
self.configurationProvider = ConfigurationProvider(diagnosticsEngine: self.diagnosticsEngine)
}

Expand Down
16 changes: 6 additions & 10 deletions Sources/swift-format/Subcommands/Lint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension SwiftFormatCommand {

@Flag(
name: .shortAndLong,
help: "Fail on warnings. Deprecated: All findings are treated as errors now."
help: "Treat all findings as errors instead of warnings."
)
var strict: Bool = false

Expand All @@ -37,15 +37,11 @@ extension SwiftFormatCommand {

func run() throws {
try performanceMeasurementOptions.printingInstructionCountIfRequested {
let frontend = LintFrontend(configurationOptions: configurationOptions, lintFormatOptions: lintOptions)

if strict {
frontend.diagnosticsEngine.emitWarning(
"""
Running swift-format with --strict is deprecated and will be removed in the future.
"""
)
}
let frontend = LintFrontend(
configurationOptions: configurationOptions,
lintFormatOptions: lintOptions,
treatWarningsAsErrors: strict
)
frontend.run()

if frontend.diagnosticsEngine.hasErrors {
Expand Down
12 changes: 10 additions & 2 deletions Sources/swift-format/Utilities/DiagnosticsEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,28 @@ final class DiagnosticsEngine {
/// A Boolean value indicating whether any warnings were emitted by the diagnostics engine.
private(set) var hasWarnings: Bool

/// Whether to upgrade all warnings to errors.
private let treatWarningsAsErrors: Bool

/// Creates a new diagnostics engine with the given diagnostic handlers.
///
/// - Parameter diagnosticsHandlers: An array of functions, each of which takes a `Diagnostic` as
/// its sole argument and returns `Void`. The functions are called whenever a diagnostic is
/// received by the engine.
init(diagnosticsHandlers: [(Diagnostic) -> Void]) {
init(diagnosticsHandlers: [(Diagnostic) -> Void], treatWarningsAsErrors: Bool = false) {
self.handlers = diagnosticsHandlers
self.hasErrors = false
self.hasWarnings = false
self.treatWarningsAsErrors = treatWarningsAsErrors
}

/// Emits the diagnostic by passing it to the registered handlers, and tracks whether it was an
/// error or warning diagnostic.
private func emit(_ diagnostic: Diagnostic) {
var diagnostic = diagnostic
if treatWarningsAsErrors, diagnostic.severity == .warning {
diagnostic.severity = .error
}
switch diagnostic.severity {
case .error: self.hasErrors = true
case .warning: self.hasWarnings = true
Expand Down Expand Up @@ -135,7 +143,7 @@ final class DiagnosticsEngine {
/// diagnostics engine and returns it.
private func diagnosticMessage(for finding: Finding) -> Diagnostic {
return Diagnostic(
severity: .error,
severity: .warning,
location: finding.location.map(Diagnostic.Location.init),
category: "\(finding.category)",
message: "\(finding.message.text)"
Expand Down