Skip to content

Commit 93fa016

Browse files
committed
Ignore as_deref_mut in needless_option_as_deref
1 parent d5d830a commit 93fa016

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

clippy_lints/src/needless_option_as_deref.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_span::symbol::sym;
1010

1111
declare_clippy_lint! {
1212
/// ### What it does
13-
/// Checks for no-op uses of Option::{as_deref,as_deref_mut},
14-
/// for example, `Option<&T>::as_deref()` returns the same type.
13+
/// Checks for no-op uses of `Option::as_deref`, for example,
14+
/// `Option<&T>::as_deref()` returns the same type.
1515
///
1616
/// ### Why is this bad?
1717
/// Redundant code and improving readability.
@@ -29,12 +29,10 @@ declare_clippy_lint! {
2929
#[clippy::version = "1.57.0"]
3030
pub NEEDLESS_OPTION_AS_DEREF,
3131
complexity,
32-
"no-op use of `deref` or `deref_mut` method to `Option`."
32+
"no-op use of `as_deref` method to `Option`."
3333
}
3434

35-
declare_lint_pass!(OptionNeedlessDeref=> [
36-
NEEDLESS_OPTION_AS_DEREF,
37-
]);
35+
declare_lint_pass!(OptionNeedlessDeref => [NEEDLESS_OPTION_AS_DEREF]);
3836

3937
impl<'tcx> LateLintPass<'tcx> for OptionNeedlessDeref {
4038
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
@@ -45,19 +43,18 @@ impl<'tcx> LateLintPass<'tcx> for OptionNeedlessDeref {
4543
let outer_ty = typeck.expr_ty(expr);
4644

4745
if_chain! {
48-
if is_type_diagnostic_item(cx,outer_ty,sym::Option);
46+
if is_type_diagnostic_item(cx, outer_ty, sym::Option);
4947
if let ExprKind::MethodCall(path, _, [sub_expr], _) = expr.kind;
50-
let symbol = path.ident.as_str();
51-
if symbol=="as_deref" || symbol=="as_deref_mut";
52-
if TyS::same_type( outer_ty, typeck.expr_ty(sub_expr) );
53-
then{
48+
if path.ident.as_str() == "as_deref";
49+
if TyS::same_type(outer_ty, typeck.expr_ty(sub_expr));
50+
then {
5451
span_lint_and_sugg(
5552
cx,
5653
NEEDLESS_OPTION_AS_DEREF,
5754
expr.span,
5855
"derefed type is same as origin",
5956
"try this",
60-
snippet_opt(cx,sub_expr.span).unwrap(),
57+
snippet_opt(cx, sub_expr.span).unwrap(),
6158
Applicability::MachineApplicable
6259
);
6360
}

tests/ui/needless_option_as_deref.fixed

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@
55
fn main() {
66
// should lint
77
let _: Option<&usize> = Some(&1);
8-
let _: Option<&mut usize> = Some(&mut 1);
8+
9+
// false negative, could lint if the source Option is movable and not used later
10+
let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
911

1012
// should not lint
1113
let _ = Some(Box::new(1)).as_deref();
1214
let _ = Some(Box::new(1)).as_deref_mut();
15+
16+
// #7846
17+
let mut i = 0;
18+
let mut opt_vec = vec![Some(&mut i)];
19+
opt_vec[0].as_deref_mut().unwrap();
20+
21+
// #8047
22+
let mut y = 0;
23+
let mut x = Some(&mut y);
24+
x.as_deref_mut();
25+
println!("{:?}", x);
1326
}

tests/ui/needless_option_as_deref.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@
55
fn main() {
66
// should lint
77
let _: Option<&usize> = Some(&1).as_deref();
8+
9+
// false negative, could lint if the source Option is movable and not used later
810
let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
911

1012
// should not lint
1113
let _ = Some(Box::new(1)).as_deref();
1214
let _ = Some(Box::new(1)).as_deref_mut();
15+
16+
// #7846
17+
let mut i = 0;
18+
let mut opt_vec = vec![Some(&mut i)];
19+
opt_vec[0].as_deref_mut().unwrap();
20+
21+
// #8047
22+
let mut y = 0;
23+
let mut x = Some(&mut y);
24+
x.as_deref_mut();
25+
println!("{:?}", x);
1326
}

tests/ui/needless_option_as_deref.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,5 @@ LL | let _: Option<&usize> = Some(&1).as_deref();
66
|
77
= note: `-D clippy::needless-option-as-deref` implied by `-D warnings`
88

9-
error: derefed type is same as origin
10-
--> $DIR/needless_option_as_deref.rs:8:33
11-
|
12-
LL | let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&mut 1)`
14-
15-
error: aborting due to 2 previous errors
9+
error: aborting due to previous error
1610

0 commit comments

Comments
 (0)