Skip to content

Commit f6194f3

Browse files
committed
Fix false positive in check mode caused by gen_deprecated
`gen_deprecated` should never have created the string with linebreaks. Using a single string broke the check because the changed lines were different.
1 parent 90f31e2 commit f6194f3

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

clippy_dev/src/lib.rs

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,22 @@ pub fn gen_changelog_lint_list(lints: Vec<Lint>) -> Vec<String> {
114114

115115
/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
116116
pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> {
117-
lints.iter()
118-
.filter_map(|l| {
119-
l.clone().deprecation.and_then(|depr_text| {
120-
Some(
121-
format!(
122-
" store.register_removed(\n \"{}\",\n \"{}\",\n );",
123-
l.name,
124-
depr_text
117+
itertools::flatten(
118+
lints
119+
.iter()
120+
.filter_map(|l| {
121+
l.clone().deprecation.and_then(|depr_text| {
122+
Some(
123+
vec![
124+
" store.register_removed(".to_string(),
125+
format!(" \"{}\",", l.name),
126+
format!(" \"{}\",", depr_text),
127+
" );".to_string()
128+
]
125129
)
126-
)
130+
})
127131
})
128-
})
129-
.collect()
132+
).collect()
130133
}
131134

132135
/// Gathers all files in `src/clippy_lints` and gathers all lints inside
@@ -258,6 +261,7 @@ pub fn replace_region_in_text<F>(text: &str, start: &str, end: &str, replace_sta
258261
// is incorrect.
259262
eprintln!("error: regex `{:?}` not found. You may have to update it.", start);
260263
}
264+
261265
FileChange {
262266
changed: lines.ne(new_lines.clone()),
263267
new_lines: new_lines.join("\n")
@@ -305,38 +309,40 @@ declare_deprecated_lint! {
305309

306310
#[test]
307311
fn test_replace_region() {
308-
let text = r#"
309-
abc
310-
123
311-
789
312-
def
313-
ghi"#;
314-
let expected = r#"
315-
abc
316-
hello world
317-
def
318-
ghi"#;
312+
let text = "\nabc\n123\n789\ndef\nghi";
313+
let expected = FileChange {
314+
changed: true,
315+
new_lines: "\nabc\nhello world\ndef\nghi".to_string()
316+
};
319317
let result = replace_region_in_text(text, r#"^\s*abc$"#, r#"^\s*def"#, false, || {
320318
vec!["hello world".to_string()]
321-
}).new_lines;
319+
});
322320
assert_eq!(expected, result);
323321
}
324322

325323
#[test]
326324
fn test_replace_region_with_start() {
327-
let text = r#"
328-
abc
329-
123
330-
789
331-
def
332-
ghi"#;
333-
let expected = r#"
334-
hello world
335-
def
336-
ghi"#;
325+
let text = "\nabc\n123\n789\ndef\nghi";
326+
let expected = FileChange {
327+
changed: true,
328+
new_lines: "\nhello world\ndef\nghi".to_string()
329+
};
337330
let result = replace_region_in_text(text, r#"^\s*abc$"#, r#"^\s*def"#, true, || {
338331
vec!["hello world".to_string()]
339-
}).new_lines;
332+
});
333+
assert_eq!(expected, result);
334+
}
335+
336+
#[test]
337+
fn test_replace_region_no_changes() {
338+
let text = "123\n456\n789";
339+
let expected = FileChange {
340+
changed: false,
341+
new_lines: "123\n456\n789".to_string()
342+
};
343+
let result = replace_region_in_text(text, r#"^\s*123$"#, r#"^\s*456"#, false, || {
344+
vec![]
345+
});
340346
assert_eq!(expected, result);
341347
}
342348

@@ -390,14 +396,19 @@ fn test_gen_changelog_lint_list() {
390396
fn test_gen_deprecated() {
391397
let lints = vec![
392398
Lint::new("should_assert_eq", "group1", "abc", Some("has been superseeded by should_assert_eq2"), "module_name"),
399+
Lint::new("another_deprecated", "group2", "abc", Some("will be removed"), "module_name"),
393400
Lint::new("should_assert_eq2", "group2", "abc", None, "module_name")
394401
];
395402
let expected: Vec<String> = vec![
396-
r#" store.register_removed(
397-
"should_assert_eq",
398-
"has been superseeded by should_assert_eq2",
399-
);"#.to_string()
400-
];
403+
" store.register_removed(",
404+
" \"should_assert_eq\",",
405+
" \"has been superseeded by should_assert_eq2\",",
406+
" );",
407+
" store.register_removed(",
408+
" \"another_deprecated\",",
409+
" \"will be removed\",",
410+
" );"
411+
].into_iter().map(String::from).collect();
401412
assert_eq!(expected, gen_deprecated(&lints));
402413
}
403414

0 commit comments

Comments
 (0)