Skip to content

Commit e5bf24a

Browse files
committed
Fix attributes on mod tests; problem + Expanded a little bit the tests
1 parent c0e8e42 commit e5bf24a

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

clippy_lints/src/items_after_test_module.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
6262

6363
if !in_external_macro(cx.sess(), item.span);
6464
then {
65-
was_test_mod_visited = false;
6665
span_lint_and_then(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", |diag| {
6766
diag.multipart_suggestion("move the item to before the testing module was defined", vec![
6867
(item.span, String::new()), // Remove the item
6968
(
7069
Span::new(
71-
hir.item(items[when_was_visited - 1]).span.hi() + BytePos(1),
72-
hir.item(items[when_was_visited]).span.lo() - BytePos(1),
70+
hir.item(items[when_was_visited - 2]).span.hi() + BytePos(1),
71+
hir.item(items[when_was_visited - 1]).span.lo() - BytePos(1),
7372
item.span.ctxt(), item.span.parent()),
7473

7574
format!("\n{}\n", snippet(cx, item.span, ".."))

tests/ui/items_after_test_modules/items_after_test_module.fixed

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ mod my_mod;
77

88
fn main() {}
99

10-
fn should_not_lint() {}
11-
1210
fn should_lint() {}
1311

12+
const SHOULD_ALSO_LINT: usize = 1;
13+
14+
fn should_not_lint() {}
15+
16+
#[allow(dead_code)]
17+
#[allow(unused)] // Some attributes to check that span replacement is good enough
18+
#[allow(clippy::allow_attributes)]
19+
#[cfg(test)]
1420
mod tests {
1521
#[test]
1622
fn hi() {}
1723
}
1824

1925

26+
27+

tests/ui/items_after_test_modules/items_after_test_module.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ fn main() {}
99

1010
fn should_not_lint() {}
1111

12+
#[allow(dead_code)]
13+
#[allow(unused)] // Some attributes to check that span replacement is good enough
14+
#[allow(clippy::allow_attributes)]
1215
#[cfg(test)]
1316
mod tests {
1417
#[test]
1518
fn hi() {}
1619
}
1720

1821
fn should_lint() {}
22+
23+
const SHOULD_ALSO_LINT: usize = 1;

tests/ui/items_after_test_modules/items_after_test_module.stderr

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: an item was found after the testing module
2-
--> $DIR/items_after_test_module.rs:18:1
2+
--> $DIR/items_after_test_module.rs:21:1
33
|
44
LL | fn should_lint() {}
55
| ^^^^^^^^^^^^^^^^^^^
@@ -12,5 +12,18 @@ LL + fn should_lint() {}
1212
LL +
1313
|
1414

15-
error: aborting due to previous error
15+
error: an item was found after the testing module
16+
--> $DIR/items_after_test_module.rs:23:1
17+
|
18+
LL | const SHOULD_ALSO_LINT: usize = 1;
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: move the item to before the testing module was defined
22+
|
23+
LL +
24+
LL + const SHOULD_ALSO_LINT: usize = 1;
25+
LL +
26+
|
27+
28+
error: aborting due to 2 previous errors
1629

0 commit comments

Comments
 (0)