Skip to content

question_mark: avoid incorrect suggestion when ref binding used #14158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2025
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
43 changes: 41 additions & 2 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,47 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
{
let mut applicability = Applicability::MaybeIncorrect;
let init_expr_str = snippet_with_applicability(cx, init_expr.span, "..", &mut applicability);
let receiver_str = snippet_with_applicability(cx, inner_pat.span, "..", &mut applicability);
let sugg = format!("let {receiver_str} = {init_expr_str}?;",);
// Take care when binding is `ref`
let sugg = if let PatKind::Binding(
BindingMode(ByRef::Yes(ref_mutability), binding_mutability),
_hir_id,
ident,
subpattern,
) = inner_pat.kind
{
let (from_method, replace_to) = match ref_mutability {
Mutability::Mut => (".as_mut()", "&mut "),
Mutability::Not => (".as_ref()", "&"),
};

let mutability_str = match binding_mutability {
Mutability::Mut => "mut ",
Mutability::Not => "",
};

// Handle subpattern (@ subpattern)
let maybe_subpattern = match subpattern {
Some(Pat {
kind: PatKind::Binding(BindingMode(ByRef::Yes(_), _), _, subident, None),
..
}) => {
// avoid `&ref`
// note that, because you can't have aliased, mutable references, we don't have to worry about
// the outer and inner mutability being different
format!(" @ {subident}")
},
Some(subpattern) => {
let substr = snippet_with_applicability(cx, subpattern.span, "..", &mut applicability);
format!(" @ {replace_to}{substr}")
},
None => String::new(),
};

format!("let {mutability_str}{ident}{maybe_subpattern} = {init_expr_str}{from_method}?;")
} else {
let receiver_str = snippet_with_applicability(cx, inner_pat.span, "..", &mut applicability);
format!("let {receiver_str} = {init_expr_str}?;")
};
span_lint_and_sugg(
cx,
QUESTION_MARK,
Expand Down
34 changes: 34 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,37 @@ fn issue12412(foo: &Foo, bar: &Bar) -> Option<()> {
//~^^^ question_mark
Some(())
}

struct StructWithOptionString {
opt_x: Option<String>,
}

struct WrapperStructWithString(String);

#[allow(clippy::disallowed_names)]
fn issue_13417(foo: &mut StructWithOptionString) -> Option<String> {
let x = foo.opt_x.as_ref()?;
//~^^^ question_mark
let opt_y = Some(x.clone());
std::mem::replace(&mut foo.opt_x, opt_y)
}

#[allow(clippy::disallowed_names)]
fn issue_13417_mut(foo: &mut StructWithOptionString) -> Option<String> {
let x = foo.opt_x.as_mut()?;
//~^^^ question_mark
let opt_y = Some(x.clone());
std::mem::replace(&mut foo.opt_x, opt_y)
}

#[allow(clippy::disallowed_names)]
#[allow(unused)]
fn issue_13417_weirder(foo: &mut StructWithOptionString, mut bar: Option<WrapperStructWithString>) -> Option<()> {
let x @ y = foo.opt_x.as_ref()?;
//~^^^ question_mark
let x @ &WrapperStructWithString(_) = bar.as_ref()?;
//~^^^ question_mark
let x @ &mut WrapperStructWithString(_) = bar.as_mut()?;
//~^^^ question_mark
Some(())
}
44 changes: 44 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,47 @@ fn issue12412(foo: &Foo, bar: &Bar) -> Option<()> {
//~^^^ question_mark
Some(())
}

struct StructWithOptionString {
opt_x: Option<String>,
}

struct WrapperStructWithString(String);

#[allow(clippy::disallowed_names)]
fn issue_13417(foo: &mut StructWithOptionString) -> Option<String> {
let Some(ref x) = foo.opt_x else {
return None;
};
//~^^^ question_mark
let opt_y = Some(x.clone());
std::mem::replace(&mut foo.opt_x, opt_y)
}

#[allow(clippy::disallowed_names)]
fn issue_13417_mut(foo: &mut StructWithOptionString) -> Option<String> {
let Some(ref mut x) = foo.opt_x else {
return None;
};
//~^^^ question_mark
let opt_y = Some(x.clone());
std::mem::replace(&mut foo.opt_x, opt_y)
}

#[allow(clippy::disallowed_names)]
#[allow(unused)]
fn issue_13417_weirder(foo: &mut StructWithOptionString, mut bar: Option<WrapperStructWithString>) -> Option<()> {
let Some(ref x @ ref y) = foo.opt_x else {
return None;
};
//~^^^ question_mark
let Some(ref x @ WrapperStructWithString(_)) = bar else {
return None;
};
//~^^^ question_mark
let Some(ref mut x @ WrapperStructWithString(_)) = bar else {
return None;
};
//~^^^ question_mark
Some(())
}
42 changes: 41 additions & 1 deletion tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,45 @@ LL | | return None;
LL | | };
| |______^ help: replace it with: `let v = bar.foo.owned.clone()?;`

error: aborting due to 22 previous errors
error: this `let...else` may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:464:5
|
LL | / let Some(ref x) = foo.opt_x else {
LL | | return None;
LL | | };
| |______^ help: replace it with: `let x = foo.opt_x.as_ref()?;`

error: this `let...else` may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:474:5
|
LL | / let Some(ref mut x) = foo.opt_x else {
LL | | return None;
LL | | };
| |______^ help: replace it with: `let x = foo.opt_x.as_mut()?;`

error: this `let...else` may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:485:5
|
LL | / let Some(ref x @ ref y) = foo.opt_x else {
LL | | return None;
LL | | };
| |______^ help: replace it with: `let x @ y = foo.opt_x.as_ref()?;`

error: this `let...else` may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:489:5
|
LL | / let Some(ref x @ WrapperStructWithString(_)) = bar else {
LL | | return None;
LL | | };
| |______^ help: replace it with: `let x @ &WrapperStructWithString(_) = bar.as_ref()?;`

error: this `let...else` may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:493:5
|
LL | / let Some(ref mut x @ WrapperStructWithString(_)) = bar else {
LL | | return None;
LL | | };
| |______^ help: replace it with: `let x @ &mut WrapperStructWithString(_) = bar.as_mut()?;`

error: aborting due to 27 previous errors