Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diagnostic when using public instead of pub #99903

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,17 @@ impl<'a> Parser<'a> {
self.last_unexpected_token_span = Some(self.token.span);
let mut err = self.struct_span_err(self.token.span, &msg_exp);

if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
if symbol.as_str() == "public" {
err.span_suggestion_short(
self.prev_token.span,
"write `pub` instead of `public` to make the item public",
"pub",
appl,
);
}
}

// Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
// there are unclosed angle brackets
if self.unmatched_angle_bracket_count > 0
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/parser/public-instead-of-pub.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Checks what happens when `public` is used instead of the correct, `pub`
// edition:2018
// run-rustfix
pub struct X;
//~^ ERROR expected one of `!` or `::`, found keyword `struct`
//~^^ HELP write `pub` instead of `public` to make the item public

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/parser/public-instead-of-pub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Checks what happens when `public` is used instead of the correct, `pub`
// edition:2018
// run-rustfix
public struct X;
//~^ ERROR expected one of `!` or `::`, found keyword `struct`
//~^^ HELP write `pub` instead of `public` to make the item public

fn main() {}
13 changes: 13 additions & 0 deletions src/test/ui/parser/public-instead-of-pub.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: expected one of `!` or `::`, found keyword `struct`
--> $DIR/public-instead-of-pub.rs:4:8
|
LL | public struct X;
| ^^^^^^ expected one of `!` or `::`
|
help: write `pub` instead of `public` to make the item public
|
LL | pub struct X;
| ~~~

error: aborting due to previous error