Skip to content
Open
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
14 changes: 10 additions & 4 deletions clippy_lints/src/manual_option_as_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use clippy_utils::res::{MaybeDef as _, MaybeQPath as _, MaybeResPath as _};
use clippy_utils::source::snippet_with_context;
use clippy_utils::{as_some_pattern, is_none_pattern, msrvs, peel_hir_expr_refs, sym};
use rustc_errors::Applicability;
use rustc_hir::{Arm, Expr, ExprKind, Pat, PatKind, QPath, is_range_literal};
use rustc_lint::{LateContext, LateLintPass, impl_lint_pass};
use rustc_hir::{Arm, ByRef, Expr, ExprKind, Pat, PatKind, QPath, is_range_literal};
use rustc_lint::{LateContext, LateLintPass, LintContext as _, impl_lint_pass};
use rustc_span::{Span, Symbol};

declare_clippy_lint! {
Expand Down Expand Up @@ -66,9 +66,9 @@ impl LateLintPass<'_> for ManualOptionAsSlice {
},
ExprKind::If(cond, then, Some(other)) => {
if let ExprKind::Let(let_expr) = cond.kind
&& is_empty_slice(cx, other.peel_blocks())
&& let Some(binding) = extract_ident_from_some_pat(cx, let_expr.pat)
&& check_some_body(cx, binding, then)
&& is_empty_slice(cx, other.peel_blocks())
{
check_as_ref(cx, let_expr.init, span, self.msrv);
}
Expand Down Expand Up @@ -142,7 +142,13 @@ fn check_as_ref(cx: &LateContext<'_>, expr: &Expr<'_>, span: Span, msrv: Msrv) {

fn extract_ident_from_some_pat(cx: &LateContext<'_>, pat: &Pat<'_>) -> Option<Symbol> {
if let Some([binding]) = as_some_pattern(cx, pat)
&& let PatKind::Binding(_mode, _hir_id, ident, _inner_pat) = binding.kind
&& let PatKind::Binding(_, hir_id, ident, _inner_pat) = binding.kind
&& matches!(
cx.typeck_results()
.extract_binding_mode(cx.sess(), hir_id, binding.span)
.0,
ByRef::No
)
{
Some(ident.name)
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/manual_option_as_slice.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ fn check(x: Option<u32>) {
_ = x.as_ref().map_or(&[42][..], std::slice::from_ref);
_ = x.as_ref().map_or_else(|| &[42][..1], std::slice::from_ref);
_ = x.as_ref().map(|f| std::slice::from_ref(f)).unwrap_or_default();

let _: &[&u32] = match x.as_ref() {
Some(ref f) => std::slice::from_ref(f),
None => &[],
};
let _: &[&u32] = match x.as_ref() {
Some(ref mut f) => std::slice::from_ref(f),
None => &[],
};
}

#[clippy::msrv = "1.74"]
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/manual_option_as_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ fn check(x: Option<u32>) {
_ = x.as_ref().map_or(&[42][..], std::slice::from_ref);
_ = x.as_ref().map_or_else(|| &[42][..1], std::slice::from_ref);
_ = x.as_ref().map(|f| std::slice::from_ref(f)).unwrap_or_default();

let _: &[&u32] = match x.as_ref() {
Some(ref f) => std::slice::from_ref(f),
None => &[],
};
let _: &[&u32] = match x.as_ref() {
Some(ref mut f) => std::slice::from_ref(f),
None => &[],
};
Comment thread
cuishuang marked this conversation as resolved.
}

#[clippy::msrv = "1.74"]
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/manual_option_as_slice_2021.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ edition: 2021
//@ check-pass

#![warn(clippy::manual_option_as_slice)]

fn check(x: Option<u32>) {
// Edition 2024 drops the `as_ref` temporary before the result of the `if let` is consumed.
_ = if let Some(ref f) = x.as_ref() {
std::slice::from_ref(f)
} else {
&[]
};
_ = if let Some(ref mut f) = x.as_ref() {
std::slice::from_ref(f)
} else {
&[]
};
}

fn main() {}