Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clippy_lints/src/manual_option_as_slice.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::msrvs::Msrv;
use clippy_utils::{is_none_arm, msrvs, peel_hir_expr_refs, sym};
use clippy_utils::{is_none_pattern, msrvs, peel_hir_expr_refs, sym};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Arm, Expr, ExprKind, LangItem, Pat, PatKind, QPath, is_range_literal};
Expand Down Expand Up @@ -60,8 +60,8 @@ impl LateLintPass<'_> for ManualOptionAsSlice {
}
match expr.kind {
ExprKind::Match(scrutinee, [arm1, arm2], _) => {
if is_none_arm(cx, arm2) && check_arms(cx, arm2, arm1)
|| is_none_arm(cx, arm1) && check_arms(cx, arm1, arm2)
if is_none_pattern(cx, arm2.pat) && check_arms(cx, arm2, arm1)
|| is_none_pattern(cx, arm1.pat) && check_arms(cx, arm1, arm2)
{
check_as_ref(cx, scrutinee, span, self.msrv);
}
Expand Down
16 changes: 10 additions & 6 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,17 @@ pub fn is_wild(pat: &Pat<'_>) -> bool {
matches!(pat.kind, PatKind::Wild)
}

// Checks if arm has the form `None => None`
pub fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
matches!(
arm.pat.kind,
/// Checks if the `pat` is `None`.
pub fn is_none_pattern(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
matches!(pat.kind,
PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), .. })
if is_res_lang_ctor(cx, cx.qpath_res(qpath, arm.pat.hir_id), OptionNone)
)
if is_res_lang_ctor(cx, cx.qpath_res(qpath, pat.hir_id), OptionNone))
}

/// Checks if `arm` has the form `None => None`.
pub fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
is_none_pattern(cx, arm.pat)
&& matches!(peel_blocks(arm.body).kind, ExprKind::Path(qpath) if is_res_lang_ctor(cx, cx.qpath_res(&qpath, arm.body.hir_id), OptionNone))
}

/// Checks if the given `QPath` belongs to a type alias.
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/match_as_ref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,33 @@ fn main() {
None => None,
};
}

mod issue15691 {
use std::ops::{Deref, DerefMut};

struct A(B);
struct B;

impl Deref for A {
type Target = B;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for A {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

fn func() {
let mut a = Some(A(B));
let mut b = Some(B);
// Do not lint, we don't have `None => None`
let _ = match b {
Some(ref mut x) => Some(x),
None => a.as_deref_mut(),
};
}
}
30 changes: 30 additions & 0 deletions tests/ui/match_as_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,33 @@ fn main() {
None => None,
};
}

mod issue15691 {
use std::ops::{Deref, DerefMut};

struct A(B);
struct B;

impl Deref for A {
type Target = B;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for A {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

fn func() {
let mut a = Some(A(B));
let mut b = Some(B);
// Do not lint, we don't have `None => None`
let _ = match b {
Some(ref mut x) => Some(x),
None => a.as_deref_mut(),
};
}
}