Skip to content

Commit 11b28d4

Browse files
Implement mut ref/mut ref mut
1 parent 4b10cb2 commit 11b28d4

14 files changed

+34
-29
lines changed

clippy_lints/src/functions/misnamed_getters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub fn check_fn(cx: &LateContext<'_>, kind: FnKind<'_>, decl: &FnDecl<'_>, body:
2424
let name = ident.name.as_str();
2525

2626
let name = match decl.implicit_self {
27-
ImplicitSelfKind::MutRef => {
27+
ImplicitSelfKind::RefMut => {
2828
let Some(name) = name.strip_suffix("_mut") else {
2929
return;
3030
};
3131
name
3232
},
33-
ImplicitSelfKind::Imm | ImplicitSelfKind::Mut | ImplicitSelfKind::ImmRef => name,
33+
ImplicitSelfKind::Imm | ImplicitSelfKind::Mut | ImplicitSelfKind::RefImm => name,
3434
ImplicitSelfKind::None => return,
3535
};
3636

clippy_lints/src/index_refutable_slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxIndexMap<hir
9797
value_hir_id,
9898
ident,
9999
sub_pat,
100-
) = pat.kind
100+
) = pat.kind && by_ref != hir::ByRef::Yes(hir::Mutability::Mut)
101101
{
102102
// This block catches bindings with sub patterns. It would be hard to build a correct suggestion
103103
// for them and it's likely that the user knows what they are doing in such a case.
@@ -115,7 +115,7 @@ fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxIndexMap<hir
115115
if let ty::Slice(inner_ty) | ty::Array(inner_ty, _) = bound_ty.peel_refs().kind() {
116116
// The values need to use the `ref` keyword if they can't be copied.
117117
// This will need to be adjusted if the lint want to support mutable access in the future
118-
let src_is_ref = bound_ty.is_ref() && by_ref != hir::ByRef::Yes;
118+
let src_is_ref = bound_ty.is_ref() && by_ref == hir::ByRef::No;
119119
let needs_ref = !(src_is_ref || is_copy(cx, *inner_ty));
120120

121121
let slice_info = slices

clippy_lints/src/iter_without_into_iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ impl {self_ty_without_ref} {{
216216
fn check_impl_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ImplItem<'_>) {
217217
let item_did = item.owner_id.to_def_id();
218218
let (borrow_prefix, expected_implicit_self) = match item.ident.name {
219-
sym::iter => ("&", ImplicitSelfKind::ImmRef),
220-
sym::iter_mut => ("&mut ", ImplicitSelfKind::MutRef),
219+
sym::iter => ("&", ImplicitSelfKind::RefImm),
220+
sym::iter_mut => ("&mut ", ImplicitSelfKind::RefMut),
221221
_ => return,
222222
};
223223

clippy_lints/src/len_zero.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ impl LenOutput {
384384

385385
fn expected_sig(self, self_kind: ImplicitSelfKind) -> String {
386386
let self_ref = match self_kind {
387-
ImplicitSelfKind::ImmRef => "&",
388-
ImplicitSelfKind::MutRef => "&mut ",
387+
ImplicitSelfKind::RefImm => "&",
388+
ImplicitSelfKind::RefMut => "&mut ",
389389
_ => "",
390390
};
391391
match self {
@@ -411,8 +411,8 @@ fn check_is_empty_sig<'tcx>(
411411
[arg, res] if len_output.matches_is_empty_output(cx, *res) => {
412412
matches!(
413413
(arg.kind(), self_kind),
414-
(ty::Ref(_, _, Mutability::Not), ImplicitSelfKind::ImmRef)
415-
| (ty::Ref(_, _, Mutability::Mut), ImplicitSelfKind::MutRef)
414+
(ty::Ref(_, _, Mutability::Not), ImplicitSelfKind::RefImm)
415+
| (ty::Ref(_, _, Mutability::Mut), ImplicitSelfKind::RefMut)
416416
) || (!arg.is_ref() && matches!(self_kind, ImplicitSelfKind::Imm | ImplicitSelfKind::Mut))
417417
},
418418
_ => false,

clippy_lints/src/matches/infallible_destructuring_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::{path_to_local_id, peel_blocks, strip_pat_refs};
44
use rustc_errors::Applicability;
5-
use rustc_hir::{ByRef, ExprKind, LetStmt, MatchSource, PatKind, QPath};
5+
use rustc_hir::{ExprKind, LetStmt, MatchSource, PatKind, QPath};
66
use rustc_lint::LateContext;
77

88
use super::INFALLIBLE_DESTRUCTURING_MATCH;
@@ -30,7 +30,7 @@ pub(crate) fn check(cx: &LateContext<'_>, local: &LetStmt<'_>) -> bool {
3030
format!(
3131
"let {}({}{}) = {};",
3232
snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
33-
if binding.0 == ByRef::Yes { "ref " } else { "" },
33+
binding.prefix_str(),
3434
snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
3535
snippet_with_applicability(cx, target.span, "..", &mut applicability),
3636
),

clippy_lints/src/matches/match_as_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
6767
fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option<Mutability> {
6868
if let PatKind::TupleStruct(ref qpath, [first_pat, ..], _) = arm.pat.kind
6969
&& is_res_lang_ctor(cx, cx.qpath_res(qpath, arm.pat.hir_id), LangItem::OptionSome)
70-
&& let PatKind::Binding(BindingAnnotation(ByRef::Yes, mutabl), .., ident, _) = first_pat.kind
70+
&& let PatKind::Binding(BindingAnnotation(ByRef::Yes(mutabl), _), .., ident, _) = first_pat.kind
7171
&& let ExprKind::Call(e, [arg]) = peel_blocks(arm.body).kind
7272
&& is_res_lang_ctor(cx, path_res(cx, e), LangItem::OptionSome)
7373
&& let ExprKind::Path(QPath::Resolved(_, path2)) = arg.kind

clippy_lints/src/matches/needless_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
178178
},
179179
)),
180180
) => {
181-
return !matches!(annot, BindingAnnotation(ByRef::Yes, _)) && pat_ident.name == first_seg.ident.name;
181+
return !matches!(annot, BindingAnnotation(ByRef::Yes(_), _)) && pat_ident.name == first_seg.ident.name;
182182
},
183183
// Example: `Custom::TypeA => Custom::TypeB`, or `None => None`
184184
(PatKind::Path(QPath::Resolved(_, p_path)), ExprKind::Path(QPath::Resolved(_, e_path))) => {

clippy_lints/src/matches/redundant_guards.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ fn get_pat_binding<'tcx>(
182182
if let PatKind::Binding(bind_annot, hir_id, ident, _) = pat.kind
183183
&& hir_id == local
184184
{
185-
if matches!(bind_annot.0, rustc_ast::ByRef::Yes) {
185+
if matches!(bind_annot.0, rustc_ast::ByRef::Yes(_)) {
186186
let _ = byref_ident.insert(ident);
187187
}
188188
// the second call of `replace()` returns a `Some(span)`, meaning a multi-binding pattern

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(super) fn check(
6969
_ => false,
7070
},
7171
// local binding capturing a reference
72-
Node::LetStmt(l) if matches!(l.pat.kind, PatKind::Binding(BindingAnnotation(ByRef::Yes, _), ..)) => {
72+
Node::LetStmt(l) if matches!(l.pat.kind, PatKind::Binding(BindingAnnotation(ByRef::Yes(_), _), ..)) => {
7373
return;
7474
},
7575
_ => false,

clippy_lints/src/methods/iter_kv_map.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,15 @@ pub(super) fn check<'tcx>(
6060
applicability,
6161
);
6262
} else {
63-
let ref_annotation = if annotation.0 == ByRef::Yes { "ref " } else { "" };
64-
let mut_annotation = if annotation.1 == Mutability::Mut { "mut " } else { "" };
6563
span_lint_and_sugg(
6664
cx,
6765
ITER_KV_MAP,
6866
expr.span,
6967
&format!("iterating on a map's {replacement_kind}s"),
7068
"try",
7169
format!(
72-
"{recv_snippet}.{into_prefix}{replacement_kind}s().map(|{ref_annotation}{mut_annotation}{bound_ident}| {})",
70+
"{recv_snippet}.{into_prefix}{replacement_kind}s().map(|{}{bound_ident}| {})",
71+
annotation.prefix_str(),
7372
snippet_with_applicability(cx, body_expr.span, "/* body */", &mut applicability)
7473
),
7574
applicability,

0 commit comments

Comments
 (0)