-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Suggest invalid expression when use public
#100165
Comments
@rustbot claim |
Oh dear, I didn't think of that while making the PR. 😅 A possible solution could be checking if the current token is for initializing an item? Like, struct/enum/macro, etc. |
I wrote this solution, but I don't know if I can use it in rustc, it's currently building 😁 let is_valid_stmt: bool = syn::parse_str::<syn::Stmt>("pub let some = 3;")
.is_ok(); // false |
I can't work on the problem unfortunately, whoever will assign it to him you can solve it this way can_be_public = ["enum", "struct", "fn", "extern", "trait"];
if can_be_public.contains(&self.token.as_str()) {
// ...
} The block rust/compiler/rustc_parse/src/parser/diagnostics.rs Lines 605 to 614 in affe0d3
|
@rustbot release-assignment |
@rustbot claim |
Trying to add a method in token: /// Returns `true` if the token the start of a item
pub fn can_begin_item(&self) -> bool {
self.is_keyword(kw::Use)
|| self.is_keyword(kw::Fn)
|| self.is_keyword(kw::Extern)
|| self.is_keyword(kw::Crate)
|| self.is_keyword(kw::Mod)
|| self.is_keyword(kw::Const)
|| self.is_keyword(kw::Static)
|| self.is_keyword(kw::Trait)
|| self.is_keyword(kw::Impl)
|| self.is_keyword(kw::Mod)
|| self.is_keyword(kw::Type)
|| self.is_keyword(kw::Enum)
|| self.is_keyword(kw::Struct)
|| self.is_keyword(kw::Union)
} And add a check: if self.prev_token.is_ident_named(sym::public) && self.token.can_begin_item() {
err.span_suggestion_short(
self.prev_token.span,
"write `pub` instead of `public` to make the item public",
"pub",
appl,
);
} Any other better idea? @gimbles |
|
…ebank Parser will not suggest invalid expression when use public Fixes rust-lang#100165
…ebank Parser will not suggest invalid expression when use public Fixes rust-lang#100165
…ebank Parser will not suggest invalid expression when use public Fixes rust-lang#100165
When writing
public let some = 3;
it will suggestpub let some = 3;
and I think nothing should be suggested before it is validated.The issue from: #99903
Given the following code:
The current output is:
Ideally the output should look like:
The text was updated successfully, but these errors were encountered: