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
59 changes: 39 additions & 20 deletions clippy_lints/src/matches/match_as_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,30 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr:
} else {
None
}
&& let output_ty = cx.typeck_results().expr_ty(expr)
&& let input_ty = cx.typeck_results().expr_ty(ex)
&& let Some(input_ty) = option_arg_ty(cx, input_ty)
&& let Some(output_ty) = option_arg_ty(cx, output_ty)
&& let ty::Ref(_, output_ty, output_mutbl) = *output_ty.kind()
{
let method = match arm_ref_mutbl {
Mutability::Not => "as_ref",
Mutability::Mut => "as_mut",
};

let output_ty = cx.typeck_results().expr_ty(expr);
let input_ty = cx.typeck_results().expr_ty(ex);
// ```
// let _: Option<&T> = match opt {
// Some(ref mut t) => Some(t),
// None => None,
// };
// ```
// We need to suggest `t.as_ref()` in order downcast the reference from `&mut` to `&`.
// We may or may not need to cast the type as well, for which we'd need `.map()`, and that could
// theoretically take care of the reference downcasting as well, but we chose to keep these two
// operations separate
let need_as_ref = arm_ref_mutbl == Mutability::Mut && output_mutbl == Mutability::Not;

let cast = if let Some(input_ty) = option_arg_ty(cx, input_ty)
&& let Some(output_ty) = option_arg_ty(cx, output_ty)
&& let ty::Ref(_, output_ty, _) = *output_ty.kind()
&& input_ty != output_ty
{
".map(|x| x as _)"
} else {
""
};
let cast = if input_ty == output_ty { "" } else { ".map(|x| x as _)" };

let mut applicability = Applicability::MachineApplicable;
span_lint_and_then(
Expand All @@ -47,15 +53,28 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr:
expr.span,
format!("manual implementation of `Option::{method}`"),
|diag| {
diag.span_suggestion_verbose(
expr.span,
format!("use `Option::{method}()` directly"),
format!(
"{}.{method}(){cast}",
Sugg::hir_with_applicability(cx, ex, "_", &mut applicability).maybe_paren(),
),
applicability,
);
if need_as_ref {
diag.note("but the type is coerced to a non-mutable reference, and so `as_ref` can used instead");
diag.span_suggestion_verbose(
expr.span,
"use `Option::as_ref()`",
format!(
"{}.as_ref(){cast}",
Sugg::hir_with_applicability(cx, ex, "_", &mut applicability).maybe_paren(),
),
applicability,
);
} else {
diag.span_suggestion_verbose(
expr.span,
format!("use `Option::{method}()` directly"),
format!(
"{}.{method}(){cast}",
Sugg::hir_with_applicability(cx, ex, "_", &mut applicability).maybe_paren(),
),
applicability,
);
}
},
);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/match_as_ref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ fn recv_requiring_parens() {

let _ = (!S).as_ref();
}

fn issue15932() {
let _: Option<&u32> = Some(0).as_ref();

let _: Option<&dyn std::fmt::Debug> = Some(0).as_ref().map(|x| x as _);
}
14 changes: 14 additions & 0 deletions tests/ui/match_as_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,17 @@ fn recv_requiring_parens() {
Some(ref v) => Some(v),
};
}

fn issue15932() {
let _: Option<&u32> = match Some(0) {
//~^ match_as_ref
None => None,
Some(ref mut v) => Some(v),
};

let _: Option<&dyn std::fmt::Debug> = match Some(0) {
//~^ match_as_ref
None => None,
Some(ref mut v) => Some(v),
};
}
46 changes: 45 additions & 1 deletion tests/ui/match_as_ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,49 @@ LL - };
LL + let _ = (!S).as_ref();
|

error: aborting due to 4 previous errors
error: manual implementation of `Option::as_mut`
--> tests/ui/match_as_ref.rs:105:27
|
LL | let _: Option<&u32> = match Some(0) {
| ___________________________^
LL | |
LL | | None => None,
LL | | Some(ref mut v) => Some(v),
LL | | };
| |_____^
|
= note: but the type is coerced to a non-mutable reference, and so `as_ref` can used instead
help: use `Option::as_ref()`
|
LL - let _: Option<&u32> = match Some(0) {
LL -
LL - None => None,
LL - Some(ref mut v) => Some(v),
LL - };
LL + let _: Option<&u32> = Some(0).as_ref();
|

error: manual implementation of `Option::as_mut`
--> tests/ui/match_as_ref.rs:111:43
|
LL | let _: Option<&dyn std::fmt::Debug> = match Some(0) {
| ___________________________________________^
LL | |
LL | | None => None,
LL | | Some(ref mut v) => Some(v),
LL | | };
| |_____^
|
= note: but the type is coerced to a non-mutable reference, and so `as_ref` can used instead
help: use `Option::as_ref()`
|
LL - let _: Option<&dyn std::fmt::Debug> = match Some(0) {
LL -
LL - None => None,
LL - Some(ref mut v) => Some(v),
LL - };
LL + let _: Option<&dyn std::fmt::Debug> = Some(0).as_ref().map(|x| x as _);
|

error: aborting due to 6 previous errors