Skip to content

Commit

Permalink
needless_option_take: add autofix (#14042)
Browse files Browse the repository at this point in the history
changelog: [`needless_option_take`]: add autofix
  • Loading branch information
llogiq authored Jan 28, 2025
2 parents 51d49c1 + f0b99b2 commit 9ede32f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 13 deletions.
18 changes: 14 additions & 4 deletions clippy_lints/src/methods/needless_option_take.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
);
},
);
}
}
Expand Down
58 changes: 58 additions & 0 deletions tests/ui/needless_option_take.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
struct MyStruct;

impl MyStruct {
pub fn get_option() -> Option<Self> {
todo!()
}
}

fn return_option() -> Option<i32> {
todo!()
}

fn main() {
println!("Testing non erroneous option_take_on_temporary");
let mut option = Some(1);
let _ = Box::new(move || option.take().unwrap());

println!("Testing non erroneous option_take_on_temporary");
let x = Some(3);
x.as_ref();

let x = Some(3);
x.as_ref();
//~^ ERROR: called `Option::take()` on a temporary value

println!("Testing non erroneous option_take_on_temporary");
let mut x = Some(3);
let y = x.as_mut();

let mut x = Some(3);
let y = x.as_mut();
//~^ ERROR: called `Option::take()` on a temporary value
let y = x.replace(289);
//~^ ERROR: called `Option::take()` on a temporary value

let y = Some(3).as_mut();
//~^ ERROR: called `Option::take()` on a temporary value

let y = Option::as_mut(&mut x);
//~^ ERROR: called `Option::take()` on a temporary value

let x = return_option();
let x = return_option();
//~^ ERROR: called `Option::take()` on a temporary value

let x = MyStruct::get_option();
let x = MyStruct::get_option();
//~^ ERROR: called `Option::take()` on a temporary value

let mut my_vec = vec![1, 2, 3];
my_vec.push(4);
let y = my_vec.first();
let y = my_vec.first();
//~^ ERROR: called `Option::take()` on a temporary value

let y = my_vec.first();
//~^ ERROR: called `Option::take()` on a temporary value
}
36 changes: 27 additions & 9 deletions tests/ui/needless_option_take.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:23:5
|
LL | x.as_ref().take();
| ^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^-------
| |
| help: remove
|
= note: `as_ref` creates a temporary value, so calling take() has no effect
= note: `-D clippy::needless-option-take` implied by `-D warnings`
Expand All @@ -12,63 +14,79 @@ error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:31:13
|
LL | let y = x.as_mut().take();
| ^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^-------
| |
| help: remove
|
= note: `as_mut` creates a temporary value, so calling take() has no effect

error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:33:13
|
LL | let y = x.replace(289).take();
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^-------
| |
| help: remove
|
= note: `replace` creates a temporary value, so calling take() has no effect

error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:36:13
|
LL | let y = Some(3).as_mut().take();
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^-------
| |
| help: remove
|
= note: `as_mut` creates a temporary value, so calling take() has no effect

error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:39:13
|
LL | let y = Option::as_mut(&mut x).take();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^-------
| |
| help: remove
|
= note: `as_mut` creates a temporary value, so calling take() has no effect

error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:43:13
|
LL | let x = return_option().take();
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^-------
| |
| help: remove
|
= note: `return_option` creates a temporary value, so calling take() has no effect

error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:47:13
|
LL | let x = MyStruct::get_option().take();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^-------
| |
| help: remove
|
= note: `get_option` creates a temporary value, so calling take() has no effect

error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:53:13
|
LL | let y = my_vec.first().take();
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^-------
| |
| help: remove
|
= note: `first` creates a temporary value, so calling take() has no effect

error: called `Option::take()` on a temporary value
--> tests/ui/needless_option_take.rs:56:13
|
LL | let y = my_vec.first().take();
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^-------
| |
| help: remove
|
= note: `first` creates a temporary value, so calling take() has no effect

Expand Down

0 comments on commit 9ede32f

Please sign in to comment.