Skip to content

Commit 614e03e

Browse files
committed
Remove suggestions, replace it with a help message
1 parent 083ef84 commit 614e03e

File tree

6 files changed

+25
-77
lines changed

6 files changed

+25
-77
lines changed

clippy_lints/src/items_after_test_module.rs

+6-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use clippy_utils::{diagnostics::span_lint_and_then, is_in_cfg_test, source::snippet};
2-
use rustc_errors::Applicability;
1+
use clippy_utils::{diagnostics::span_lint_and_help, is_in_cfg_test};
32
use rustc_hir::{HirId, ItemId, ItemKind, Mod};
43
use rustc_lint::{LateContext, LateLintPass, LintContext};
54
use rustc_middle::lint::in_external_macro;
65
use rustc_session::{declare_lint_pass, declare_tool_lint};
7-
use rustc_span::{sym, BytePos, Span};
6+
use rustc_span::sym;
87

98
declare_clippy_lint! {
109
/// ### What it does
@@ -44,42 +43,24 @@ declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]);
4443
impl LateLintPass<'_> for ItemsAfterTestModule {
4544
fn check_mod(&mut self, cx: &LateContext<'_>, _: &Mod<'_>, _: HirId) {
4645
let mut was_test_mod_visited = false;
47-
let mut test_span: Option<Span> = None;
48-
let mut when_was_visited = 0;
4946

5047
let hir = cx.tcx.hir();
5148
let items = hir.items().collect::<Vec<ItemId>>();
5249

53-
for (i, itid) in items.iter().enumerate() {
50+
for itid in items.iter() {
5451
let item = hir.item(*itid);
5552

5653
if_chain! {
5754
if was_test_mod_visited;
58-
if cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash ==
59-
cx.sess().source_map().lookup_char_pos(test_span.unwrap().lo()).file.name_hash;
6055
if !matches!(item.kind, ItemKind::Mod(_));
6156
if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself
6257

6358
if !in_external_macro(cx.sess(), item.span);
6459
then {
65-
span_lint_and_then(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", |diag| {
66-
diag.multipart_suggestion("move the item to before the testing module was defined", vec![
67-
(item.span, String::new()), // Remove the item
68-
(
69-
Span::new(
70-
hir.item(items[when_was_visited - 2]).span.hi() + BytePos(1),
71-
hir.item(items[when_was_visited - 1]).span.lo() - BytePos(1),
72-
item.span.ctxt(), item.span.parent()),
60+
span_lint_and_help(cx, ITEMS_AFTER_TEST_MODULE, item.span, "an item was found after the testing module", None, "move the item to before the testing module was defined");
61+
}};
7362

74-
format!("\n{}\n", snippet(cx, item.span, ".."))
75-
) // ^ Copy the item to the new location
76-
], Applicability::MachineApplicable);
77-
});
78-
return;
79-
}
80-
}
81-
82-
if let ItemKind::Mod(mod_item) = item.kind {
63+
if matches!(item.kind, ItemKind::Mod(_)) {
8364
for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) {
8465
if_chain! {
8566
if attr.has_name(sym::cfg);
@@ -88,8 +69,6 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
8869
if mitem.has_name(sym::test);
8970
then {
9071
was_test_mod_visited = true;
91-
test_span = Some(mod_item.spans.inject_use_span);
92-
when_was_visited = i;
9372
}
9473
}
9574
}

tests/ui/items_after_test_modules/items_after_test_module.rs renamed to tests/ui/items_after_test_module.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// compile-flags: --test
2-
// run-rustfix
32
#![allow(unused)]
43
#![warn(clippy::items_after_test_module)]
54

6-
mod my_mod;
7-
85
fn main() {}
96

107
fn should_not_lint() {}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: an item was found after the testing module
2+
--> $DIR/items_after_test_module.rs:18:1
3+
|
4+
LL | fn should_lint() {}
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: move the item to before the testing module was defined
8+
= note: `-D clippy::items-after-test-module` implied by `-D warnings`
9+
10+
error: an item was found after the testing module
11+
--> $DIR/items_after_test_module.rs:20:1
12+
|
13+
LL | const SHOULD_ALSO_LINT: usize = 1;
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: move the item to before the testing module was defined
17+
18+
error: aborting due to 2 previous errors
19+

tests/ui/items_after_test_modules/items_after_test_module.fixed

-25
This file was deleted.

tests/ui/items_after_test_modules/items_after_test_module.stderr

-16
This file was deleted.

tests/ui/items_after_test_modules/my_mod.rs

-6
This file was deleted.

0 commit comments

Comments
 (0)