Suggest swapping the order of ref
and box
This commit is contained in:
parent
b29a1e00f8
commit
f4c2bdeec9
@ -721,6 +721,9 @@ parse_sugg_wrap_pattern_in_parens = wrap the pattern in parentheses
|
|||||||
parse_switch_mut_let_order =
|
parse_switch_mut_let_order =
|
||||||
switch the order of `mut` and `let`
|
switch the order of `mut` and `let`
|
||||||
|
|
||||||
|
parse_switch_ref_box_order = switch the order of `ref` and `box`
|
||||||
|
.suggestion = swap them
|
||||||
|
|
||||||
parse_ternary_operator = Rust has no ternary operator
|
parse_ternary_operator = Rust has no ternary operator
|
||||||
.help = use an `if-else` expression instead
|
.help = use an `if-else` expression instead
|
||||||
|
|
||||||
|
@ -137,6 +137,14 @@ pub(crate) enum InvalidVariableDeclarationSub {
|
|||||||
UseLetNotVar(#[primary_span] Span),
|
UseLetNotVar(#[primary_span] Span),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(parse_switch_ref_box_order)]
|
||||||
|
pub(crate) struct SwitchRefBoxOrder {
|
||||||
|
#[primary_span]
|
||||||
|
#[suggestion(applicability = "machine-applicable", code = "box ref")]
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(parse_invalid_comparison_operator)]
|
#[diag(parse_invalid_comparison_operator)]
|
||||||
pub(crate) struct InvalidComparisonOperator {
|
pub(crate) struct InvalidComparisonOperator {
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
ExpectedCommaAfterPatternField, GenericArgsInPatRequireTurbofishSyntax,
|
ExpectedCommaAfterPatternField, GenericArgsInPatRequireTurbofishSyntax,
|
||||||
InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, InvalidMutInPattern,
|
InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, InvalidMutInPattern,
|
||||||
PatternOnWrongSideOfAt, RefMutOrderIncorrect, RemoveLet, RepeatedMutInPattern,
|
PatternOnWrongSideOfAt, RefMutOrderIncorrect, RemoveLet, RepeatedMutInPattern,
|
||||||
TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed,
|
SwitchRefBoxOrder, TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg,
|
||||||
UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam,
|
TrailingVertNotAllowed, UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam,
|
||||||
UnexpectedVertVertInPattern,
|
UnexpectedVertVertInPattern,
|
||||||
};
|
};
|
||||||
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
|
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
|
||||||
@ -374,6 +374,12 @@ fn parse_pat_with_range_pat(
|
|||||||
} else if self.eat_keyword(kw::Mut) {
|
} else if self.eat_keyword(kw::Mut) {
|
||||||
self.parse_pat_ident_mut(syntax_loc)?
|
self.parse_pat_ident_mut(syntax_loc)?
|
||||||
} else if self.eat_keyword(kw::Ref) {
|
} else if self.eat_keyword(kw::Ref) {
|
||||||
|
if self.check_keyword(kw::Box) {
|
||||||
|
// Suggest `box ref` and quit parsing pattern to prevent series of
|
||||||
|
// misguided diagnostics from later stages of the compiler.
|
||||||
|
let span = self.prev_token.span.to(self.token.span);
|
||||||
|
return Err(self.sess.create_err(SwitchRefBoxOrder { span }));
|
||||||
|
}
|
||||||
// Parse ref ident @ pat / ref mut ident @ pat
|
// Parse ref ident @ pat / ref mut ident @ pat
|
||||||
let mutbl = self.parse_mutability();
|
let mutbl = self.parse_mutability();
|
||||||
self.parse_pat_ident(BindingAnnotation(ByRef::Yes, mutbl), syntax_loc)?
|
self.parse_pat_ident(BindingAnnotation(ByRef::Yes, mutbl), syntax_loc)?
|
||||||
|
14
tests/ui/pattern/pattern-bad-ref-box-order.fixed
Normal file
14
tests/ui/pattern/pattern-bad-ref-box-order.fixed
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
#![feature(box_patterns)]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
fn foo(f: Option<Box<i32>>) {
|
||||||
|
match f {
|
||||||
|
Some(box ref _i) => {},
|
||||||
|
//~^ ERROR switch the order of `ref` and `box`
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() { }
|
14
tests/ui/pattern/pattern-bad-ref-box-order.rs
Normal file
14
tests/ui/pattern/pattern-bad-ref-box-order.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
#![feature(box_patterns)]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
fn foo(f: Option<Box<i32>>) {
|
||||||
|
match f {
|
||||||
|
Some(ref box _i) => {},
|
||||||
|
//~^ ERROR switch the order of `ref` and `box`
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() { }
|
8
tests/ui/pattern/pattern-bad-ref-box-order.stderr
Normal file
8
tests/ui/pattern/pattern-bad-ref-box-order.stderr
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
error: switch the order of `ref` and `box`
|
||||||
|
--> $DIR/pattern-bad-ref-box-order.rs:8:14
|
||||||
|
|
|
||||||
|
LL | Some(ref box _i) => {},
|
||||||
|
| ^^^^^^^ help: swap them: `box ref`
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
Loading…
Reference in New Issue
Block a user