Skip to content

Commit 262b35e

Browse files
committed
Introduce option_take_on_temporary lints
This lint checks if Option::take() is used on a temporary value (a value that is not of type &mut Option and that is not a Place expression) to suggest omitting take()
1 parent 849668a commit 262b35e

10 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3473,6 +3473,7 @@ Released 2018-09-13
34733473
[`needless_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
34743474
[`needless_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_match
34753475
[`needless_option_as_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_as_deref
3476+
[`needless_option_take`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_take
34763477
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
34773478
[`needless_question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
34783479
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
183183
LintId::of(methods::MAP_FLATTEN),
184184
LintId::of(methods::MAP_IDENTITY),
185185
LintId::of(methods::NEEDLESS_OPTION_AS_DEREF),
186+
LintId::of(methods::NEEDLESS_OPTION_TAKE),
186187
LintId::of(methods::NEEDLESS_SPLITN),
187188
LintId::of(methods::NEW_RET_NO_SELF),
188189
LintId::of(methods::OK_EXPECT),

clippy_lints/src/lib.register_complexity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
4545
LintId::of(methods::MAP_FLATTEN),
4646
LintId::of(methods::MAP_IDENTITY),
4747
LintId::of(methods::NEEDLESS_OPTION_AS_DEREF),
48+
LintId::of(methods::NEEDLESS_OPTION_TAKE),
4849
LintId::of(methods::NEEDLESS_SPLITN),
4950
LintId::of(methods::OPTION_AS_REF_DEREF),
5051
LintId::of(methods::OPTION_FILTER_MAP),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ store.register_lints(&[
322322
methods::MAP_IDENTITY,
323323
methods::MAP_UNWRAP_OR,
324324
methods::NEEDLESS_OPTION_AS_DEREF,
325+
methods::NEEDLESS_OPTION_TAKE,
325326
methods::NEEDLESS_SPLITN,
326327
methods::NEW_RET_NO_SELF,
327328
methods::OK_EXPECT,

clippy_lints/src/lib.register_suspicious.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec!
2222
LintId::of(loops::EMPTY_LOOP),
2323
LintId::of(loops::FOR_LOOPS_OVER_FALLIBLES),
2424
LintId::of(loops::MUT_RANGE_BOUND),
25+
LintId::of(methods::OPTION_TAKE_ON_TEMPORARY),
2526
LintId::of(methods::SUSPICIOUS_MAP),
2627
LintId::of(mut_key::MUTABLE_KEY_TYPE),
2728
LintId::of(octal_escapes::OCTAL_ESCAPES),

clippy_lints/src/methods/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod map_flatten;
4343
mod map_identity;
4444
mod map_unwrap_or;
4545
mod needless_option_as_deref;
46+
mod needless_option_take;
4647
mod ok_expect;
4748
mod option_as_ref_deref;
4849
mod option_map_or_none;
@@ -2162,6 +2163,24 @@ declare_clippy_lint! {
21622163
"use of `char::is_digit(..)` with literal radix of 10 or 16"
21632164
}
21642165

2166+
declare_clippy_lint! {
2167+
///
2168+
/// ### Why is this bad?
2169+
///
2170+
/// ### Example
2171+
/// ```rust
2172+
/// // example code where clippy issues a warning
2173+
/// ```
2174+
/// Use instead:
2175+
/// ```rust
2176+
/// // example code which does not raise clippy warning
2177+
/// ```
2178+
#[clippy::version = "1.61.0"]
2179+
pub NEEDLESS_OPTION_TAKE,
2180+
suspicious,
2181+
"using `.as_ref().take()` on a temporary value"
2182+
}
2183+
21652184
pub struct Methods {
21662185
avoid_breaking_exported_api: bool,
21672186
msrv: Option<RustcVersion>,
@@ -2251,6 +2270,7 @@ impl_lint_pass!(Methods => [
22512270
ERR_EXPECT,
22522271
NEEDLESS_OPTION_AS_DEREF,
22532272
IS_DIGIT_ASCII_RADIX,
2273+
NEEDLESS_OPTION_TAKE,
22542274
]);
22552275

22562276
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2623,6 +2643,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
26232643
}
26242644
}
26252645
},
2646+
("take", []) => needless_option_take::check(cx, expr, recv),
26262647
("to_os_string" | "to_owned" | "to_path_buf" | "to_vec", []) => {
26272648
implicit_clone::check(cx, name, expr, recv);
26282649
},
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::ty::is_type_diagnostic_item;
3+
use rustc_hir::Expr;
4+
use rustc_lint::LateContext;
5+
use rustc_span::sym::Result as sym_result;
6+
7+
use super::NEEDLESS_OPTION_TAKE;
8+
9+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
10+
if_chain! {
11+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym_result);
12+
let result_type = cx.typeck_results().expr_ty(recv);
13+
14+
then {
15+
span_lint(
16+
cx,
17+
NEEDLESS_OPTION_TAKE,
18+
expr.span,
19+
"Format test"
20+
);
21+
}
22+
};
23+
}

tests/ui/needless_option_take.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
println!("Testing non erroneous option_take_on_temporary");
5+
let mut option = Some(1);
6+
let _ = Box::new(move || option.take().unwrap());
7+
8+
println!("Testing non erroneous option_take_on_temporary");
9+
let x = Some(3);
10+
x.as_ref();
11+
12+
println!("Testing erroneous option_take_on_temporary");
13+
let x = Some(3);
14+
x.as_ref();
15+
}

tests/ui/needless_option_take.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
println!("Testing option_take_on_temporary");
3+
let x = Some(3);
4+
let y = x.as_ref().take();
5+
}

tests/ui/needless_option_take.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: called `Option::take()` on a temporary value
2+
--> $DIR/needless_option_take.rs:14:5
3+
|
4+
LL | x.as_ref().take();
5+
| ^^^^^^^^^^^^^^^^^ help: try: `x.as_ref()`
6+
|
7+
= note: `-D clippy::needless-option-take` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)