2016-01-24 10:16:56 +01:00
|
|
|
//! lint when items are used after statements
|
|
|
|
|
2023-03-24 15:29:30 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_hir;
|
|
|
|
use rustc_hir::{Block, ItemKind, StmtKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2020-11-05 14:29:48 +01:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2016-01-24 10:16:56 +01:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for items declared after some statement in a block.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Items live for the entire scope they are declared
|
2019-03-05 11:50:33 -05:00
|
|
|
/// in. But statements are processed in order. This might cause confusion as
|
|
|
|
/// it's hard to figure out which item is meant in a statement.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2019-03-05 11:50:33 -05:00
|
|
|
/// fn foo() {
|
|
|
|
/// println!("cake");
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// foo(); // prints "foo"
|
|
|
|
/// fn foo() {
|
|
|
|
/// println!("foo");
|
|
|
|
/// }
|
|
|
|
/// foo(); // prints "foo"
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
2022-06-05 15:24:41 -04:00
|
|
|
/// Use instead:
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2020-06-09 14:36:01 +00:00
|
|
|
/// fn foo() {
|
|
|
|
/// println!("cake");
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// fn foo() {
|
|
|
|
/// println!("foo");
|
|
|
|
/// }
|
|
|
|
/// foo(); // prints "foo"
|
|
|
|
/// foo(); // prints "foo"
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-02-06 00:13:29 +01:00
|
|
|
pub ITEMS_AFTER_STATEMENTS,
|
2018-03-28 15:24:26 +02:00
|
|
|
pedantic,
|
2016-08-06 10:18:36 +02:00
|
|
|
"blocks where an item comes after a statement"
|
2016-02-06 00:13:29 +01:00
|
|
|
}
|
2016-01-24 10:16:56 +01:00
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
|
2016-01-24 10:16:56 +01:00
|
|
|
|
2023-03-24 15:29:30 +00:00
|
|
|
impl LateLintPass<'_> for ItemsAfterStatements {
|
|
|
|
fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) {
|
2024-06-12 20:14:50 -04:00
|
|
|
if block.stmts.len() > 1 {
|
|
|
|
let ctxt = block.span.ctxt();
|
|
|
|
let mut in_external = None;
|
|
|
|
block
|
|
|
|
.stmts
|
|
|
|
.iter()
|
|
|
|
.skip_while(|stmt| matches!(stmt.kind, StmtKind::Item(..)))
|
|
|
|
.filter_map(|stmt| match stmt.kind {
|
|
|
|
StmtKind::Item(id) => Some(cx.tcx.hir().item(id)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
// Ignore macros since they can only see previously defined locals.
|
|
|
|
.filter(|item| !matches!(item.kind, ItemKind::Macro(..)))
|
|
|
|
// Stop linting if macros define items.
|
|
|
|
.take_while(|item| item.span.ctxt() == ctxt)
|
|
|
|
// Don't use `next` due to the complex filter chain.
|
|
|
|
.for_each(|item| {
|
|
|
|
// Only do the macro check once, but delay it until it's needed.
|
|
|
|
if !*in_external.get_or_insert_with(|| in_external_macro(cx.sess(), block.span)) {
|
|
|
|
span_lint_hir(
|
|
|
|
cx,
|
|
|
|
ITEMS_AFTER_STATEMENTS,
|
|
|
|
item.hir_id(),
|
|
|
|
item.span,
|
|
|
|
"adding items after statements is confusing, since items exist from the \
|
|
|
|
start of the scope",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2016-01-24 10:16:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|