Skip to content

Commit 89064c1

Browse files
committed
fix: error if duplicate linters found
1 parent 1018103 commit 89064c1

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/lint_config.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,17 @@ pub fn get_linters_from_config(
124124
config_path: &AbsPath,
125125
) -> Result<Vec<Linter>> {
126126
let mut linters = Vec::new();
127+
let mut all_linters: HashSet<String> = HashSet::new();
128+
127129
for lint_config in linter_configs {
130+
if all_linters.contains(&lint_config.code) {
131+
bail!(
132+
"Invalid linter configuration: linter '{}' is defined multiple times.",
133+
lint_config.code
134+
);
135+
}
136+
all_linters.insert(lint_config.code.clone());
137+
128138
let include_patterns = patterns_from_strs(&lint_config.include_patterns)?;
129139
let exclude_patterns = if let Some(exclude_patterns) = &lint_config.exclude_patterns {
130140
patterns_from_strs(exclude_patterns)?
@@ -137,6 +147,7 @@ pub fn get_linters_from_config(
137147
"Invalid linter configuration: '{}' has an empty command list.",
138148
lint_config.code
139149
);
150+
140151
linters.push(Linter {
141152
code: lint_config.code.clone(),
142153
include_patterns,
@@ -146,13 +157,8 @@ pub fn get_linters_from_config(
146157
config_path: config_path.clone(),
147158
});
148159
}
149-
let all_linters = linters
150-
.iter()
151-
.map(|l| &l.code)
152-
.cloned()
153-
.collect::<HashSet<_>>();
154160

155-
debug!("Found linters: {:?}", all_linters,);
161+
debug!("Found linters: {:?}", all_linters);
156162

157163
// Apply --take
158164
if let Some(taken_linters) = taken_linters {

tests/integration_test.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,30 @@ fn simple_linter_fails_on_nonexistent_file() -> Result<()> {
204204
Ok(())
205205
}
206206

207+
#[test]
208+
fn duplicate_code_fails() -> Result<()> {
209+
let config = temp_config(
210+
"\
211+
[[linter]]
212+
code = 'DUPE'
213+
include_patterns = ['**']
214+
command = ['wont_be_run']
215+
216+
[[linter]]
217+
code = 'DUPE'
218+
include_patterns = ['**']
219+
command = ['wont_be_run']
220+
",
221+
)?;
222+
223+
let mut cmd = Command::cargo_bin("lintrunner")?;
224+
cmd.arg(format!("--config={}", config.path().to_str().unwrap()));
225+
cmd.assert().failure();
226+
assert_output_snapshot("duplicate_code_fails", &mut cmd)?;
227+
228+
Ok(())
229+
}
230+
207231
#[test]
208232
fn linter_providing_nonexistent_path_degrades_gracefully() -> Result<()> {
209233
let data_path = tempfile::tempdir()?;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: tests/integration_test.rs
3+
expression: output_lines
4+
---
5+
- "STDOUT:"
6+
- ""
7+
- ""
8+
- "STDERR:"
9+
- "error: Invalid linter configuration: linter 'DUPE' is defined multiple times."
10+

0 commit comments

Comments
 (0)