Skip to content

Commit 182b7c3

Browse files
committed
Fix as_deref_mut false positives in needless_option_as_deref
Also moves the lint to the methods directory
1 parent 409a936 commit 182b7c3

10 files changed

+136
-75
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
176176
LintId::of(methods::MAP_COLLECT_RESULT_UNIT),
177177
LintId::of(methods::MAP_FLATTEN),
178178
LintId::of(methods::MAP_IDENTITY),
179+
LintId::of(methods::NEEDLESS_OPTION_AS_DEREF),
179180
LintId::of(methods::NEEDLESS_SPLITN),
180181
LintId::of(methods::NEW_RET_NO_SELF),
181182
LintId::of(methods::OK_EXPECT),
@@ -225,7 +226,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
225226
LintId::of(needless_bool::NEEDLESS_BOOL),
226227
LintId::of(needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE),
227228
LintId::of(needless_late_init::NEEDLESS_LATE_INIT),
228-
LintId::of(needless_option_as_deref::NEEDLESS_OPTION_AS_DEREF),
229229
LintId::of(needless_question_mark::NEEDLESS_QUESTION_MARK),
230230
LintId::of(needless_update::NEEDLESS_UPDATE),
231231
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),

clippy_lints/src/lib.register_complexity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
4444
LintId::of(methods::MANUAL_SPLIT_ONCE),
4545
LintId::of(methods::MAP_FLATTEN),
4646
LintId::of(methods::MAP_IDENTITY),
47+
LintId::of(methods::NEEDLESS_OPTION_AS_DEREF),
4748
LintId::of(methods::NEEDLESS_SPLITN),
4849
LintId::of(methods::OPTION_AS_REF_DEREF),
4950
LintId::of(methods::OPTION_FILTER_MAP),
@@ -60,7 +61,6 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6061
LintId::of(needless_bool::BOOL_COMPARISON),
6162
LintId::of(needless_bool::NEEDLESS_BOOL),
6263
LintId::of(needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE),
63-
LintId::of(needless_option_as_deref::NEEDLESS_OPTION_AS_DEREF),
6464
LintId::of(needless_question_mark::NEEDLESS_QUESTION_MARK),
6565
LintId::of(needless_update::NEEDLESS_UPDATE),
6666
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ store.register_lints(&[
315315
methods::MAP_FLATTEN,
316316
methods::MAP_IDENTITY,
317317
methods::MAP_UNWRAP_OR,
318+
methods::NEEDLESS_OPTION_AS_DEREF,
318319
methods::NEEDLESS_SPLITN,
319320
methods::NEW_RET_NO_SELF,
320321
methods::OK_EXPECT,
@@ -386,7 +387,6 @@ store.register_lints(&[
386387
needless_continue::NEEDLESS_CONTINUE,
387388
needless_for_each::NEEDLESS_FOR_EACH,
388389
needless_late_init::NEEDLESS_LATE_INIT,
389-
needless_option_as_deref::NEEDLESS_OPTION_AS_DEREF,
390390
needless_pass_by_value::NEEDLESS_PASS_BY_VALUE,
391391
needless_question_mark::NEEDLESS_QUESTION_MARK,
392392
needless_update::NEEDLESS_UPDATE,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ mod needless_borrowed_ref;
308308
mod needless_continue;
309309
mod needless_for_each;
310310
mod needless_late_init;
311-
mod needless_option_as_deref;
312311
mod needless_pass_by_value;
313312
mod needless_question_mark;
314313
mod needless_update;
@@ -536,7 +535,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
536535
store.register_late_pass(|| Box::new(ptr::Ptr));
537536
store.register_late_pass(|| Box::new(ptr_eq::PtrEq));
538537
store.register_late_pass(|| Box::new(needless_bool::NeedlessBool));
539-
store.register_late_pass(|| Box::new(needless_option_as_deref::OptionNeedlessDeref));
540538
store.register_late_pass(|| Box::new(needless_bool::BoolComparison));
541539
store.register_late_pass(|| Box::new(needless_for_each::NeedlessForEach));
542540
store.register_late_pass(|| Box::new(misc::MiscLints));

clippy_lints/src/methods/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod map_collect_result_unit;
4141
mod map_flatten;
4242
mod map_identity;
4343
mod map_unwrap_or;
44+
mod needless_option_as_deref;
4445
mod ok_expect;
4546
mod option_as_ref_deref;
4647
mod option_map_or_none;
@@ -2106,6 +2107,30 @@ declare_clippy_lint! {
21062107
"using `.collect::<Vec<String>>().join(\"\")` on an iterator"
21072108
}
21082109

2110+
declare_clippy_lint! {
2111+
/// ### What it does
2112+
/// Checks for no-op uses of `Option::{as_deref, as_deref_mut}`,
2113+
/// for example, `Option<&T>::as_deref()` returns the same type.
2114+
///
2115+
/// ### Why is this bad?
2116+
/// Redundant code and improving readability.
2117+
///
2118+
/// ### Example
2119+
/// ```rust
2120+
/// let a = Some(&1);
2121+
/// let b = a.as_deref(); // goes from Option<&i32> to Option<&i32>
2122+
/// ```
2123+
/// Could be written as:
2124+
/// ```rust
2125+
/// let a = Some(&1);
2126+
/// let b = a;
2127+
/// ```
2128+
#[clippy::version = "1.57.0"]
2129+
pub NEEDLESS_OPTION_AS_DEREF,
2130+
complexity,
2131+
"no-op use of `deref` or `deref_mut` method to `Option`."
2132+
}
2133+
21092134
pub struct Methods {
21102135
avoid_breaking_exported_api: bool,
21112136
msrv: Option<RustcVersion>,
@@ -2193,6 +2218,7 @@ impl_lint_pass!(Methods => [
21932218
UNNECESSARY_TO_OWNED,
21942219
UNNECESSARY_JOIN,
21952220
ERR_EXPECT,
2221+
NEEDLESS_OPTION_AS_DEREF,
21962222
]);
21972223

21982224
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2425,6 +2451,9 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
24252451
unnecessary_lazy_eval::check(cx, expr, recv, arg, "and");
24262452
}
24272453
},
2454+
("as_deref" | "as_deref_mut", []) => {
2455+
needless_option_as_deref::check(cx, expr, recv, name);
2456+
},
24282457
("as_mut", []) => useless_asref::check(cx, expr, "as_mut", recv),
24292458
("as_ref", []) => useless_asref::check(cx, expr, "as_ref", recv),
24302459
("assume_init", []) => uninit_assumed_init::check(cx, expr, recv),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::path_res;
3+
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::ty::is_type_diagnostic_item;
5+
use clippy_utils::usage::local_used_after_expr;
6+
use rustc_errors::Applicability;
7+
use rustc_hir::def::Res;
8+
use rustc_hir::Expr;
9+
use rustc_lint::LateContext;
10+
use rustc_span::sym;
11+
12+
use super::NEEDLESS_OPTION_AS_DEREF;
13+
14+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, name: &str) {
15+
let typeck = cx.typeck_results();
16+
let outer_ty = typeck.expr_ty(expr);
17+
18+
if is_type_diagnostic_item(cx, outer_ty, sym::Option) && outer_ty == typeck.expr_ty(recv) {
19+
if name == "as_deref_mut" && recv.is_syntactic_place_expr() {
20+
let Res::Local(binding_id) = path_res(cx, recv) else { return };
21+
22+
if local_used_after_expr(cx, binding_id, recv) {
23+
return;
24+
}
25+
}
26+
27+
span_lint_and_sugg(
28+
cx,
29+
NEEDLESS_OPTION_AS_DEREF,
30+
expr.span,
31+
"derefed type is same as origin",
32+
"try this",
33+
snippet_opt(cx, recv.span).unwrap(),
34+
Applicability::MachineApplicable,
35+
);
36+
}
37+
}

clippy_lints/src/needless_option_as_deref.rs

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
// run-rustfix
22

3-
#[warn(clippy::needless_option_as_deref)]
3+
#![allow(unused)]
4+
#![warn(clippy::needless_option_as_deref)]
45

56
fn main() {
67
// should lint
78
let _: Option<&usize> = Some(&1);
89
let _: Option<&mut usize> = Some(&mut 1);
910

11+
let mut y = 0;
12+
let mut x = Some(&mut y);
13+
let _ = x;
14+
1015
// should not lint
1116
let _ = Some(Box::new(1)).as_deref();
1217
let _ = Some(Box::new(1)).as_deref_mut();
18+
19+
// #7846
20+
let mut i = 0;
21+
let mut opt_vec = vec![Some(&mut i)];
22+
opt_vec[0].as_deref_mut().unwrap();
23+
24+
let mut i = 0;
25+
let x = &mut Some(&mut i);
26+
(*x).as_deref_mut();
27+
28+
// #8047
29+
let mut y = 0;
30+
let mut x = Some(&mut y);
31+
x.as_deref_mut();
32+
dbg!(x);
33+
}
34+
35+
struct S<'a> {
36+
opt: Option<&'a mut usize>,
37+
}
38+
39+
fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> {
40+
s.opt.as_deref_mut()
1341
}

tests/ui/needless_option_as_deref.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
// run-rustfix
22

3-
#[warn(clippy::needless_option_as_deref)]
3+
#![allow(unused)]
4+
#![warn(clippy::needless_option_as_deref)]
45

56
fn main() {
67
// should lint
78
let _: Option<&usize> = Some(&1).as_deref();
89
let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
910

11+
let mut y = 0;
12+
let mut x = Some(&mut y);
13+
let _ = x.as_deref_mut();
14+
1015
// should not lint
1116
let _ = Some(Box::new(1)).as_deref();
1217
let _ = Some(Box::new(1)).as_deref_mut();
18+
19+
// #7846
20+
let mut i = 0;
21+
let mut opt_vec = vec![Some(&mut i)];
22+
opt_vec[0].as_deref_mut().unwrap();
23+
24+
let mut i = 0;
25+
let x = &mut Some(&mut i);
26+
(*x).as_deref_mut();
27+
28+
// #8047
29+
let mut y = 0;
30+
let mut x = Some(&mut y);
31+
x.as_deref_mut();
32+
dbg!(x);
33+
}
34+
35+
struct S<'a> {
36+
opt: Option<&'a mut usize>,
37+
}
38+
39+
fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> {
40+
s.opt.as_deref_mut()
1341
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
error: derefed type is same as origin
2-
--> $DIR/needless_option_as_deref.rs:7:29
2+
--> $DIR/needless_option_as_deref.rs:8:29
33
|
44
LL | let _: Option<&usize> = Some(&1).as_deref();
55
| ^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&1)`
66
|
77
= note: `-D clippy::needless-option-as-deref` implied by `-D warnings`
88

99
error: derefed type is same as origin
10-
--> $DIR/needless_option_as_deref.rs:8:33
10+
--> $DIR/needless_option_as_deref.rs:9:33
1111
|
1212
LL | let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&mut 1)`
1414

15-
error: aborting due to 2 previous errors
15+
error: derefed type is same as origin
16+
--> $DIR/needless_option_as_deref.rs:13:13
17+
|
18+
LL | let _ = x.as_deref_mut();
19+
| ^^^^^^^^^^^^^^^^ help: try this: `x`
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)