Suggest remove on const async const instead of const const async
This commit is contained in:
parent
7bc8577d99
commit
5c9950023b
1 changed files with 45 additions and 16 deletions
|
@ -1938,33 +1938,62 @@ impl<'a> Parser<'a> {
|
||||||
Ok(false) => unreachable!(),
|
Ok(false) => unreachable!(),
|
||||||
Err(mut err) => {
|
Err(mut err) => {
|
||||||
// Qualifier keywords ordering check
|
// Qualifier keywords ordering check
|
||||||
|
enum WrongKw {
|
||||||
|
Duplicated(Span),
|
||||||
|
Misplaced(Span),
|
||||||
|
}
|
||||||
|
|
||||||
// This will allow the machine fix to directly place the keyword in the correct place
|
// This will allow the machine fix to directly place the keyword in the correct place or to indicate
|
||||||
let current_qual_sp = if self.check_keyword(kw::Const) {
|
// that the keyword is already present and the second instance should be removed.
|
||||||
Some(async_start_sp)
|
let wrong_kw = if self.check_keyword(kw::Const) {
|
||||||
|
match constness {
|
||||||
|
Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
|
||||||
|
Const::No => Some(WrongKw::Misplaced(async_start_sp)),
|
||||||
|
}
|
||||||
} else if self.check_keyword(kw::Async) {
|
} else if self.check_keyword(kw::Async) {
|
||||||
Some(unsafe_start_sp)
|
match asyncness {
|
||||||
|
Async::Yes { span, .. } => Some(WrongKw::Duplicated(span)),
|
||||||
|
Async::No => Some(WrongKw::Misplaced(unsafe_start_sp)),
|
||||||
|
}
|
||||||
} else if self.check_keyword(kw::Unsafe) {
|
} else if self.check_keyword(kw::Unsafe) {
|
||||||
Some(ext_start_sp)
|
match unsafety {
|
||||||
|
Unsafe::Yes(sp) => Some(WrongKw::Duplicated(sp)),
|
||||||
|
Unsafe::No => Some(WrongKw::Misplaced(ext_start_sp)),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(current_qual_sp) = current_qual_sp {
|
// The keyword is already present, suggest removal of the second instance
|
||||||
let current_qual_sp = current_qual_sp.to(self.prev_token.span);
|
if let Some(WrongKw::Duplicated(original_sp)) = wrong_kw {
|
||||||
if let Ok(current_qual) = self.span_to_snippet(current_qual_sp) {
|
let original_kw = self
|
||||||
let invalid_qual_sp = self.token.uninterpolated_span();
|
.span_to_snippet(original_sp)
|
||||||
let invalid_qual = self.span_to_snippet(invalid_qual_sp).unwrap();
|
.expect("Span extracted directly from keyword should always work");
|
||||||
|
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
current_qual_sp.to(invalid_qual_sp),
|
self.token.uninterpolated_span(),
|
||||||
&format!("`{}` must come before `{}`", invalid_qual, current_qual),
|
&format!("`{}` already used earlier, remove this one", original_kw),
|
||||||
format!("{} {}", invalid_qual, current_qual),
|
"".to_string(),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
)
|
||||||
|
.span_note(original_sp, &format!("`{}` first seen here", original_kw));
|
||||||
|
}
|
||||||
|
// The keyword has not been seen yet, suggest correct placement in the function front matter
|
||||||
|
else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
|
||||||
|
let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
|
||||||
|
if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
|
||||||
|
let misplaced_qual_sp = self.token.uninterpolated_span();
|
||||||
|
let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
|
||||||
|
|
||||||
|
err.span_suggestion(
|
||||||
|
correct_pos_sp.to(misplaced_qual_sp),
|
||||||
|
&format!("`{}` must come before `{}`", misplaced_qual, current_qual),
|
||||||
|
format!("{} {}", misplaced_qual, current_qual),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
).note("keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`");
|
).note("keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Recover incorrect visibility order such as `async pub`.
|
// Recover incorrect visibility order such as `async pub`
|
||||||
else if self.check_keyword(kw::Pub) {
|
else if self.check_keyword(kw::Pub) {
|
||||||
let orig_vis = vis.unwrap_or(&Visibility {
|
let orig_vis = vis.unwrap_or(&Visibility {
|
||||||
span: rustc_span::DUMMY_SP,
|
span: rustc_span::DUMMY_SP,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue