Rollup merge of #154561 - FranciscoTGouveia:typo-detection-after-visibility, r=TaKO8Ki

Suggest similar keyword when visibility is not followed by an item

Fixes rust-lang/rust#153353.

I would appreciate feedback on the following:
- I inlined [`find_similar_kw`](https://github.com/rust-lang/rust/blob/cd14b73b4a41542d921f59e362a5b5005fa4f2ef/compiler/rustc_parse/src/parser/diagnostics.rs#L218-L224) instead of changing its visibility to `pub(super)`. I am happy to switch if it is preferred;
- I didn't add a comment to the new test. Although the rustc-dev-guide specifies that a [comment should be added](https://rustc-dev-guide.rust-lang.org/tests/adding.html#comment-explaining-what-the-test-is-about) to every new test, this is not common in `tests/ui/parser/misspelled_keywords/` and the test seems self-explanatory. Let me know if you would like me to add a comment;

Thank you in advance for your time and input!
This commit is contained in:
Jonathan Brouwer
2026-04-06 08:27:50 +02:00
committed by GitHub
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 {