From 9a8038540f7ad7b8d1775c8875388033590b8790 Mon Sep 17 00:00:00 2001 From: Daniel Sommermann Date: Fri, 7 Aug 2026 11:23:07 -0700 Subject: [PATCH] Add generic `allow-in-tests` configuration Adds an array-typed `allow-in-tests` option that suppresses any Clippy lint in test code, superseding the per-lint `allow-*-in-tests` options. Suppression happens once at diagnostic emission in `clippy_utils`, so it covers every lint rather than needing per-lint plumbing. Lint names are resolved against the declared lints at startup; unknown names warn with a span into `clippy.toml`. The seven migratable `allow-*-in-tests` options are soft-deprecated: they keep working, but setting one to `true` now warns and points at the replacement. `allow-large-stack-frames-in-tests` is excluded because its meaningful setting is `false`, which `allow-in-tests` cannot express. --- CHANGELOG.md | 1 + book/src/lint_configuration.md | 42 +++++++++++ clippy_config/src/conf.rs | 71 ++++++++++++++++++- clippy_config/src/metadata.rs | 18 +++-- .../src/arbitrary_source_item_ordering.rs | 6 +- clippy_lints/src/lib.rs | 60 +++++++++++++++- clippy_utils/src/diagnostics.rs | 69 ++++++++++++++++-- src/driver.rs | 2 +- tests/config-metadata.rs | 1 - .../ui-toml/allow_in_tests/allow_in_tests.rs | 43 +++++++++++ .../allow_in_tests/allow_in_tests.stderr | 46 ++++++++++++ tests/ui-toml/allow_in_tests/clippy.toml | 1 + .../allow_in_tests_deprecated.rs | 5 ++ .../allow_in_tests_deprecated.stderr | 18 +++++ .../allow_in_tests_deprecated/clippy.toml | 9 +++ .../allow_in_tests_unknown.rs | 6 ++ .../allow_in_tests_unknown.stderr | 26 +++++++ .../allow_in_tests_unknown/clippy.toml | 8 +++ tests/ui-toml/dbg_macro/dbg_macro.fixed | 1 + tests/ui-toml/dbg_macro/dbg_macro.rs | 1 + tests/ui-toml/dbg_macro/dbg_macro.stderr | 24 ++++--- tests/ui-toml/expect_used/expect_used.rs | 1 + tests/ui-toml/expect_used/expect_used.stderr | 18 +++-- .../indexing_slicing/indexing_slicing.rs | 1 + .../indexing_slicing/indexing_slicing.stderr | 12 +++- tests/ui-toml/panic/panic.rs | 1 + tests/ui-toml/panic/panic.stderr | 14 +++- tests/ui-toml/print_macro/print_macro.rs | 1 + tests/ui-toml/print_macro/print_macro.stderr | 14 +++- .../toml_unknown_key/conf_unknown_key.stderr | 1 + tests/ui-toml/unwrap_used/unwrap_used.fixed | 1 + tests/ui-toml/unwrap_used/unwrap_used.rs | 1 + tests/ui-toml/unwrap_used/unwrap_used.stderr | 66 +++++++++-------- .../ui-toml/unwrap_used/unwrap_used_const.rs | 1 + .../unwrap_used/unwrap_used_const.stderr | 14 +++- tests/ui-toml/useless_vec/useless_vec.fixed | 1 + tests/ui-toml/useless_vec/useless_vec.rs | 1 + tests/ui-toml/useless_vec/useless_vec.stderr | 12 +++- 38 files changed, 545 insertions(+), 73 deletions(-) create mode 100644 tests/ui-toml/allow_in_tests/allow_in_tests.rs create mode 100644 tests/ui-toml/allow_in_tests/allow_in_tests.stderr create mode 100644 tests/ui-toml/allow_in_tests/clippy.toml create mode 100644 tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.rs create mode 100644 tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.stderr create mode 100644 tests/ui-toml/allow_in_tests_deprecated/clippy.toml create mode 100644 tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.rs create mode 100644 tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.stderr create mode 100644 tests/ui-toml/allow_in_tests_unknown/clippy.toml diff --git a/CHANGELOG.md b/CHANGELOG.md index 3876af9b2f37..5970745c029f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7670,6 +7670,7 @@ Released 2018-09-13 [`allow-exact-repetitions`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-exact-repetitions [`allow-expect-in-consts`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-consts [`allow-expect-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-expect-in-tests +[`allow-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-in-tests [`allow-indexing-slicing-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-indexing-slicing-in-tests [`allow-large-stack-frames-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-large-stack-frames-in-tests [`allow-mixed-uninlined-format-args`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-mixed-uninlined-format-args diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index 6869cc56ed95..9c6a5ac910ab 100644 --- a/book/src/lint_configuration.md +++ b/book/src/lint_configuration.md @@ -64,6 +64,9 @@ Don't lint when comparing the result of a modulo operation to zero. ## `allow-dbg-in-tests` Whether `dbg!` should be allowed in test functions or `#[cfg(test)]` +Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any +lint. This option still works, but new configurations should use `allow-in-tests`. + **Default Value:** `false` --- @@ -94,6 +97,9 @@ Whether `expect` should be allowed in code always evaluated at compile time ## `allow-expect-in-tests` Whether `expect` should be allowed in test functions or `#[cfg(test)]` +Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any +lint. This option still works, but new configurations should use `allow-in-tests`. + **Default Value:** `false` --- @@ -101,9 +107,33 @@ Whether `expect` should be allowed in test functions or `#[cfg(test)]` * [`expect_used`](https://rust-lang.github.io/rust-clippy/master/index.html#expect_used) +## `allow-in-tests` +A list of Clippy lints to suppress in test functions and `#[cfg(test)]` items. + +This supersedes the per-lint `allow--in-tests` options, which are deprecated but +still honored: a lint is suppressed in test code if it is listed here or its own +`allow--in-tests` option is set. + +#### Example + +```toml +allow-in-tests = ["dbg_macro", "unwrap_used"] +``` + +#### Noteworthy + +- This applies to late lint passes only, as test code becomes recognizable once the HIR + has been built. + +**Default Value:** `[]` + + ## `allow-indexing-slicing-in-tests` Whether `indexing_slicing` should be allowed in test functions or `#[cfg(test)]` +Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any +lint. This option still works, but new configurations should use `allow-in-tests`. + **Default Value:** `false` --- @@ -144,6 +174,9 @@ Whether to allow `r#""#` when `r""` can be used ## `allow-panic-in-tests` Whether `panic` should be allowed in test functions or `#[cfg(test)]` +Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any +lint. This option still works, but new configurations should use `allow-in-tests`. + **Default Value:** `false` --- @@ -154,6 +187,9 @@ Whether `panic` should be allowed in test functions or `#[cfg(test)]` ## `allow-print-in-tests` Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]` +Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any +lint. This option still works, but new configurations should use `allow-in-tests`. + **Default Value:** `false` --- @@ -207,6 +243,9 @@ Whether `unwrap` should be allowed in code always evaluated at compile time ## `allow-unwrap-in-tests` Whether `unwrap` should be allowed in test functions or `#[cfg(test)]` +Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any +lint. This option still works, but new configurations should use `allow-in-tests`. + **Default Value:** `false` --- @@ -234,6 +273,9 @@ allow-unwrap-types = [ "std::sync::LockResult" ] ## `allow-useless-vec-in-tests` Whether `useless_vec` should ignore test functions or `#[cfg(test)]` +Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any +lint. This option still works, but new configurations should use `allow-in-tests`. + **Default Value:** `false` --- diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs index 13c73e944581..e5d0e1a3f99a 100644 --- a/clippy_config/src/conf.rs +++ b/clippy_config/src/conf.rs @@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; use rustc_hir::attrs::RustcVersion; use rustc_session::Session; -use rustc_span::{Pos as _, SourceFile, Symbol}; +use rustc_span::{Pos as _, SourceFile, Spanned, Symbol}; use std::path::PathBuf; use std::sync::{Arc, OnceLock}; use std::{env, fs, io}; @@ -77,6 +77,10 @@ macro_rules! define_Conf { $(#[doc = $doc:literal])* $(#[default_text = $default_text:literal])? $(#[rename = $new_name:ident])? + // Marks a `bool` field superseded by `allow-in-tests`, listing the lints it enables in + // tests. Setting such a field to `true` warns and points at the replacement. + // Must precede `#[lints]`, which `cargo dev fmt` always re-emits last. + $(#[replaced_by_allow_in_tests($($replacement_lints:ident),* $(,)?)])? $(#[lints($($for_lints:ident),* $(,)?)])? // The type must exist for regular fields and shouldn't exist for deprecated ones. $name:ident($name_str:literal) $(: $ty:ty $(= $default:expr)?)?, @@ -196,6 +200,26 @@ macro_rules! define_Conf { } } + // Nudge the per-lint `allow-*-in-tests` options towards `allow-in-tests`. Only + // enabling one is worth warning about; disabling it is the default and has no + // equivalent in the replacement. + $($( + if $name == Some(true) + && let Some((key, _)) = table.get_key_value($name_str) + { + dcx.inner + .struct_span_warn( + dcx.make_sp(key.span()), + concat!("`", $name_str, "` is deprecated"), + ) + .with_help(format!( + "use `allow-in-tests = [{}]` instead, which works for any lint", + [$(concat!("\"", stringify!($replacement_lints), "\"")),*].join(", "), + )) + .emit(); + } + )?)* + Self {$($( $name: $name.unwrap_or_else( || <$ty as FromDefault<_>>::from_default(first_expr!($($default,)? ())) @@ -236,6 +260,10 @@ define_Conf! { #[lints(modulo_arithmetic)] allow_comparison_to_zero("allow-comparison-to-zero"): bool = true, /// Whether `dbg!` should be allowed in test functions or `#[cfg(test)]` + /// + /// Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any + /// lint. This option still works, but new configurations should use `allow-in-tests`. + #[replaced_by_allow_in_tests(dbg_macro)] #[lints(dbg_macro)] allow_dbg_in_tests("allow-dbg-in-tests"): bool = false, /// Whether an item should be allowed to have the same name as its containing module @@ -245,9 +273,34 @@ define_Conf! { #[lints(expect_used)] allow_expect_in_consts("allow-expect-in-consts"): bool = true, /// Whether `expect` should be allowed in test functions or `#[cfg(test)]` + /// + /// Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any + /// lint. This option still works, but new configurations should use `allow-in-tests`. + #[replaced_by_allow_in_tests(expect_used)] #[lints(expect_used)] allow_expect_in_tests("allow-expect-in-tests"): bool = false, + /// A list of Clippy lints to suppress in test functions and `#[cfg(test)]` items. + /// + /// This supersedes the per-lint `allow--in-tests` options, which are deprecated but + /// still honored: a lint is suppressed in test code if it is listed here or its own + /// `allow--in-tests` option is set. + /// + /// #### Example + /// + /// ```toml + /// allow-in-tests = ["dbg_macro", "unwrap_used"] + /// ``` + /// + /// #### Noteworthy + /// + /// - This applies to late lint passes only, as test code becomes recognizable once the HIR + /// has been built. + allow_in_tests("allow-in-tests"): Vec>, /// Whether `indexing_slicing` should be allowed in test functions or `#[cfg(test)]` + /// + /// Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any + /// lint. This option still works, but new configurations should use `allow-in-tests`. + #[replaced_by_allow_in_tests(indexing_slicing)] #[lints(indexing_slicing)] allow_indexing_slicing_in_tests("allow-indexing-slicing-in-tests"): bool = false, /// Whether functions inside `#[cfg(test)]` modules or test functions should be checked. @@ -260,9 +313,17 @@ define_Conf! { #[lints(needless_raw_string_hashes)] allow_one_hash_in_raw_strings("allow-one-hash-in-raw-strings"): bool = false, /// Whether `panic` should be allowed in test functions or `#[cfg(test)]` + /// + /// Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any + /// lint. This option still works, but new configurations should use `allow-in-tests`. + #[replaced_by_allow_in_tests(panic)] #[lints(panic)] allow_panic_in_tests("allow-panic-in-tests"): bool = false, /// Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]` + /// + /// Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any + /// lint. This option still works, but new configurations should use `allow-in-tests`. + #[replaced_by_allow_in_tests(print_stderr, print_stdout)] #[lints(print_stderr, print_stdout)] allow_print_in_tests("allow-print-in-tests"): bool = false, /// Whether to allow module inception if it's not public. @@ -287,6 +348,10 @@ define_Conf! { #[lints(unwrap_used)] allow_unwrap_in_consts("allow-unwrap-in-consts"): bool = true, /// Whether `unwrap` should be allowed in test functions or `#[cfg(test)]` + /// + /// Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any + /// lint. This option still works, but new configurations should use `allow-in-tests`. + #[replaced_by_allow_in_tests(unwrap_used)] #[lints(unwrap_used)] allow_unwrap_in_tests("allow-unwrap-in-tests"): bool = false, /// List of types to allow `unwrap()` and `expect()` on. @@ -299,6 +364,10 @@ define_Conf! { #[lints(expect_used, unwrap_used)] allow_unwrap_types("allow-unwrap-types"): Vec, /// Whether `useless_vec` should ignore test functions or `#[cfg(test)]` + /// + /// Deprecated in favor of [`allow-in-tests`](#allow-in-tests), which works for any + /// lint. This option still works, but new configurations should use `allow-in-tests`. + #[replaced_by_allow_in_tests(useless_vec)] #[lints(useless_vec)] allow_useless_vec_in_tests("allow-useless-vec-in-tests"): bool = false, /// Additional dotfiles (files or directories starting with a dot) to allow diff --git a/clippy_config/src/metadata.rs b/clippy_config/src/metadata.rs index 740ba3750948..84a6bb41ce97 100644 --- a/clippy_config/src/metadata.rs +++ b/clippy_config/src/metadata.rs @@ -26,17 +26,25 @@ impl ConfMetadata { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "## `{}`\n{}\n\n**Default Value:** `{}`\n\n---\n**Affected lints:**\n{}\n\n", + "## `{}`\n{}\n\n**Default Value:** `{}`\n\n", self.0.name, self.0 .doc .lines() .format_with("\n", |doc, f| f(&doc.strip_prefix(" ").unwrap_or(doc))), self.0.default, - self.0.lints.iter().format_with("\n", |name, f| f(&format_args!( - "* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})" - ))), - ) + )?; + // Options such as `allow-in-tests` apply to any lint, so they have no list to show. + if !self.0.lints.is_empty() { + write!( + f, + "---\n**Affected lints:**\n{}\n\n", + self.0.lints.iter().format_with("\n", |name, f| f(&format_args!( + "* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})" + ))), + )?; + } + Ok(()) } } S(self) diff --git a/clippy_lints/src/arbitrary_source_item_ordering.rs b/clippy_lints/src/arbitrary_source_item_ordering.rs index ca3a923aac5d..faf685f6ae28 100644 --- a/clippy_lints/src/arbitrary_source_item_ordering.rs +++ b/clippy_lints/src/arbitrary_source_item_ordering.rs @@ -4,14 +4,14 @@ use clippy_config::types::{ SourceItemOrderingTraitAssocItemKind, SourceItemOrderingTraitAssocItemKinds, SourceItemOrderingWithinModuleItemGroupings, TraitImplItemOrder, }; -use clippy_utils::diagnostics::span_lint_and_note; +use clippy_utils::diagnostics::{ClippyLintContext, span_lint_and_note}; use clippy_utils::is_cfg_test; use rustc_hir::attrs::AttributeKind; use rustc_hir::{ Attribute, FieldDef, HirId, ImplItemId, IsAuto, Item, ItemKind, Mod, OwnerId, QPath, TraitItemId, TyKind, Variant, VariantData, }; -use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_lint::{LateContext, LateLintPass, LintContext as _}; use rustc_middle::ty::{AssocKind, TyCtxt}; use rustc_session::impl_lint_pass; use rustc_span::{Ident, Symbol}; @@ -227,7 +227,7 @@ impl ArbitrarySourceItemOrdering { } /// Produces a linting warning for incorrectly ordered item members. - fn lint_member_name(cx: &T, ident: Ident, before_ident: Ident) { + fn lint_member_name(cx: &T, ident: Ident, before_ident: Ident) { span_lint_and_note( cx, ARBITRARY_SOURCE_ITEM_ORDERING, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 8b0765fb6c01..3784d58350d2 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -416,8 +416,12 @@ mod zombie_processes; use clippy_config::{Conf, sanitize_explanation}; use clippy_utils::macros::FormatArgsStorage; use rustc_data_structures::fx::FxHashSet; +use rustc_errors::Applicability; use rustc_lint::is_lint_pass_required; use rustc_middle::ty::TyCtxt; +use rustc_session::Session; +use rustc_span::Symbol; +use rustc_span::edit_distance::find_best_match_for_name; use utils::attr_collector::AttrStorage; pub fn explain(name: &str) -> i32 { @@ -441,10 +445,64 @@ pub fn explain(name: &str) -> i32 { } } +/// Resolves the lint names given in the `allow-in-tests` configuration and hands them to +/// `clippy_utils`, which drops their diagnostics when they are emitted from test code. +/// +/// Names which don't refer to a Clippy lint are reported and ignored. +fn register_lints_allowed_in_tests(sess: &Session, conf: &'static Conf) { + let mut allowed = Vec::with_capacity(conf.allow_in_tests.len()); + for name in &conf.allow_in_tests { + // Accept `unwrap_used`, `clippy::unwrap_used` and `unwrap-used` alike. + let bare_name = name.node.strip_prefix("clippy::").unwrap_or(&name.node); + let lint_name = format!("clippy::{}", bare_name.replace('-', "_").to_ascii_uppercase()); + + if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == lint_name) { + allowed.push(info.lint.name); + continue; + } + + let mut diag = sess + .dcx() + .struct_span_warn(name.span, format!("unknown lint: `{}`", name.node)); + diag.note("`allow-in-tests` only accepts Clippy lints"); + let renamed = deprecated_lints::RENAMED + .iter() + .find(|(old_name, _)| old_name.eq_ignore_ascii_case(&lint_name)); + if let Some((_, new_name)) = renamed { + let new_name = new_name.strip_prefix("clippy::").unwrap_or(new_name); + diag.span_suggestion( + name.span, + format!("`{}` has been renamed", name.node), + format!("\"{new_name}\""), + Applicability::MachineApplicable, + ); + } else if let Some(sugg) = find_best_match_for_name(&lint_symbols(), Symbol::intern(bare_name), None) { + diag.span_suggestion( + name.span, + "did you mean", + format!("\"{sugg}\""), + Applicability::MaybeIncorrect, + ); + } + diag.emit(); + } + clippy_utils::diagnostics::set_lints_allowed_in_tests(allowed); +} + +/// The names of all Clippy lints, without the `clippy::` prefix, for use in suggestions. +fn lint_symbols() -> Vec { + declared_lints::LINTS + .iter() + .map(|info| Symbol::intern(info.lint.name_lower().strip_prefix("clippy::").unwrap())) + .collect() +} + /// Register all lints and lint groups with the rustc lint store /// /// Used in `./src/driver.rs`. -pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Conf) { +pub fn register_lint_passes(sess: &Session, store: &mut rustc_lint::LintStore, conf: &'static Conf) { + register_lints_allowed_in_tests(sess, conf); + for (old_name, new_name) in deprecated_lints::RENAMED { store.register_renamed(old_name, new_name); } diff --git a/clippy_utils/src/diagnostics.rs b/clippy_utils/src/diagnostics.rs index f3c4ae3500af..823ad0c44f0e 100644 --- a/clippy_utils/src/diagnostics.rs +++ b/clippy_utils/src/diagnostics.rs @@ -8,13 +8,57 @@ //! Thank you! //! ~The `INTERNAL_METADATA_COLLECTOR` lint +use crate::is_in_test; +use rustc_data_structures::fx::FxHashSet; use rustc_errors::{Applicability, Diag, DiagCtxtHandle, DiagMessage, Diagnostic, Level, MultiSpan}; #[cfg(debug_assertions)] use rustc_errors::{EmissionGuarantee, SubstitutionPart, Suggestions}; use rustc_hir::HirId; -use rustc_lint::{LateContext, Lint, LintContext}; +use rustc_lint::{EarlyContext, LateContext, Lint, LintContext}; use rustc_span::Span; use std::env; +use std::sync::OnceLock; + +/// The lints which the `allow-in-tests` configuration suppresses in test code, stored as +/// [`Lint::name`] values (e.g. `"clippy::UNWRAP_USED"`). +static ALLOWED_IN_TESTS: OnceLock> = OnceLock::new(); + +/// Sets the lints which the `allow-in-tests` configuration suppresses in test code. +/// +/// This must be called before any lint pass runs; only the first call has an effect. +pub fn set_lints_allowed_in_tests(lints: impl IntoIterator) { + let _ = ALLOWED_IN_TESTS.set(lints.into_iter().collect()); +} + +fn is_allowed_in_tests(lint: &'static Lint) -> bool { + // This runs for every emitted lint, so check for the common case of an unset configuration + // before hashing the lint's name. + ALLOWED_IN_TESTS + .get() + .is_some_and(|lints| !lints.is_empty() && lints.contains(lint.name)) +} + +/// Extends [`LintContext`] with the information clippy's diagnostic functions need to apply the +/// `allow-in-tests` configuration. +pub trait ClippyLintContext: LintContext { + /// Whether the node the lint level is resolved against is in test code, i.e. inside a + /// `#[test]` function or a `#[cfg(test)]` item. + fn is_in_test_code(&self) -> bool; +} + +impl ClippyLintContext for LateContext<'_> { + fn is_in_test_code(&self) -> bool { + is_in_test(self.tcx, self.last_node_with_lint_attrs) + } +} + +/// Early lint passes run before the HIR is built, at which point test code becomes recognizable, +/// so `allow-in-tests` applies to late lint passes only. +impl ClippyLintContext for EarlyContext<'_> { + fn is_in_test_code(&self) -> bool { + false + } +} fn docs_link(diag: &mut Diag<'_, ()>, lint: &'static Lint) { if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() @@ -103,7 +147,12 @@ fn validate_diag(diag: &Diag<'_, impl EmissionGuarantee>) { /// | ^^^^^^^^^^^^^^^^^^^^^^^ /// ``` #[track_caller] -pub fn span_lint(cx: &T, lint: &'static Lint, sp: impl Into, msg: impl Into) { +pub fn span_lint( + cx: &T, + lint: &'static Lint, + sp: impl Into, + msg: impl Into, +) { span_lint_and_then(cx, lint, sp, msg, |_| {}); } @@ -142,7 +191,7 @@ pub fn span_lint(cx: &T, lint: &'static Lint, sp: impl Into( +pub fn span_lint_and_help( cx: &T, lint: &'static Lint, span: impl Into, @@ -197,7 +246,7 @@ pub fn span_lint_and_help( /// | ^^^^^^^^^^^ /// ``` #[track_caller] -pub fn span_lint_and_note( +pub fn span_lint_and_note( cx: &T, lint: &'static Lint, span: impl Into, @@ -235,7 +284,7 @@ pub fn span_lint_and_note( #[track_caller] pub fn span_lint_and_then(cx: &C, lint: &'static Lint, sp: S, msg: M, f: F) where - C: LintContext, + C: ClippyLintContext, S: Into, M: Into, F: FnOnce(&mut Diag<'_, ()>), @@ -250,6 +299,10 @@ where } } + if is_allowed_in_tests(lint) && cx.is_in_test_code() { + return; + } + let sp = sp.into(); #[expect(clippy::disallowed_methods)] cx.emit_span_lint( @@ -329,6 +382,10 @@ pub fn span_lint_hir_and_then( msg: impl Into, f: impl FnOnce(&mut Diag<'_, ()>), ) { + if is_allowed_in_tests(lint) && is_in_test(cx.tcx, hir_id) { + return; + } + #[expect(clippy::disallowed_methods)] cx.tcx.emit_node_span_lint( lint, @@ -379,7 +436,7 @@ pub fn span_lint_hir_and_then( /// = note: `-D fold-any` implied by `-D warnings` /// ``` #[track_caller] -pub fn span_lint_and_sugg( +pub fn span_lint_and_sugg( cx: &T, lint: &'static Lint, sp: Span, diff --git a/src/driver.rs b/src/driver.rs index 78b9b2cd8de3..6513192cbe3b 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -163,7 +163,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks { list_builder.register(lint_store); let conf = clippy_config::Conf::load(sess); - clippy_lints::register_lint_passes(lint_store, conf); + clippy_lints::register_lint_passes(sess, lint_store, conf); #[cfg(feature = "internal")] clippy_lints_internal::register_lints(lint_store); diff --git a/tests/config-metadata.rs b/tests/config-metadata.rs index a91785e78203..60827f3c1163 100644 --- a/tests/config-metadata.rs +++ b/tests/config-metadata.rs @@ -12,7 +12,6 @@ fn metadata() -> impl Iterator { Conf::get_metadata() .into_iter() .filter(|config| config.renamed_to.is_none()) - .filter(|config| !config.lints.is_empty()) } #[test] diff --git a/tests/ui-toml/allow_in_tests/allow_in_tests.rs b/tests/ui-toml/allow_in_tests/allow_in_tests.rs new file mode 100644 index 000000000000..ccb7e094d4ad --- /dev/null +++ b/tests/ui-toml/allow_in_tests/allow_in_tests.rs @@ -0,0 +1,43 @@ +//@compile-flags: --test +//@no-rustfix +#![warn(clippy::dbg_macro, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)] +#![allow(clippy::no_effect, clippy::unnecessary_operation)] + +fn main() {} + +// Outside of test code the listed lints still fire. +fn not_a_test(x: Option, s: &[u32]) -> u32 { + dbg!(x); + //~^ dbg_macro + s[0]; + //~^ indexing_slicing + x.unwrap() + //~^ unwrap_used +} + +#[test] +fn in_test_fn() { + let x: Option = "1".parse().ok(); + let s: &[u32] = &[1]; + dbg!(x); + s[0]; + x.unwrap(); + // `panic` isn't listed in `allow-in-tests`, so it is still linted. + panic!("boom"); + //~^ panic +} + +#[cfg(test)] +mod tests { + // Not a `#[test]` function, but inside a `#[cfg(test)]` module. + fn helper(x: Option, s: &[u32]) -> u32 { + dbg!(x); + s[0]; + x.unwrap() + } + + #[test] + fn uses_helper() { + helper("1".parse().ok(), &[1]); + } +} diff --git a/tests/ui-toml/allow_in_tests/allow_in_tests.stderr b/tests/ui-toml/allow_in_tests/allow_in_tests.stderr new file mode 100644 index 000000000000..480aeaf77736 --- /dev/null +++ b/tests/ui-toml/allow_in_tests/allow_in_tests.stderr @@ -0,0 +1,46 @@ +error: the `dbg!` macro is intended as a debugging tool + --> tests/ui-toml/allow_in_tests/allow_in_tests.rs:10:5 + | +LL | dbg!(x); + | ^^^^^^^ + | + = note: `-D clippy::dbg-macro` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::dbg_macro)]` +help: remove the invocation before committing it to a version control system + | +LL - dbg!(x); +LL + x; + | + +error: indexing may panic + --> tests/ui-toml/allow_in_tests/allow_in_tests.rs:12:5 + | +LL | s[0]; + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + = note: `-D clippy::indexing-slicing` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]` + +error: used `unwrap()` on an `Option` value + --> tests/ui-toml/allow_in_tests/allow_in_tests.rs:14:5 + | +LL | x.unwrap() + | ^^^^^^^^^^ + | + = note: if this value is `None`, it will panic + = help: consider using `expect()` to provide a better panic message + = note: `-D clippy::unwrap-used` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::unwrap_used)]` + +error: `panic` should not be present in production code + --> tests/ui-toml/allow_in_tests/allow_in_tests.rs:26:5 + | +LL | panic!("boom"); + | ^^^^^^^^^^^^^^ + | + = note: `-D clippy::panic` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::panic)]` + +error: aborting due to 4 previous errors + diff --git a/tests/ui-toml/allow_in_tests/clippy.toml b/tests/ui-toml/allow_in_tests/clippy.toml new file mode 100644 index 000000000000..ad55942ed3d7 --- /dev/null +++ b/tests/ui-toml/allow_in_tests/clippy.toml @@ -0,0 +1 @@ +allow-in-tests = ["dbg_macro", "clippy::unwrap_used", "indexing-slicing"] diff --git a/tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.rs b/tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.rs new file mode 100644 index 000000000000..07e1614c5efe --- /dev/null +++ b/tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.rs @@ -0,0 +1,5 @@ +//@no-rustfix +//@error-in-other-file: `allow-panic-in-tests` is deprecated +//@error-in-other-file: `allow-print-in-tests` is deprecated + +fn main() {} diff --git a/tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.stderr b/tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.stderr new file mode 100644 index 000000000000..b35f9557d758 --- /dev/null +++ b/tests/ui-toml/allow_in_tests_deprecated/allow_in_tests_deprecated.stderr @@ -0,0 +1,18 @@ +warning: `allow-panic-in-tests` is deprecated + --> $DIR/tests/ui-toml/allow_in_tests_deprecated/clippy.toml:2:1 + | +LL | allow-panic-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["panic"]` instead, which works for any lint + +warning: `allow-print-in-tests` is deprecated + --> $DIR/tests/ui-toml/allow_in_tests_deprecated/clippy.toml:3:1 + | +LL | allow-print-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["print_stderr", "print_stdout"]` instead, which works for any lint + +warning: 2 warnings emitted + diff --git a/tests/ui-toml/allow_in_tests_deprecated/clippy.toml b/tests/ui-toml/allow_in_tests_deprecated/clippy.toml new file mode 100644 index 000000000000..3206d1450eba --- /dev/null +++ b/tests/ui-toml/allow_in_tests_deprecated/clippy.toml @@ -0,0 +1,9 @@ +# Superseded by `allow-in-tests`; enabling these warns. +allow-panic-in-tests = true +allow-print-in-tests = true + +# Explicitly disabling is the default and has no `allow-in-tests` equivalent, so it stays quiet. +allow-dbg-in-tests = false + +# Not superseded: `false` can't be expressed with `allow-in-tests`. +allow-large-stack-frames-in-tests = true diff --git a/tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.rs b/tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.rs new file mode 100644 index 000000000000..a09164c02df6 --- /dev/null +++ b/tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.rs @@ -0,0 +1,6 @@ +//@no-rustfix +//@error-in-other-file: unknown lint: `unwrap_use` +//@error-in-other-file: unknown lint: `clippy::stutter` +//@error-in-other-file: unknown lint: `unused_variables` + +fn main() {} diff --git a/tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.stderr b/tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.stderr new file mode 100644 index 000000000000..f8e5abd446f7 --- /dev/null +++ b/tests/ui-toml/allow_in_tests_unknown/allow_in_tests_unknown.stderr @@ -0,0 +1,26 @@ +warning: unknown lint: `unwrap_use` + --> $DIR/tests/ui-toml/allow_in_tests_unknown/clippy.toml:3:5 + | +LL | "unwrap_use", + | ^^^^^^^^^^^^ help: did you mean: `"unwrap_used"` + | + = note: `allow-in-tests` only accepts Clippy lints + +warning: unknown lint: `clippy::stutter` + --> $DIR/tests/ui-toml/allow_in_tests_unknown/clippy.toml:5:5 + | +LL | "clippy::stutter", + | ^^^^^^^^^^^^^^^^^ help: `clippy::stutter` has been renamed: `"module_name_repetitions"` + | + = note: `allow-in-tests` only accepts Clippy lints + +warning: unknown lint: `unused_variables` + --> $DIR/tests/ui-toml/allow_in_tests_unknown/clippy.toml:7:5 + | +LL | "unused_variables", + | ^^^^^^^^^^^^^^^^^^ help: did you mean: `"unused_peekable"` + | + = note: `allow-in-tests` only accepts Clippy lints + +warning: 3 warnings emitted + diff --git a/tests/ui-toml/allow_in_tests_unknown/clippy.toml b/tests/ui-toml/allow_in_tests_unknown/clippy.toml new file mode 100644 index 000000000000..4cf5669a7b24 --- /dev/null +++ b/tests/ui-toml/allow_in_tests_unknown/clippy.toml @@ -0,0 +1,8 @@ +allow-in-tests = [ + # not a lint at all + "unwrap_use", + # a renamed lint + "clippy::stutter", + # a rustc lint + "unused_variables", +] diff --git a/tests/ui-toml/dbg_macro/dbg_macro.fixed b/tests/ui-toml/dbg_macro/dbg_macro.fixed index 08a8437e1050..335f4e1b1c64 100644 --- a/tests/ui-toml/dbg_macro/dbg_macro.fixed +++ b/tests/ui-toml/dbg_macro/dbg_macro.fixed @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-dbg-in-tests` is deprecated #![warn(clippy::dbg_macro)] #![allow(clippy::no_effect, clippy::unnecessary_operation)] diff --git a/tests/ui-toml/dbg_macro/dbg_macro.rs b/tests/ui-toml/dbg_macro/dbg_macro.rs index 1286f696fcd0..ef294ea0fe37 100644 --- a/tests/ui-toml/dbg_macro/dbg_macro.rs +++ b/tests/ui-toml/dbg_macro/dbg_macro.rs @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-dbg-in-tests` is deprecated #![warn(clippy::dbg_macro)] #![allow(clippy::no_effect, clippy::unnecessary_operation)] diff --git a/tests/ui-toml/dbg_macro/dbg_macro.stderr b/tests/ui-toml/dbg_macro/dbg_macro.stderr index 4587d28c3f34..d4a62664b9b4 100644 --- a/tests/ui-toml/dbg_macro/dbg_macro.stderr +++ b/tests/ui-toml/dbg_macro/dbg_macro.stderr @@ -1,5 +1,13 @@ +warning: `allow-dbg-in-tests` is deprecated + --> $DIR/tests/ui-toml/dbg_macro/clippy.toml:1:1 + | +LL | allow-dbg-in-tests = true + | ^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["dbg_macro"]` instead, which works for any lint + error: the `dbg!` macro is intended as a debugging tool - --> tests/ui-toml/dbg_macro/dbg_macro.rs:6:22 + --> tests/ui-toml/dbg_macro/dbg_macro.rs:7:22 | LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n } | ^^^^^^^^^^^^^^^^^^^^^^ @@ -13,7 +21,7 @@ LL + if let Some(n) = n.checked_sub(4) { n } else { n } | error: the `dbg!` macro is intended as a debugging tool - --> tests/ui-toml/dbg_macro/dbg_macro.rs:11:8 + --> tests/ui-toml/dbg_macro/dbg_macro.rs:12:8 | LL | if dbg!(n <= 1) { | ^^^^^^^^^^^^ @@ -25,7 +33,7 @@ LL + if n <= 1 { | error: the `dbg!` macro is intended as a debugging tool - --> tests/ui-toml/dbg_macro/dbg_macro.rs:13:9 + --> tests/ui-toml/dbg_macro/dbg_macro.rs:14:9 | LL | dbg!(1) | ^^^^^^^ @@ -37,7 +45,7 @@ LL + 1 | error: the `dbg!` macro is intended as a debugging tool - --> tests/ui-toml/dbg_macro/dbg_macro.rs:16:9 + --> tests/ui-toml/dbg_macro/dbg_macro.rs:17:9 | LL | dbg!(n * factorial(n - 1)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -49,7 +57,7 @@ LL + n * factorial(n - 1) | error: the `dbg!` macro is intended as a debugging tool - --> tests/ui-toml/dbg_macro/dbg_macro.rs:22:5 + --> tests/ui-toml/dbg_macro/dbg_macro.rs:23:5 | LL | dbg!(42); | ^^^^^^^^ @@ -61,7 +69,7 @@ LL + 42; | error: the `dbg!` macro is intended as a debugging tool - --> tests/ui-toml/dbg_macro/dbg_macro.rs:24:14 + --> tests/ui-toml/dbg_macro/dbg_macro.rs:25:14 | LL | foo(3) + dbg!(factorial(4)); | ^^^^^^^^^^^^^^^^^^ @@ -73,7 +81,7 @@ LL + foo(3) + factorial(4); | error: the `dbg!` macro is intended as a debugging tool - --> tests/ui-toml/dbg_macro/dbg_macro.rs:26:5 + --> tests/ui-toml/dbg_macro/dbg_macro.rs:27:5 | LL | dbg!(1, 2, 3, 4, 5); | ^^^^^^^^^^^^^^^^^^^ @@ -84,5 +92,5 @@ LL - dbg!(1, 2, 3, 4, 5); LL + (1, 2, 3, 4, 5); | -error: aborting due to 7 previous errors +error: aborting due to 7 previous errors; 1 warning emitted diff --git a/tests/ui-toml/expect_used/expect_used.rs b/tests/ui-toml/expect_used/expect_used.rs index 143b2cd28035..20d8e69ff163 100644 --- a/tests/ui-toml/expect_used/expect_used.rs +++ b/tests/ui-toml/expect_used/expect_used.rs @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-expect-in-tests` is deprecated #![warn(clippy::expect_used)] #![allow(clippy::unnecessary_literal_unwrap)] diff --git a/tests/ui-toml/expect_used/expect_used.stderr b/tests/ui-toml/expect_used/expect_used.stderr index b794c2fc42fc..0a56a8ee6776 100644 --- a/tests/ui-toml/expect_used/expect_used.stderr +++ b/tests/ui-toml/expect_used/expect_used.stderr @@ -1,5 +1,13 @@ +warning: `allow-expect-in-tests` is deprecated + --> $DIR/tests/ui-toml/expect_used/clippy.toml:2:1 + | +LL | allow-expect-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["expect_used"]` instead, which works for any lint + error: used `expect()` on an `Option` value - --> tests/ui-toml/expect_used/expect_used.rs:7:13 + --> tests/ui-toml/expect_used/expect_used.rs:8:13 | LL | let _ = opt.expect(""); | ^^^^^^^^^^^^^^ @@ -9,7 +17,7 @@ LL | let _ = opt.expect(""); = help: to override `-D warnings` add `#[allow(clippy::expect_used)]` error: used `expect()` on a `Result` value - --> tests/ui-toml/expect_used/expect_used.rs:13:13 + --> tests/ui-toml/expect_used/expect_used.rs:14:13 | LL | let _ = res.expect(""); | ^^^^^^^^^^^^^^ @@ -17,7 +25,7 @@ LL | let _ = res.expect(""); = note: if this value is an `Err`, it will panic error: used `expect()` on an `Option` value - --> tests/ui-toml/expect_used/expect_used.rs:19:28 + --> tests/ui-toml/expect_used/expect_used.rs:20:28 | LL | const UNWRAPPED: i32 = SOME.expect("Not three?"); | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -25,12 +33,12 @@ LL | const UNWRAPPED: i32 = SOME.expect("Not three?"); = note: if this value is `None`, it will panic error: used `expect()` on an `Option` value - --> tests/ui-toml/expect_used/expect_used.rs:22:9 + --> tests/ui-toml/expect_used/expect_used.rs:23:9 | LL | SOME.expect("Still not three?"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is `None`, it will panic -error: aborting due to 4 previous errors +error: aborting due to 4 previous errors; 1 warning emitted diff --git a/tests/ui-toml/indexing_slicing/indexing_slicing.rs b/tests/ui-toml/indexing_slicing/indexing_slicing.rs index dd741be91d29..eb239342ba31 100644 --- a/tests/ui-toml/indexing_slicing/indexing_slicing.rs +++ b/tests/ui-toml/indexing_slicing/indexing_slicing.rs @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-indexing-slicing-in-tests` is deprecated #![warn(clippy::indexing_slicing)] #![allow(clippy::no_effect)] diff --git a/tests/ui-toml/indexing_slicing/indexing_slicing.stderr b/tests/ui-toml/indexing_slicing/indexing_slicing.stderr index 5a4de8337b46..2a812bfd0569 100644 --- a/tests/ui-toml/indexing_slicing/indexing_slicing.stderr +++ b/tests/ui-toml/indexing_slicing/indexing_slicing.stderr @@ -1,5 +1,13 @@ +warning: `allow-indexing-slicing-in-tests` is deprecated + --> $DIR/tests/ui-toml/indexing_slicing/clippy.toml:1:1 + | +LL | allow-indexing-slicing-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["indexing_slicing"]` instead, which works for any lint + error: slicing may panic - --> tests/ui-toml/indexing_slicing/indexing_slicing.rs:8:6 + --> tests/ui-toml/indexing_slicing/indexing_slicing.rs:9:6 | LL | &x[index..]; | ^^^^^^^^^^ @@ -8,5 +16,5 @@ LL | &x[index..]; = note: `-D clippy::indexing-slicing` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]` -error: aborting due to 1 previous error +error: aborting due to 1 previous error; 1 warning emitted diff --git a/tests/ui-toml/panic/panic.rs b/tests/ui-toml/panic/panic.rs index 7bdc896453d8..6ee05bdd50b9 100644 --- a/tests/ui-toml/panic/panic.rs +++ b/tests/ui-toml/panic/panic.rs @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-panic-in-tests` is deprecated #![warn(clippy::panic)] use std::panic::panic_any; diff --git a/tests/ui-toml/panic/panic.stderr b/tests/ui-toml/panic/panic.stderr index 5dfecfe51ddb..cd146419cb93 100644 --- a/tests/ui-toml/panic/panic.stderr +++ b/tests/ui-toml/panic/panic.stderr @@ -1,5 +1,13 @@ +warning: `allow-panic-in-tests` is deprecated + --> $DIR/tests/ui-toml/panic/clippy.toml:1:1 + | +LL | allow-panic-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["panic"]` instead, which works for any lint + error: `panic` should not be present in production code - --> tests/ui-toml/panic/panic.rs:12:14 + --> tests/ui-toml/panic/panic.rs:13:14 | LL | _ => panic!(""), | ^^^^^^^^^^ @@ -8,10 +16,10 @@ LL | _ => panic!(""), = help: to override `-D warnings` add `#[allow(clippy::panic)]` error: `panic_any` should not be present in production code - --> tests/ui-toml/panic/panic.rs:18:5 + --> tests/ui-toml/panic/panic.rs:19:5 | LL | panic_any("should lint") | ^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 2 previous errors +error: aborting due to 2 previous errors; 1 warning emitted diff --git a/tests/ui-toml/print_macro/print_macro.rs b/tests/ui-toml/print_macro/print_macro.rs index 2ee8719d1c60..8fbbdb9869da 100644 --- a/tests/ui-toml/print_macro/print_macro.rs +++ b/tests/ui-toml/print_macro/print_macro.rs @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-print-in-tests` is deprecated #![warn(clippy::print_stderr, clippy::print_stdout)] fn foo(n: u32) { diff --git a/tests/ui-toml/print_macro/print_macro.stderr b/tests/ui-toml/print_macro/print_macro.stderr index e2a284cb7ec8..6ba5148b744f 100644 --- a/tests/ui-toml/print_macro/print_macro.stderr +++ b/tests/ui-toml/print_macro/print_macro.stderr @@ -1,5 +1,13 @@ +warning: `allow-print-in-tests` is deprecated + --> $DIR/tests/ui-toml/print_macro/clippy.toml:1:1 + | +LL | allow-print-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["print_stderr", "print_stdout"]` instead, which works for any lint + error: use of `print!` - --> tests/ui-toml/print_macro/print_macro.rs:5:5 + --> tests/ui-toml/print_macro/print_macro.rs:6:5 | LL | print!("{n}"); | ^^^^^^^^^^^^^ @@ -8,7 +16,7 @@ LL | print!("{n}"); = help: to override `-D warnings` add `#[allow(clippy::print_stdout)]` error: use of `eprint!` - --> tests/ui-toml/print_macro/print_macro.rs:7:5 + --> tests/ui-toml/print_macro/print_macro.rs:8:5 | LL | eprint!("{n}"); | ^^^^^^^^^^^^^^ @@ -16,5 +24,5 @@ LL | eprint!("{n}"); = note: `-D clippy::print-stderr` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::print_stderr)]` -error: aborting due to 2 previous errors +error: aborting due to 2 previous errors; 1 warning emitted diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index bee9e63ef742..588dadfb2124 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -14,6 +14,7 @@ LL | foobar = 42 allow-exact-repetitions allow-expect-in-consts allow-expect-in-tests + allow-in-tests allow-indexing-slicing-in-tests allow-large-stack-frames-in-tests allow-mixed-uninlined-format-args diff --git a/tests/ui-toml/unwrap_used/unwrap_used.fixed b/tests/ui-toml/unwrap_used/unwrap_used.fixed index 887fcc46f155..7c69d5cad426 100644 --- a/tests/ui-toml/unwrap_used/unwrap_used.fixed +++ b/tests/ui-toml/unwrap_used/unwrap_used.fixed @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-unwrap-in-tests` is deprecated #![allow(clippy::get_first, clippy::useless_vec)] #![warn(clippy::get_unwrap, clippy::unwrap_used)] diff --git a/tests/ui-toml/unwrap_used/unwrap_used.rs b/tests/ui-toml/unwrap_used/unwrap_used.rs index 8916d694c64a..593f37c02d89 100644 --- a/tests/ui-toml/unwrap_used/unwrap_used.rs +++ b/tests/ui-toml/unwrap_used/unwrap_used.rs @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-unwrap-in-tests` is deprecated #![allow(clippy::get_first, clippy::useless_vec)] #![warn(clippy::get_unwrap, clippy::unwrap_used)] diff --git a/tests/ui-toml/unwrap_used/unwrap_used.stderr b/tests/ui-toml/unwrap_used/unwrap_used.stderr index f38cbbd4e4d3..bc2396b1ee26 100644 --- a/tests/ui-toml/unwrap_used/unwrap_used.stderr +++ b/tests/ui-toml/unwrap_used/unwrap_used.stderr @@ -1,5 +1,13 @@ +warning: `allow-unwrap-in-tests` is deprecated + --> $DIR/tests/ui-toml/unwrap_used/clippy.toml:2:1 + | +LL | allow-unwrap-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["unwrap_used"]` instead, which works for any lint + error: called `.get().unwrap()` on a slice - --> tests/ui-toml/unwrap_used/unwrap_used.rs:32:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:33:17 | LL | let _ = boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -13,7 +21,7 @@ LL + let _ = &boxed_slice[1]; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:32:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:33:17 | LL | let _ = boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -24,7 +32,7 @@ LL | let _ = boxed_slice.get(1).unwrap(); = help: to override `-D warnings` add `#[allow(clippy::unwrap_used)]` error: called `.get().unwrap()` on a slice - --> tests/ui-toml/unwrap_used/unwrap_used.rs:35:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:36:17 | LL | let _ = some_slice.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -36,7 +44,7 @@ LL + let _ = &some_slice[0]; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:35:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:36:17 | LL | let _ = some_slice.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -45,7 +53,7 @@ LL | let _ = some_slice.get(0).unwrap(); = help: consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a Vec - --> tests/ui-toml/unwrap_used/unwrap_used.rs:38:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:39:17 | LL | let _ = some_vec.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -57,7 +65,7 @@ LL + let _ = &some_vec[0]; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:38:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:39:17 | LL | let _ = some_vec.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -66,7 +74,7 @@ LL | let _ = some_vec.get(0).unwrap(); = help: consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a VecDeque - --> tests/ui-toml/unwrap_used/unwrap_used.rs:41:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:42:17 | LL | let _ = some_vecdeque.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -78,7 +86,7 @@ LL + let _ = &some_vecdeque[0]; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:41:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:42:17 | LL | let _ = some_vecdeque.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -87,7 +95,7 @@ LL | let _ = some_vecdeque.get(0).unwrap(); = help: consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a HashMap - --> tests/ui-toml/unwrap_used/unwrap_used.rs:44:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:45:17 | LL | let _ = some_hashmap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -99,7 +107,7 @@ LL + let _ = &some_hashmap[&1]; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:44:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:45:17 | LL | let _ = some_hashmap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -108,7 +116,7 @@ LL | let _ = some_hashmap.get(&1).unwrap(); = help: consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a BTreeMap - --> tests/ui-toml/unwrap_used/unwrap_used.rs:47:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:48:17 | LL | let _ = some_btreemap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -120,7 +128,7 @@ LL + let _ = &some_btreemap[&1]; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:47:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:48:17 | LL | let _ = some_btreemap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -129,7 +137,7 @@ LL | let _ = some_btreemap.get(&1).unwrap(); = help: consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a slice - --> tests/ui-toml/unwrap_used/unwrap_used.rs:53:21 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:54:21 | LL | let _: u8 = *boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -141,7 +149,7 @@ LL + let _: u8 = boxed_slice[1]; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:53:22 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:54:22 | LL | let _: u8 = *boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -150,7 +158,7 @@ LL | let _: u8 = *boxed_slice.get(1).unwrap(); = help: consider using `expect()` to provide a better panic message error: called `.get_mut().unwrap()` on a slice - --> tests/ui-toml/unwrap_used/unwrap_used.rs:60:9 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:61:9 | LL | *boxed_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -162,7 +170,7 @@ LL + boxed_slice[0] = 1; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:60:10 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:61:10 | LL | *boxed_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -171,7 +179,7 @@ LL | *boxed_slice.get_mut(0).unwrap() = 1; = help: consider using `expect()` to provide a better panic message error: called `.get_mut().unwrap()` on a slice - --> tests/ui-toml/unwrap_used/unwrap_used.rs:63:9 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:64:9 | LL | *some_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -183,7 +191,7 @@ LL + some_slice[0] = 1; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:63:10 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:64:10 | LL | *some_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -192,7 +200,7 @@ LL | *some_slice.get_mut(0).unwrap() = 1; = help: consider using `expect()` to provide a better panic message error: called `.get_mut().unwrap()` on a Vec - --> tests/ui-toml/unwrap_used/unwrap_used.rs:66:9 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:67:9 | LL | *some_vec.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -204,7 +212,7 @@ LL + some_vec[0] = 1; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:66:10 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:67:10 | LL | *some_vec.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -213,7 +221,7 @@ LL | *some_vec.get_mut(0).unwrap() = 1; = help: consider using `expect()` to provide a better panic message error: called `.get_mut().unwrap()` on a VecDeque - --> tests/ui-toml/unwrap_used/unwrap_used.rs:69:9 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:70:9 | LL | *some_vecdeque.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -225,7 +233,7 @@ LL + some_vecdeque[0] = 1; | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:69:10 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:70:10 | LL | *some_vecdeque.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -234,7 +242,7 @@ LL | *some_vecdeque.get_mut(0).unwrap() = 1; = help: consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a Vec - --> tests/ui-toml/unwrap_used/unwrap_used.rs:83:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:84:17 | LL | let _ = some_vec.get(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -246,7 +254,7 @@ LL + let _ = some_vec[0..1].to_vec(); | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:83:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:84:17 | LL | let _ = some_vec.get(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -255,7 +263,7 @@ LL | let _ = some_vec.get(0..1).unwrap().to_vec(); = help: consider using `expect()` to provide a better panic message error: called `.get_mut().unwrap()` on a Vec - --> tests/ui-toml/unwrap_used/unwrap_used.rs:86:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:87:17 | LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -267,7 +275,7 @@ LL + let _ = some_vec[0..1].to_vec(); | error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used.rs:86:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:87:17 | LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -276,7 +284,7 @@ LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); = help: consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a slice - --> tests/ui-toml/unwrap_used/unwrap_used.rs:95:13 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:96:13 | LL | let _ = boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -288,7 +296,7 @@ LL + let _ = &boxed_slice[1]; | error: called `.get().unwrap()` on a slice - --> tests/ui-toml/unwrap_used/unwrap_used.rs:115:17 + --> tests/ui-toml/unwrap_used/unwrap_used.rs:116:17 | LL | let _ = Box::new([0]).get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -299,5 +307,5 @@ LL - let _ = Box::new([0]).get(1).unwrap(); LL + let _ = &Box::new([0])[1]; | -error: aborting due to 28 previous errors +error: aborting due to 28 previous errors; 1 warning emitted diff --git a/tests/ui-toml/unwrap_used/unwrap_used_const.rs b/tests/ui-toml/unwrap_used/unwrap_used_const.rs index c7d8e8c1ffb5..5e06cc41af82 100644 --- a/tests/ui-toml/unwrap_used/unwrap_used_const.rs +++ b/tests/ui-toml/unwrap_used/unwrap_used_const.rs @@ -1,3 +1,4 @@ +//@error-in-other-file: `allow-unwrap-in-tests` is deprecated #![warn(clippy::unwrap_used)] fn main() { diff --git a/tests/ui-toml/unwrap_used/unwrap_used_const.stderr b/tests/ui-toml/unwrap_used/unwrap_used_const.stderr index 362b41bc1bff..7a072fec9596 100644 --- a/tests/ui-toml/unwrap_used/unwrap_used_const.stderr +++ b/tests/ui-toml/unwrap_used/unwrap_used_const.stderr @@ -1,5 +1,13 @@ +warning: `allow-unwrap-in-tests` is deprecated + --> $DIR/tests/ui-toml/unwrap_used/clippy.toml:2:1 + | +LL | allow-unwrap-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["unwrap_used"]` instead, which works for any lint + error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used_const.rs:5:28 + --> tests/ui-toml/unwrap_used/unwrap_used_const.rs:6:28 | LL | const UNWRAPPED: i32 = SOME.unwrap(); | ^^^^^^^^^^^^^ @@ -10,7 +18,7 @@ LL | const UNWRAPPED: i32 = SOME.unwrap(); = help: to override `-D warnings` add `#[allow(clippy::unwrap_used)]` error: used `unwrap()` on an `Option` value - --> tests/ui-toml/unwrap_used/unwrap_used_const.rs:8:9 + --> tests/ui-toml/unwrap_used/unwrap_used_const.rs:9:9 | LL | SOME.unwrap(); | ^^^^^^^^^^^^^ @@ -18,5 +26,5 @@ LL | SOME.unwrap(); = note: if this value is `None`, it will panic = help: consider using `expect()` to provide a better panic message -error: aborting due to 2 previous errors +error: aborting due to 2 previous errors; 1 warning emitted diff --git a/tests/ui-toml/useless_vec/useless_vec.fixed b/tests/ui-toml/useless_vec/useless_vec.fixed index ece5eb42b5b0..97a69b17786a 100644 --- a/tests/ui-toml/useless_vec/useless_vec.fixed +++ b/tests/ui-toml/useless_vec/useless_vec.fixed @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-useless-vec-in-tests` is deprecated #![warn(clippy::useless_vec)] #![allow(clippy::no_effect, clippy::unnecessary_operation)] diff --git a/tests/ui-toml/useless_vec/useless_vec.rs b/tests/ui-toml/useless_vec/useless_vec.rs index ccbea21a2ba8..e4a2eea3da59 100644 --- a/tests/ui-toml/useless_vec/useless_vec.rs +++ b/tests/ui-toml/useless_vec/useless_vec.rs @@ -1,4 +1,5 @@ //@compile-flags: --test +//@error-in-other-file: `allow-useless-vec-in-tests` is deprecated #![warn(clippy::useless_vec)] #![allow(clippy::no_effect, clippy::unnecessary_operation)] diff --git a/tests/ui-toml/useless_vec/useless_vec.stderr b/tests/ui-toml/useless_vec/useless_vec.stderr index 633110c3c8d9..5a2428a54970 100644 --- a/tests/ui-toml/useless_vec/useless_vec.stderr +++ b/tests/ui-toml/useless_vec/useless_vec.stderr @@ -1,5 +1,13 @@ +warning: `allow-useless-vec-in-tests` is deprecated + --> $DIR/tests/ui-toml/useless_vec/clippy.toml:1:1 + | +LL | allow-useless-vec-in-tests = true + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: use `allow-in-tests = ["useless_vec"]` instead, which works for any lint + error: useless use of `vec!` - --> tests/ui-toml/useless_vec/useless_vec.rs:8:9 + --> tests/ui-toml/useless_vec/useless_vec.rs:9:9 | LL | foo(&vec![1_u32]); | ^^^^^^^^^^^^ help: you can use a slice directly: `&[1_u32]` @@ -7,5 +15,5 @@ LL | foo(&vec![1_u32]); = note: `-D clippy::useless-vec` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_vec)]` -error: aborting due to 1 previous error +error: aborting due to 1 previous error; 1 warning emitted