diff --git a/clippy_lints/src/partial_pub_fields.rs b/clippy_lints/src/partial_pub_fields.rs index 2d20cbea698f..267e2067e101 100644 --- a/clippy_lints/src/partial_pub_fields.rs +++ b/clippy_lints/src/partial_pub_fields.rs @@ -1,4 +1,4 @@ -use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::diagnostics::span_lint_and_then; use rustc_ast::ast::{Item, ItemKind}; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::declare_lint_pass; @@ -57,24 +57,16 @@ impl EarlyLintPass for PartialPubFields { for field in fields { if all_priv && field.vis.kind.is_pub() { - span_lint_and_help( - cx, - PARTIAL_PUB_FIELDS, - field.vis.span, - msg, - None, - "consider using private field here", - ); + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| { + diag.help("consider using private field here"); + }); return; } else if all_pub && !field.vis.kind.is_pub() { - span_lint_and_help( - cx, - PARTIAL_PUB_FIELDS, - field.vis.span, - msg, - None, - "consider using public field here", - ); + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| { + diag.help("consider using public field here"); + }); return; } } diff --git a/clippy_lints/src/pattern_type_mismatch.rs b/clippy_lints/src/pattern_type_mismatch.rs index 9661a57b8b95..c1296b04387a 100644 --- a/clippy_lints/src/pattern_type_mismatch.rs +++ b/clippy_lints/src/pattern_type_mismatch.rs @@ -1,4 +1,4 @@ -use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::diagnostics::span_lint_and_then; use rustc_hir::{ intravisit, Body, Expr, ExprKind, FnDecl, LetExpr, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind, }; @@ -133,23 +133,25 @@ enum DerefPossible { fn apply_lint(cx: &LateContext<'_>, pat: &Pat<'_>, deref_possible: DerefPossible) -> bool { let maybe_mismatch = find_first_mismatch(cx, pat); if let Some((span, mutability, level)) = maybe_mismatch { - span_lint_and_help( + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then( cx, PATTERN_TYPE_MISMATCH, span, "type of pattern does not match the expression type", - None, - format!( - "{}explicitly match against a `{}` pattern and adjust the enclosed variable bindings", - match (deref_possible, level) { - (DerefPossible::Possible, Level::Top) => "use `*` to dereference the match expression or ", - _ => "", - }, - match mutability { - Mutability::Mut => "&mut _", - Mutability::Not => "&_", - }, - ), + |diag| { + diag.help(format!( + "{}explicitly match against a `{}` pattern and adjust the enclosed variable bindings", + match (deref_possible, level) { + (DerefPossible::Possible, Level::Top) => "use `*` to dereference the match expression or ", + _ => "", + }, + match mutability { + Mutability::Mut => "&mut _", + Mutability::Not => "&_", + }, + )); + }, ); true } else { diff --git a/clippy_lints/src/pub_use.rs b/clippy_lints/src/pub_use.rs index ab8f8a1689dc..5b973a79eae3 100644 --- a/clippy_lints/src/pub_use.rs +++ b/clippy_lints/src/pub_use.rs @@ -1,4 +1,4 @@ -use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::diagnostics::span_lint_and_then; use rustc_ast::ast::{Item, ItemKind, VisibilityKind}; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::declare_lint_pass; @@ -42,14 +42,10 @@ impl EarlyLintPass for PubUse { if let ItemKind::Use(_) = item.kind && let VisibilityKind::Public = item.vis.kind { - span_lint_and_help( - cx, - PUB_USE, - item.span, - "using `pub use`", - None, - "move the exported item to a public module instead", - ); + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then(cx, PUB_USE, item.span, "using `pub use`", |diag| { + diag.help("move the exported item to a public module instead"); + }); } } } diff --git a/clippy_lints/src/unicode.rs b/clippy_lints/src/unicode.rs index d42697b31d1f..e1fc644e4cee 100644 --- a/clippy_lints/src/unicode.rs +++ b/clippy_lints/src/unicode.rs @@ -1,4 +1,4 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::is_lint_allowed; use clippy_utils::macros::span_is_local; use clippy_utils::source::snippet; @@ -105,45 +105,51 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) { let string = snippet(cx, span, ""); if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) { - span_lint_and_sugg( - cx, - INVISIBLE_CHARACTERS, - span, - "invisible character detected", - "consider replacing the string with", - string - .replace('\u{200B}', "\\u{200B}") - .replace('\u{ad}', "\\u{AD}") - .replace('\u{2060}', "\\u{2060}"), - Applicability::MachineApplicable, - ); + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then(cx, INVISIBLE_CHARACTERS, span, "invisible character detected", |diag| { + diag.span_suggestion( + span, + "consider replacing the string with", + string + .replace('\u{200B}', "\\u{200B}") + .replace('\u{ad}', "\\u{AD}") + .replace('\u{2060}', "\\u{2060}"), + Applicability::MachineApplicable, + ); + }); } if string.chars().any(|c| c as u32 > 0x7F) { - span_lint_and_sugg( + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then( cx, NON_ASCII_LITERAL, span, "literal non-ASCII character detected", - "consider replacing the string with", - if is_lint_allowed(cx, UNICODE_NOT_NFC, id) { - escape(string.chars()) - } else { - escape(string.nfc()) + |diag| { + diag.span_suggestion( + span, + "consider replacing the string with", + if is_lint_allowed(cx, UNICODE_NOT_NFC, id) { + escape(string.chars()) + } else { + escape(string.nfc()) + }, + Applicability::MachineApplicable, + ); }, - Applicability::MachineApplicable, ); } if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) { - span_lint_and_sugg( - cx, - UNICODE_NOT_NFC, - span, - "non-NFC Unicode sequence detected", - "consider replacing the string with", - string.nfc().collect::(), - Applicability::MachineApplicable, - ); + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then(cx, UNICODE_NOT_NFC, span, "non-NFC Unicode sequence detected", |diag| { + diag.span_suggestion( + span, + "consider replacing the string with", + string.nfc().collect::(), + Applicability::MachineApplicable, + ); + }); } } diff --git a/clippy_lints/src/visibility.rs b/clippy_lints/src/visibility.rs index 11dcceca7abb..63f3a5d7f830 100644 --- a/clippy_lints/src/visibility.rs +++ b/clippy_lints/src/visibility.rs @@ -1,4 +1,4 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet_opt; use rustc_ast::ast::{Item, VisibilityKind}; use rustc_errors::Applicability; @@ -85,14 +85,19 @@ impl EarlyLintPass for Visibility { if **path == kw::SelfLower && let Some(false) = is_from_proc_macro(cx, item.vis.span) { - span_lint_and_sugg( + span_lint_and_then( cx, NEEDLESS_PUB_SELF, item.vis.span, format!("unnecessary `pub({}self)`", if *shorthand { "" } else { "in " }), - "remove it", - String::new(), - Applicability::MachineApplicable, + |diag| { + diag.span_suggestion_hidden( + item.vis.span, + "remove it", + String::new(), + Applicability::MachineApplicable, + ); + }, ); } @@ -101,14 +106,20 @@ impl EarlyLintPass for Visibility { && let [.., last] = &*path.segments && let Some(false) = is_from_proc_macro(cx, item.vis.span) { - span_lint_and_sugg( + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then( cx, PUB_WITHOUT_SHORTHAND, item.vis.span, "usage of `pub` with `in`", - "remove it", - format!("pub({})", last.ident), - Applicability::MachineApplicable, + |diag| { + diag.span_suggestion( + item.vis.span, + "remove it", + format!("pub({})", last.ident), + Applicability::MachineApplicable, + ); + }, ); } @@ -116,14 +127,20 @@ impl EarlyLintPass for Visibility { && let [.., last] = &*path.segments && let Some(false) = is_from_proc_macro(cx, item.vis.span) { - span_lint_and_sugg( + #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] + span_lint_and_then( cx, PUB_WITH_SHORTHAND, item.vis.span, "usage of `pub` without `in`", - "add it", - format!("pub(in {})", last.ident), - Applicability::MachineApplicable, + |diag| { + diag.span_suggestion( + item.vis.span, + "add it", + format!("pub(in {})", last.ident), + Applicability::MachineApplicable, + ); + }, ); } } diff --git a/tests/ui/needless_pub_self.stderr b/tests/ui/needless_pub_self.stderr index 0bff2e4b8b71..1fdd84165659 100644 --- a/tests/ui/needless_pub_self.stderr +++ b/tests/ui/needless_pub_self.stderr @@ -2,22 +2,27 @@ error: unnecessary `pub(self)` --> tests/ui/needless_pub_self.rs:13:1 | LL | pub(self) fn a() {} - | ^^^^^^^^^ help: remove it + | ^^^^^^^^^ | = note: `-D clippy::needless-pub-self` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_pub_self)]` + = help: remove it error: unnecessary `pub(in self)` --> tests/ui/needless_pub_self.rs:14:1 | LL | pub(in self) fn b() {} - | ^^^^^^^^^^^^ help: remove it + | ^^^^^^^^^^^^ + | + = help: remove it error: unnecessary `pub(self)` --> tests/ui/needless_pub_self.rs:20:5 | LL | pub(self) fn f() {} - | ^^^^^^^^^ help: remove it + | ^^^^^^^^^ + | + = help: remove it error: aborting due to 3 previous errors