diff options
| author | infrandomness <infrandomness@gmail.com> | 2022-04-08 21:54:44 +0200 |
|---|---|---|
| committer | infrandomness <infrandomness@gmail.com> | 2022-04-14 13:15:51 +0200 |
| commit | f8f144117da48b52f2c65292402dedcb1c59d9c1 (patch) | |
| tree | 6936820c4c07bfa546739fa45b39d4d57aa25050 | |
| parent | ee9281d7a241c448f6bcbbb6cb8bdf8b17251a92 (diff) | |
| download | rust-f8f144117da48b52f2c65292402dedcb1c59d9c1.tar.gz rust-f8f144117da48b52f2c65292402dedcb1c59d9c1.zip | |
Swap span_lint for span_lint_and_sugg
This implements a machine applicable suggestion to any matched usage of `.as_ref().take()``
| -rw-r--r-- | clippy_lints/src/methods/needless_option_take.rs | 31 | ||||
| -rw-r--r-- | tests/ui/needless_option_take.rs | 2 | ||||
| -rw-r--r-- | tests/ui/option_take_on_temporary.fixed | 7 |
3 files changed, 26 insertions, 14 deletions
diff --git a/clippy_lints/src/methods/needless_option_take.rs b/clippy_lints/src/methods/needless_option_take.rs index 27ef9009328..57a43ff606a 100644 --- a/clippy_lints/src/methods/needless_option_take.rs +++ b/clippy_lints/src/methods/needless_option_take.rs @@ -1,27 +1,30 @@ -use clippy_utils::diagnostics::span_lint; +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet_with_applicability; use clippy_utils::ty::is_type_diagnostic_item; +use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; use rustc_span::sym; use super::NEEDLESS_OPTION_TAKE; -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, _recv: &'tcx Expr<'_>) { +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) { // Checks if expression type is equal to sym::Option and if the expr is not a syntactic place if is_expr_option(cx, expr) && !expr.is_syntactic_place_expr() { - span_lint(cx, OPTION_TAKE_ON_TEMPORARY, expr.span, "Format test"); + let mut applicability = Applicability::MachineApplicable; + span_lint_and_sugg( + cx, + NEEDLESS_OPTION_TAKE, + expr.span, + "Called `Option::take()` on a temporary value", + "try", + format!( + "{}", + snippet_with_applicability(cx, recv.span, "..", &mut applicability) + ), + applicability, + ); } - /* if_chain! { - is_expr_option(cx, expr); - then { - span_lint( - cx, - NEEDLESS_OPTION_TAKE, - expr.span, - "Format test" - ); - } - };*/ } fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { diff --git a/tests/ui/needless_option_take.rs b/tests/ui/needless_option_take.rs index 2b32b9467f5..eb57ffeab45 100644 --- a/tests/ui/needless_option_take.rs +++ b/tests/ui/needless_option_take.rs @@ -1,3 +1,5 @@ +// run-rustfix + fn main() { println!("Testing option_take_on_temporary"); let x = Some(3); diff --git a/tests/ui/option_take_on_temporary.fixed b/tests/ui/option_take_on_temporary.fixed new file mode 100644 index 00000000000..9b4ca5a4ad1 --- /dev/null +++ b/tests/ui/option_take_on_temporary.fixed @@ -0,0 +1,7 @@ +// run-rustfix + +fn main() { + println!("Testing option_take_on_temporary"); + let x = Some(3); + let y = x.as_ref(); +} |
