Skip to content

Commit 546be18

Browse files
committed
internal: check that coverage marks are always paired
1 parent 0eafc88 commit 546be18

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

crates/hir_def/src/body/tests/block.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ fn legacy_macro_items() {
163163
// correctly.
164164
check_at(
165165
r#"
166-
macro_rules! hit {
166+
macro_rules! mark {
167167
() => {
168168
struct Hit {}
169169
}
170170
}
171171
172172
fn f() {
173-
hit!();
173+
mark!();
174174
$0
175175
}
176176
"#,
@@ -193,20 +193,20 @@ use core::cov_mark;
193193
194194
fn f() {
195195
fn nested() {
196-
cov_mark::hit!(Hit);
196+
cov_mark::mark!(Hit);
197197
$0
198198
}
199199
}
200200
//- /core.rs crate:core
201201
pub mod cov_mark {
202202
#[macro_export]
203-
macro_rules! _hit {
203+
macro_rules! _mark {
204204
($name:ident) => {
205205
struct $name {}
206206
}
207207
}
208208
209-
pub use crate::_hit as hit;
209+
pub use crate::_mark as mark;
210210
}
211211
"#,
212212
expect![[r#"

crates/ide_completion/src/render.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ fn go(world: &WorldSnapshot) { go(w$0) }
10071007

10081008
#[test]
10091009
fn too_many_arguments() {
1010+
cov_mark::check!(too_many_arguments);
10101011
check_relevance(
10111012
r#"
10121013
struct Foo;

xtask/src/tidy.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::path::{Path, PathBuf};
1+
use std::{
2+
collections::HashSet,
3+
path::{Path, PathBuf},
4+
};
25

36
use xshell::{cmd, pushd, pushenv, read_file};
47

@@ -81,15 +84,18 @@ Please adjust docs/dev/lsp-extensions.md.
8184
#[test]
8285
fn rust_files_are_tidy() {
8386
let mut tidy_docs = TidyDocs::default();
87+
let mut tidy_marks = TidyMarks::default();
8488
for path in rust_files() {
8589
let text = read_file(&path).unwrap();
8690
check_todo(&path, &text);
8791
check_dbg(&path, &text);
8892
check_trailing_ws(&path, &text);
8993
deny_clippy(&path, &text);
9094
tidy_docs.visit(&path, &text);
95+
tidy_marks.visit(&path, &text);
9196
}
9297
tidy_docs.finish();
98+
tidy_marks.finish();
9399
}
94100

95101
#[test]
@@ -408,6 +414,39 @@ fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
408414
.any(|it| dirs_to_exclude.contains(&it))
409415
}
410416

417+
#[derive(Default)]
418+
struct TidyMarks {
419+
hits: HashSet<String>,
420+
checks: HashSet<String>,
421+
}
422+
423+
impl TidyMarks {
424+
fn visit(&mut self, _path: &Path, text: &str) {
425+
for line in text.lines() {
426+
if let Some(mark) = find_mark(line, "hit") {
427+
self.hits.insert(mark.to_string());
428+
}
429+
if let Some(mark) = find_mark(line, "check") {
430+
self.checks.insert(mark.to_string());
431+
}
432+
if let Some(mark) = find_mark(line, "check_count") {
433+
self.checks.insert(mark.to_string());
434+
}
435+
}
436+
}
437+
438+
fn finish(self) {
439+
assert!(!self.hits.is_empty());
440+
441+
let diff: Vec<_> =
442+
self.hits.symmetric_difference(&self.checks).map(|it| it.as_str()).collect();
443+
444+
if !diff.is_empty() {
445+
panic!("unpaired marks: {:?}", diff)
446+
}
447+
}
448+
}
449+
411450
#[allow(deprecated)]
412451
fn stable_hash(text: &str) -> u64 {
413452
use std::hash::{Hash, Hasher, SipHasher};
@@ -417,3 +456,11 @@ fn stable_hash(text: &str) -> u64 {
417456
text.hash(&mut hasher);
418457
hasher.finish()
419458
}
459+
460+
fn find_mark<'a>(text: &'a str, mark: &'static str) -> Option<&'a str> {
461+
let idx = text.find(mark)?;
462+
let text = text[idx + mark.len()..].strip_prefix("!(")?;
463+
let idx = text.find(|c: char| !(c.is_alphanumeric() || c == '_'))?;
464+
let text = &text[..idx];
465+
Some(text)
466+
}

0 commit comments

Comments
 (0)