Skip to content

Commit 0527cf0

Browse files
committed
fix: dont panic when no linters are specified
1 parent 0765237 commit 0527cf0

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ pub fn do_lint(
165165
"Running linters: {:?}",
166166
linters.iter().map(|l| &l.code).collect::<Vec<_>>()
167167
);
168+
let mut stdout = Term::stdout();
169+
if linters.is_empty() {
170+
stdout.write_line("No linters ran.")?;
171+
return Ok(0);
172+
}
168173

169174
let mut files = match paths_opt {
170175
PathsOpt::Auto => {
@@ -261,8 +266,6 @@ pub fn do_lint(
261266
// Flush the logger before rendering results.
262267
log::logger().flush();
263268

264-
let mut stdout = Term::stdout();
265-
266269
let did_print = match render_opt {
267270
RenderOpt::Default => render_lint_messages(&mut stdout, &all_lints)?,
268271
RenderOpt::Json => render_lint_messages_json(&mut stdout, &all_lints)?,

tests/integration_test.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,42 @@ fn linter_replacement_trailing_newlines() -> Result<()> {
878878

879879
Ok(())
880880
}
881+
882+
#[test]
883+
fn lint_with_no_linters() -> Result<()> {
884+
let data_path = tempfile::tempdir()?;
885+
let lint_message = LintMessage {
886+
path: Some("tests/fixtures/fake_source_file.rs".to_string()),
887+
line: Some(9),
888+
char: Some(1),
889+
code: "DUMMY".to_string(),
890+
name: "dummy failure".to_string(),
891+
severity: LintSeverity::Advice,
892+
original: None,
893+
replacement: None,
894+
description: Some("A dummy linter failure".to_string()),
895+
};
896+
let config = temp_config(&format!(
897+
"\
898+
[[linter]]
899+
code = 'TESTLINTER'
900+
include_patterns = ['**']
901+
command = ['echo', '{}']
902+
",
903+
serde_json::to_string(&lint_message)?
904+
))?;
905+
906+
let mut cmd = Command::cargo_bin("lintrunner")?;
907+
cmd.arg(format!("--config={}", config.path().to_str().unwrap()));
908+
cmd.arg(format!(
909+
"--data-path={}",
910+
data_path.path().to_str().unwrap()
911+
));
912+
913+
// Run on a file to ensure that the linter is run.
914+
cmd.arg("--skip=TESTLINTER");
915+
cmd.assert().success();
916+
assert_output_snapshot("lint_with_no_linters", &mut cmd)?;
917+
918+
Ok(())
919+
}

tests/snapshots/integration_test__format_command_doesnt_use_nonformat_linter.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
---
22
source: tests/integration_test.rs
3-
assertion_line: 20
43
expression: output_lines
54
---
65
- "STDOUT:"
7-
- ok No lint issues.
8-
- Successfully applied all patches.
6+
- No linters ran.
97
- ""
108
- ""
119
- "STDERR:"

0 commit comments

Comments
 (0)