mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-01 00:49:30 +00:00
0f5338cd90
The `restriction` group contains many lints which are not about necessarily “bad” things, but style choices — perhaps even style choices which contradict conventional Rust style — or are otherwise very situational. This results in silly wording like “Why is this bad? It isn't, but ...”, which I’ve seen confuse a newcomer at least once. To improve this situation, this commit replaces the “Why is this bad?” section heading with “Why restrict this?”, for most, but not all, restriction lints. I left alone the ones whose placement in the restriction group is more incidental. In order to make this make sense, I had to remove the “It isn't, but” texts from the contents of the sections. Sometimes further changes were needed, or there were obvious fixes to make, and I went ahead and made those changes without attempting to split them into another commit, even though many of them are not strictly necessary for the “Why restrict this?” project.
74 lines
2.1 KiB
Rust
74 lines
2.1 KiB
Rust
use clippy_utils::diagnostics::span_lint_and_note;
|
|
use clippy_utils::{is_in_cfg_test, is_in_test_function};
|
|
use rustc_hir::intravisit::FnKind;
|
|
use rustc_hir::{Body, FnDecl};
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
use rustc_session::declare_lint_pass;
|
|
use rustc_span::def_id::LocalDefId;
|
|
use rustc_span::Span;
|
|
|
|
declare_clippy_lint! {
|
|
/// ### What it does
|
|
/// Triggers when a testing function (marked with the `#[test]` attribute) isn't inside a testing module
|
|
/// (marked with `#[cfg(test)]`).
|
|
///
|
|
/// ### Why restrict this?
|
|
/// The idiomatic (and more performant) way of writing tests is inside a testing module (flagged with `#[cfg(test)]`),
|
|
/// having test functions outside of this module is confusing and may lead to them being "hidden".
|
|
///
|
|
/// ### Example
|
|
/// ```no_run
|
|
/// #[test]
|
|
/// fn my_cool_test() {
|
|
/// // [...]
|
|
/// }
|
|
///
|
|
/// #[cfg(test)]
|
|
/// mod tests {
|
|
/// // [...]
|
|
/// }
|
|
///
|
|
/// ```
|
|
/// Use instead:
|
|
/// ```no_run
|
|
/// #[cfg(test)]
|
|
/// mod tests {
|
|
/// #[test]
|
|
/// fn my_cool_test() {
|
|
/// // [...]
|
|
/// }
|
|
/// }
|
|
/// ```
|
|
#[clippy::version = "1.70.0"]
|
|
pub TESTS_OUTSIDE_TEST_MODULE,
|
|
restriction,
|
|
"A test function is outside the testing module."
|
|
}
|
|
|
|
declare_lint_pass!(TestsOutsideTestModule => [TESTS_OUTSIDE_TEST_MODULE]);
|
|
|
|
impl LateLintPass<'_> for TestsOutsideTestModule {
|
|
fn check_fn(
|
|
&mut self,
|
|
cx: &LateContext<'_>,
|
|
kind: FnKind<'_>,
|
|
_: &FnDecl<'_>,
|
|
body: &Body<'_>,
|
|
sp: Span,
|
|
_: LocalDefId,
|
|
) {
|
|
if !matches!(kind, FnKind::Closure)
|
|
&& is_in_test_function(cx.tcx, body.id().hir_id)
|
|
&& !is_in_cfg_test(cx.tcx, body.id().hir_id)
|
|
{
|
|
span_lint_and_note(
|
|
cx,
|
|
TESTS_OUTSIDE_TEST_MODULE,
|
|
sp,
|
|
"this function marked with #[test] is outside a #[cfg(test)] module",
|
|
None,
|
|
"move it to a testing module marked with #[cfg(test)]",
|
|
);
|
|
}
|
|
}
|
|
}
|