Suggest similar keyword when visibility is not followed by an item

This commit is contained in:
Francisco Gouveia
2026-03-29 19:34:27 +01:00
parent 584d32e3ee
commit 7f53271474
3 changed files with 43 additions and 1 deletions
+16 -1
View File
@@ -192,7 +192,22 @@ pub(super) fn parse_item_common(
// At this point, we have failed to parse an item.
if !matches!(vis.kind, VisibilityKind::Inherited) {
this.dcx().emit_err(errors::VisibilityNotFollowedByItem { span: vis.span, vis });
let mut err = this
.dcx()
.create_err(errors::VisibilityNotFollowedByItem { span: vis.span, vis });
if let Some((ident, _)) = this.token.ident()
&& !ident.is_used_keyword()
&& let Some((similar_kw, is_incorrect_case)) = ident
.name
.find_similar(&rustc_span::symbol::used_keywords(|| ident.span.edition()))
{
err.subdiagnostic(errors::MisspelledKw {
similar_kw: similar_kw.to_string(),
span: ident.span,
is_incorrect_case,
});
}
err.emit();
}
if let Defaultness::Default(span) = def {
@@ -0,0 +1,5 @@
pub cnst fn code() {}
//~^ ERROR visibility `pub` is not followed by an item
//~| ERROR expected item, found `cnst`
fn main() {}
@@ -0,0 +1,22 @@
error: visibility `pub` is not followed by an item
--> $DIR/pub-const-fn.rs:1:1
|
LL | pub cnst fn code() {}
| ^^^ the visibility
|
= help: you likely meant to define an item, e.g., `pub fn foo() {}`
help: there is a keyword `const` with a similar name
|
LL | pub const fn code() {}
| +
error: expected item, found `cnst`
--> $DIR/pub-const-fn.rs:1:5
|
LL | pub cnst fn code() {}
| ^^^^ expected item
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 2 previous errors