Skip to content

Commit e5ebece

Browse files
committed
Auto merge of #8665 - InfRandomness:option_take_on_temporary, r=llogiq
Introduce needless_option_take lint - \[x] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` Fixes #8618 changelog: Introduce [`needless_option_take`] lint
2 parents 0f4f9ec + 2903b56 commit e5ebece

10 files changed

+123
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3575,6 +3575,7 @@ Released 2018-09-13
35753575
[`needless_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
35763576
[`needless_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_match
35773577
[`needless_option_as_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_as_deref
3578+
[`needless_option_take`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_take
35783579
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
35793580
[`needless_question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
35803581
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop

clippy_lints/src/lib.register_all.rs

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
184184
LintId::of(methods::MAP_FLATTEN),
185185
LintId::of(methods::MAP_IDENTITY),
186186
LintId::of(methods::NEEDLESS_OPTION_AS_DEREF),
187+
LintId::of(methods::NEEDLESS_OPTION_TAKE),
187188
LintId::of(methods::NEEDLESS_SPLITN),
188189
LintId::of(methods::NEW_RET_NO_SELF),
189190
LintId::of(methods::OK_EXPECT),

clippy_lints/src/lib.register_complexity.rs

+1
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

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ store.register_lints(&[
323323
methods::MAP_IDENTITY,
324324
methods::MAP_UNWRAP_OR,
325325
methods::NEEDLESS_OPTION_AS_DEREF,
326+
methods::NEEDLESS_OPTION_TAKE,
326327
methods::NEEDLESS_SPLITN,
327328
methods::NEW_RET_NO_SELF,
328329
methods::OK_EXPECT,

clippy_lints/src/methods/mod.rs

+23
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,26 @@ 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+
/// let x = Some(3);
2173+
/// x.as_ref().take();
2174+
/// ```
2175+
/// Use instead:
2176+
/// ```rust
2177+
/// let x = Some(3);
2178+
/// x.as_ref();
2179+
/// ```
2180+
#[clippy::version = "1.61.0"]
2181+
pub NEEDLESS_OPTION_TAKE,
2182+
complexity,
2183+
"using `.as_ref().take()` on a temporary value"
2184+
}
2185+
21652186
pub struct Methods {
21662187
avoid_breaking_exported_api: bool,
21672188
msrv: Option<RustcVersion>,
@@ -2251,6 +2272,7 @@ impl_lint_pass!(Methods => [
22512272
ERR_EXPECT,
22522273
NEEDLESS_OPTION_AS_DEREF,
22532274
IS_DIGIT_ASCII_RADIX,
2275+
NEEDLESS_OPTION_TAKE,
22542276
]);
22552277

22562278
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2623,6 +2645,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
26232645
}
26242646
}
26252647
},
2648+
("take", []) => needless_option_take::check(cx, expr, recv),
26262649
("to_os_string" | "to_owned" | "to_path_buf" | "to_vec", []) => {
26272650
implicit_clone::check(cx, name, expr, recv);
26282651
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::match_def_path;
3+
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::ty::is_type_diagnostic_item;
5+
use rustc_errors::Applicability;
6+
use rustc_hir::Expr;
7+
use rustc_lint::LateContext;
8+
use rustc_span::sym;
9+
10+
use super::NEEDLESS_OPTION_TAKE;
11+
12+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
13+
// Checks if expression type is equal to sym::Option and if the expr is not a syntactic place
14+
if !recv.is_syntactic_place_expr() && is_expr_option(cx, recv) && has_expr_as_ref_path(cx, recv) {
15+
let mut applicability = Applicability::MachineApplicable;
16+
span_lint_and_sugg(
17+
cx,
18+
NEEDLESS_OPTION_TAKE,
19+
expr.span,
20+
"called `Option::take()` on a temporary value",
21+
"try",
22+
format!(
23+
"{}",
24+
snippet_with_applicability(cx, recv.span, "..", &mut applicability)
25+
),
26+
applicability,
27+
);
28+
}
29+
}
30+
31+
fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
32+
let expr_type = cx.typeck_results().expr_ty(expr);
33+
is_type_diagnostic_item(cx, expr_type, sym::Option)
34+
}
35+
36+
fn has_expr_as_ref_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
37+
if let Some(ref_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
38+
return match_def_path(cx, ref_id, &["core", "option", "Option", "as_ref"]);
39+
}
40+
false
41+
}

tests/ui/needless_option_take.fixed

+15
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

+15
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().take();
15+
}

tests/ui/needless_option_take.stderr

+10
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+
+15
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+
}

0 commit comments

Comments
 (0)