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

Detect ; -> , typo on vec!["_", 10] #80173

Closed
estebank opened this issue Dec 19, 2020 · 7 comments · Fixed by #128110
Closed

Detect ; -> , typo on vec!["_", 10] #80173

estebank opened this issue Dec 19, 2020 · 7 comments · Fixed by #128110
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Dec 19, 2020

Only when writing vec!["_", 10] we could suggest replacing the , with ;.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Dec 19, 2020
@jonas-schievink jonas-schievink added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Dec 19, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Dec 30, 2020
@osa1
Copy link
Contributor

osa1 commented Jan 28, 2021

Is it possible to do this in a declarative macro?

I wonder if we could have a "peephole warning/suggestion generator" for this kind of thing where we pattern match in terms in different contexts (e.g. types, expressions, items, perhaps with the errors in the spans) and suggest alternatives.

@estebank
Copy link
Contributor Author

Today it is possible to do this in a proc-macro (by explicitly looking for cases), and in the compiler (rustc today when encountering a macro call that doesn't match any arm will look for places to add a comma token and see if that would match any macro arm. If so, it suggests the comma in the right place). In declarative macros we don't have a good way of doing this, other than maybe using std::compile_error as shown in its docs.

Edit before submitting: Actually, this has to be in the compiler because macros do not have access to the type information, which you need to properly handle this. The first field can be anything that is Copy, while the second field has to be an integer literal or usize.

@henryboisdequin
Copy link
Contributor

@estebank Would this help message only be emitted when the code fails and/or when the program compiles successfully (if there is a way to do such a thing)?

@estebank
Copy link
Contributor Author

Only on error. The only "suspect" cases would be like vec![1, 42] and at that point that is fine, there are plenty of valid reasons for that code to be written. Only in cases where there's an error being emitted it makes sense to help, as the typo might not be obvious.

@henryboisdequin
Copy link
Contributor

@rustbot claim

@henryboisdequin
Copy link
Contributor

@estebank Do you know roughly where I need to search for cases such as this one? Thanks

@veera-sivarajan
Copy link
Contributor

@rustbot claim

@fmease fmease changed the title Detect ; -> , typo on vec!["_", 10] Detect ; -> , typo on vec!["_", 10] Jul 31, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 8, 2025
…gillot

Suggest Replacing Comma with Semicolon in Incorrect Repeat Expressions

Fixes rust-lang#80173

This PR detects typos in repeat expressions like `["_", 10]` and `vec![String::new(), 10]` and suggests replacing comma with semicolon.

Also, improves code in other place by adding doc comments and making use of a helper function to check if a type implements `Clone`.

References:
1. For `vec![T; N]`: https://doc.rust-lang.org/std/macro.vec.html
2. For `[T; N]`: https://doc.rust-lang.org/std/primitive.array.html
@bors bors closed this as completed in e4e2d9c Jan 9, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 9, 2025
Rollup merge of rust-lang#128110 - veera-sivarajan:bugfix-80173, r=cjgillot

Suggest Replacing Comma with Semicolon in Incorrect Repeat Expressions

Fixes rust-lang#80173

This PR detects typos in repeat expressions like `["_", 10]` and `vec![String::new(), 10]` and suggests replacing comma with semicolon.

Also, improves code in other place by adding doc comments and making use of a helper function to check if a type implements `Clone`.

References:
1. For `vec![T; N]`: https://doc.rust-lang.org/std/macro.vec.html
2. For `[T; N]`: https://doc.rust-lang.org/std/primitive.array.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants