mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
Suggest if let PAT == expr on let PAT = expr else with non-structural type const
```
error: constant of non-structural type `partial_eq::S` in a pattern
--> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:22:18
|
LL | struct S;
| -------- `partial_eq::S` must be annotated with `#[derive(PartialEq)]` to be usable in patterns
...
LL | const C: S = S;
| ---------- constant defined here
...
LL | let Some(C) = Some(S) else { return; };
| ^ constant of non-structural type
|
note: the `PartialEq` trait must be derived, manual `impl`s are not sufficient; see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
--> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:5:5
|
LL | impl PartialEq<S> for S {
| ^^^^^^^^^^^^^^^^^^^^^^^
help: check for equality instead of pattern matching
|
LL - let Some(C) = Some(S) else { return; };
LL + if Some(C) == Some(S) { return; };
|
```
This commit is contained in:
@@ -1144,6 +1144,24 @@ pub(crate) enum SuggestEq<'tcx> {
|
||||
ty: Ty<'tcx>,
|
||||
manual_partialeq_impl: bool,
|
||||
},
|
||||
#[multipart_suggestion(
|
||||
"{$manual_partialeq_impl ->
|
||||
[false] if `{$ty}` manually implemented `PartialEq`, you could check
|
||||
*[true] check
|
||||
} for equality instead of pattern matching",
|
||||
applicability = "maybe-incorrect",
|
||||
style = "verbose"
|
||||
)]
|
||||
ReplaceLetElseWithIf {
|
||||
#[suggestion_part(code = "if ")]
|
||||
if_span: Span,
|
||||
#[suggestion_part(code = " == ")]
|
||||
eq: Span,
|
||||
#[suggestion_part(code = " ")]
|
||||
else_span: Span,
|
||||
ty: Ty<'tcx>,
|
||||
manual_partialeq_impl: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
|
||||
@@ -283,6 +283,21 @@ impl<'tcx> ConstToPat<'tcx> {
|
||||
None
|
||||
}
|
||||
}
|
||||
hir::Node::LetStmt(let_stmt)
|
||||
if let Some(init) = let_stmt.init
|
||||
&& let Some(els) = let_stmt.els
|
||||
&& init.span.ctxt().is_root()
|
||||
&& els.span.ctxt().is_root() =>
|
||||
{
|
||||
// `let PAT = expr else {` -> `if PAT == expr {`.
|
||||
Some(SuggestEq::ReplaceLetElseWithIf {
|
||||
if_span: let_stmt.span.until(let_stmt.pat.span),
|
||||
eq: let_stmt.pat.span.between(init.span),
|
||||
else_span: init.span.between(els.span),
|
||||
ty,
|
||||
manual_partialeq_impl,
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -26,7 +26,7 @@ mod partial_eq {
|
||||
}
|
||||
if let V = vec![] {} //~ ERROR: constant of non-structural type
|
||||
let V = vec![] else { return; }; //~ ERROR: constant of non-structural type
|
||||
// ^ maybe suggest if V != vec![] { return; } ?
|
||||
let V = Vec::new() else { return; }; //~ ERROR: constant of non-structural type
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,11 @@ note: the `PartialEq` trait must be derived, manual `impl`s are not sufficient;
|
||||
|
|
||||
LL | impl PartialEq<S> for S {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: check for equality instead of pattern matching
|
||||
|
|
||||
LL - let Some(C) = Some(S) else { return; };
|
||||
LL + if Some(C) == Some(S) { return; };
|
||||
|
|
||||
|
||||
error: constant of non-structural type `Vec<()>` in a pattern
|
||||
--> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:24:13
|
||||
@@ -158,6 +163,26 @@ LL | let V = vec![] else { return; };
|
||||
|
|
||||
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
|
||||
|
||||
error: constant of non-structural type `Vec<()>` in a pattern
|
||||
--> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:29:13
|
||||
|
|
||||
LL | const V: Vec<()> = vec![];
|
||||
| ---------------- constant defined here
|
||||
...
|
||||
LL | let V = Vec::new() else { return; };
|
||||
| ^ constant of non-structural type
|
||||
|
|
||||
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
|
||||
|
|
||||
= note: `Vec<()>` is not usable in patterns
|
||||
|
|
||||
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
|
||||
help: check for equality instead of pattern matching
|
||||
|
|
||||
LL - let V = Vec::new() else { return; };
|
||||
LL + if V == Vec::new() { return; };
|
||||
|
|
||||
|
||||
error: constant of non-structural type `not_partial_eq::S` in a pattern
|
||||
--> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:40:18
|
||||
|
|
||||
@@ -247,6 +272,11 @@ LL | let Some(C) = Some(S) else { return; };
|
||||
| ^ constant of non-structural type
|
||||
|
|
||||
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
|
||||
help: check for equality instead of pattern matching
|
||||
|
|
||||
LL - let Some(C) = Some(S) else { return; };
|
||||
LL + if Some(C) == Some(S) { return; };
|
||||
|
|
||||
|
||||
error: aborting due to 13 previous errors
|
||||
error: aborting due to 14 previous errors
|
||||
|
||||
|
||||
Reference in New Issue
Block a user