Skip to content

Commit 4f14826

Browse files
committed
Lint on opt.as_ref().map(|x| &**x).
1 parent c211cea commit 4f14826

File tree

5 files changed

+55
-18
lines changed

5 files changed

+55
-18
lines changed

clippy_lints/src/loops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,15 +654,15 @@ fn combine_branches(b1: NeverLoopResult, b2: NeverLoopResult) -> NeverLoopResult
654654

655655
fn never_loop_block(block: &Block<'_>, main_loop_id: HirId) -> NeverLoopResult {
656656
let stmts = block.stmts.iter().map(stmt_to_expr);
657-
let expr = once(block.expr.as_ref().map(|p| &**p));
657+
let expr = once(block.expr.as_deref());
658658
let mut iter = stmts.chain(expr).filter_map(|e| e);
659659
never_loop_expr_seq(&mut iter, main_loop_id)
660660
}
661661

662662
fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<&'tcx Expr<'tcx>> {
663663
match stmt.kind {
664664
StmtKind::Semi(ref e, ..) | StmtKind::Expr(ref e, ..) => Some(e),
665-
StmtKind::Local(ref local) => local.init.as_ref().map(|p| &**p),
665+
StmtKind::Local(ref local) => local.init.as_deref(),
666666
_ => None,
667667
}
668668
}

clippy_lints/src/methods/mod.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,6 +3159,8 @@ fn lint_option_as_ref_deref<'a, 'tcx>(
31593159
map_args: &[hir::Expr<'_>],
31603160
is_mut: bool,
31613161
) {
3162+
let same_mutability = |m| (is_mut && m == &hir::Mutability::Mut) || (!is_mut && m == &hir::Mutability::Not);
3163+
31623164
let option_ty = cx.tables.expr_ty(&as_ref_args[0]);
31633165
if !match_type(cx, option_ty, &paths::OPTION) {
31643166
return;
@@ -3181,23 +3183,40 @@ fn lint_option_as_ref_deref<'a, 'tcx>(
31813183
hir::ExprKind::Closure(_, _, body_id, _, _) => {
31823184
let closure_body = cx.tcx.hir().body(body_id);
31833185
let closure_expr = remove_blocks(&closure_body.value);
3184-
if_chain! {
3185-
if let hir::ExprKind::MethodCall(_, _, args) = &closure_expr.kind;
3186-
if args.len() == 1;
3187-
if let hir::ExprKind::Path(qpath) = &args[0].kind;
3188-
if let hir::def::Res::Local(local_id) = cx.tables.qpath_res(qpath, args[0].hir_id);
3189-
if closure_body.params[0].pat.hir_id == local_id;
3190-
let adj = cx.tables.expr_adjustments(&args[0]).iter().map(|x| &x.kind).collect::<Box<[_]>>();
3191-
if let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj;
3192-
then {
3193-
let method_did = cx.tables.type_dependent_def_id(closure_expr.hir_id).unwrap();
3194-
deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
3195-
} else {
3196-
false
3197-
}
3186+
3187+
match &closure_expr.kind {
3188+
hir::ExprKind::MethodCall(_, _, args) => {
3189+
if_chain! {
3190+
if args.len() == 1;
3191+
if let hir::ExprKind::Path(qpath) = &args[0].kind;
3192+
if let hir::def::Res::Local(local_id) = cx.tables.qpath_res(qpath, args[0].hir_id);
3193+
if closure_body.params[0].pat.hir_id == local_id;
3194+
let adj = cx.tables.expr_adjustments(&args[0]).iter().map(|x| &x.kind).collect::<Box<[_]>>();
3195+
if let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj;
3196+
then {
3197+
let method_did = cx.tables.type_dependent_def_id(closure_expr.hir_id).unwrap();
3198+
deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
3199+
} else {
3200+
false
3201+
}
3202+
}
3203+
},
3204+
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, m, ref inner) if same_mutability(m) => {
3205+
if_chain! {
3206+
if let hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner1) = inner.kind;
3207+
if let hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner2) = inner1.kind;
3208+
if let hir::ExprKind::Path(ref qpath) = inner2.kind;
3209+
if let hir::def::Res::Local(local_id) = cx.tables.qpath_res(qpath, inner2.hir_id);
3210+
then {
3211+
closure_body.params[0].pat.hir_id == local_id
3212+
} else {
3213+
false
3214+
}
3215+
}
3216+
},
3217+
_ => false,
31983218
}
31993219
},
3200-
32013220
_ => false,
32023221
};
32033222

tests/ui/option_as_ref_deref.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ fn main() {
3535
let _ = Some(1_usize).as_ref().map(|x| vc[*x].as_str()); // should not be linted
3636

3737
let _: Option<&str> = Some(&String::new()).as_ref().map(|x| x.as_str()); // should not be linted
38+
39+
let _ = opt.as_deref();
40+
let _ = opt.as_deref_mut();
3841
}

tests/ui/option_as_ref_deref.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ fn main() {
3838
let _ = Some(1_usize).as_ref().map(|x| vc[*x].as_str()); // should not be linted
3939

4040
let _: Option<&str> = Some(&String::new()).as_ref().map(|x| x.as_str()); // should not be linted
41+
42+
let _ = opt.as_ref().map(|x| &**x);
43+
let _ = opt.as_mut().map(|x| &mut **x);
4144
}

tests/ui/option_as_ref_deref.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,17 @@ error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases
8888
LL | let _ = opt.clone().as_mut().map(|x| x.deref_mut()).map(|x| x.len());
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.clone().as_deref_mut()`
9090

91-
error: aborting due to 14 previous errors
91+
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref()` instead
92+
--> $DIR/option_as_ref_deref.rs:42:13
93+
|
94+
LL | let _ = opt.as_ref().map(|x| &**x);
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
96+
97+
error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
98+
--> $DIR/option_as_ref_deref.rs:43:13
99+
|
100+
LL | let _ = opt.as_mut().map(|x| &mut **x);
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()`
102+
103+
error: aborting due to 16 previous errors
92104

0 commit comments

Comments
 (0)