diff options
| author | llogiq <bogusandre@gmail.com> | 2025-01-28 18:49:27 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-28 18:49:27 +0000 |
| commit | 9ede32fe0057cba9632355cad67d03567cdc6fc0 (patch) | |
| tree | de50a2b5f154ec4807f60764fa9c6d49f154040f /clippy_lints/src/methods | |
| parent | 51d49c1ae2785b24ef18a46ef233fc1d91844666 (diff) | |
| parent | f0b99b2b388ebfd3b5b2604737d2b39db64f9081 (diff) | |
| download | rust-9ede32fe0057cba9632355cad67d03567cdc6fc0.tar.gz rust-9ede32fe0057cba9632355cad67d03567cdc6fc0.zip | |
`needless_option_take`: add autofix (#14042)
changelog: [`needless_option_take`]: add autofix
Diffstat (limited to 'clippy_lints/src/methods')
| -rw-r--r-- | clippy_lints/src/methods/needless_option_take.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/clippy_lints/src/methods/needless_option_take.rs b/clippy_lints/src/methods/needless_option_take.rs index c41ce2481d7..88b9c69f6f9 100644 --- a/clippy_lints/src/methods/needless_option_take.rs +++ b/clippy_lints/src/methods/needless_option_take.rs @@ -1,5 +1,6 @@ -use clippy_utils::diagnostics::span_lint_and_note; +use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::ty::is_type_diagnostic_item; +use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, QPath}; use rustc_lint::LateContext; use rustc_span::sym; @@ -10,13 +11,22 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &' // Checks if expression type is equal to sym::Option and if the expr is not a syntactic place if !recv.is_syntactic_place_expr() && is_expr_option(cx, recv) { if let Some(function_name) = source_of_temporary_value(recv) { - span_lint_and_note( + span_lint_and_then( cx, NEEDLESS_OPTION_TAKE, expr.span, "called `Option::take()` on a temporary value", - None, - format!("`{function_name}` creates a temporary value, so calling take() has no effect"), + |diag| { + diag.note(format!( + "`{function_name}` creates a temporary value, so calling take() has no effect" + )); + diag.span_suggestion( + expr.span.with_lo(recv.span.hi()), + "remove", + "", + Applicability::MachineApplicable, + ); + }, ); } } |
