Skip to content

Commit 4dd4884

Browse files
gvozdvmozgubenfdking
authored andcommitted
refactor: simplify lint exit codes and remove formatter failure state
1 parent dd5c6e6 commit 4dd4884

File tree

7 files changed

+21
-45
lines changed

7 files changed

+21
-45
lines changed

Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ perf = "warn"
1717
cloned_instead_of_copied = "warn"
1818

1919
# https://github.com/rustwasm/wasm-bindgen/issues/3451#issuecomment-1562982835
20-
#[profile.dev]
21-
#opt-level = "s"
20+
[profile.dev]
21+
opt-level = "s"
2222

23-
#[profile.release]
24-
#lto = true
25-
#codegen-units = 1
23+
[profile.release]
24+
lto = true
25+
codegen-units = 1
2626

2727
[workspace.dependencies]
2828
ahash = { version = "0.8.11", features = ["compile-time-rng", "serde"] }

crates/cli-lib/src/commands_lint.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ pub(crate) fn run_lint(
1111
) -> i32 {
1212
let LintArgs { paths, format } = args;
1313
let mut linter = linter(config, format, collect_parse_errors);
14-
15-
linter.lint_paths(paths, false, &ignorer);
14+
let result = linter.lint_paths(paths, false, &ignorer);
1615

1716
linter.formatter().unwrap().completion_message();
18-
if linter.formatter().unwrap().has_fail() {
19-
1
20-
} else {
21-
0
22-
}
17+
18+
result.has_violations() as i32
2319
}
2420

2521
pub(crate) fn run_lint_stdin(
@@ -30,13 +26,9 @@ pub(crate) fn run_lint_stdin(
3026
let read_in = crate::stdin::read_std_in().unwrap();
3127

3228
let linter = linter(config, format, collect_parse_errors);
33-
linter.lint_string(&read_in, None, false);
29+
let result = linter.lint_string(&read_in, None, false);
3430

3531
linter.formatter().unwrap().completion_message();
3632

37-
if linter.formatter().unwrap().has_fail() {
38-
1
39-
} else {
40-
0
41-
}
33+
result.has_violations() as i32
4234
}

crates/lib/src/cli/formatters.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::utils::*;
22
use std::borrow::Cow;
33
use std::io::{Stderr, Write};
4-
use std::sync::atomic::{AtomicBool, AtomicUsize};
4+
use std::sync::atomic::AtomicUsize;
55

66
use anstyle::{AnsiColor, Effects, Style};
77
use itertools::enumerate;
@@ -24,8 +24,6 @@ pub trait Formatter: Send + Sync {
2424

2525
fn dispatch_file_violations(&self, linted_file: &LintedFile, only_fixable: bool);
2626

27-
fn has_fail(&self) -> bool;
28-
2927
fn completion_message(&self);
3028
}
3129

@@ -35,7 +33,6 @@ pub struct OutputStreamFormatter {
3533
filter_empty: bool,
3634
verbosity: i32,
3735
output_line_length: usize,
38-
pub has_fail: AtomicBool,
3936
files_dispatched: AtomicUsize,
4037
}
4138

@@ -55,10 +52,6 @@ impl Formatter for OutputStreamFormatter {
5552
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
5653
}
5754

58-
fn has_fail(&self) -> bool {
59-
self.has_fail.load(std::sync::atomic::Ordering::SeqCst)
60-
}
61-
6255
fn completion_message(&self) {
6356
let count = self
6457
.files_dispatched
@@ -92,7 +85,6 @@ impl OutputStreamFormatter {
9285
filter_empty: true,
9386
verbosity,
9487
output_line_length: 80,
95-
has_fail: false.into(),
9688
files_dispatched: 0.into(),
9789
}
9890
}
@@ -146,11 +138,7 @@ impl OutputStreamFormatter {
146138

147139
let color = match status {
148140
Status::Pass | Status::Fixed => AnsiColor::Green,
149-
Status::Fail | Status::Error => {
150-
self.has_fail
151-
.store(true, std::sync::atomic::Ordering::SeqCst);
152-
AnsiColor::Red
153-
}
141+
Status::Fail | Status::Error => AnsiColor::Red,
154142
}
155143
.on_default();
156144

crates/lib/src/cli/github_annotation_native_formatter.rs

-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ impl Formatter for GithubAnnotationNativeFormatter {
7070
}
7171
}
7272

73-
fn has_fail(&self) -> bool {
74-
self.has_fail.load(Ordering::SeqCst)
75-
}
76-
7773
fn completion_message(&self) {
7874
// No-op
7975
}

crates/lib/src/cli/json.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::core::{config::FluffConfig, linter::linted_file::LintedFile};
44

55
use super::{
66
formatters::Formatter,
7-
json_types::{Diagnostic, DiagnosticCollection, DiagnosticSeverity},
7+
json_types::{Diagnostic, DiagnosticCollection},
88
};
99

1010
#[derive(Default)]
@@ -24,14 +24,6 @@ impl Formatter for JsonFormatter {
2424
);
2525
}
2626

27-
fn has_fail(&self) -> bool {
28-
let lock = self.violations.lock().unwrap();
29-
lock.values().any(|v| {
30-
v.iter()
31-
.any(|d| matches!(&d.severity, DiagnosticSeverity::Warning))
32-
})
33-
}
34-
3527
fn completion_message(&self) {
3628
let lock = self.violations.lock().unwrap();
3729
let json = serde_json::to_string(&*lock).unwrap();

crates/lib/src/core/linter/linted_file.rs

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub struct LintedFile {
1717
}
1818

1919
impl LintedFile {
20+
pub fn has_violations(&self) -> bool {
21+
!self.violations.is_empty()
22+
}
23+
2024
pub fn get_violations(&self, is_fixable: Option<bool>) -> Vec<SQLBaseError> {
2125
if let Some(is_fixable) = is_fixable {
2226
self.violations

crates/lib/src/core/linter/linting_result.rs

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ impl LintingResult {
1616
LintingResult { files: vec![] }
1717
}
1818

19+
pub fn has_violations(&self) -> bool {
20+
self.files.iter().any(|file| file.has_violations())
21+
}
22+
1923
/// Add a new `LintedDir` to this result.
2024
pub fn add(&mut self, path: LintedFile) -> usize {
2125
let idx = self.files.len();

0 commit comments

Comments
 (0)