-
Notifications
You must be signed in to change notification settings - Fork 13k
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
More precise suggestion for non_upper_case_globals #48361
More precise suggestion for non_upper_case_globals #48361
Conversation
This makes the suggestion span for the non_upper_case_globals lint a bit more precise. Before it was highlighting the whole line. With this change it will highlight only the variable name itself.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Manishearth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
While we're here, it might also make sense to make this a structured suggestion, as @killercup suggests on the issue? That is, I think changing (line 357+) cx.span_lint(NON_UPPER_CASE_GLOBALS,
span,
&format!("{} `{}` should have an upper case name such as `{}`", sort, name, uc)); to something like let mut err = cx.struct_span_lint(NON_UPPER_CASE_GLOBALS, span, &format!("{} `{}` should have an upper case name");
err.span_suggestion(span, "use an uppercase name", uc);
err.emit(); should give a suggestion like
|
Hm, it seems like we can't get the span of the name directly. That's unfortunate -- we can't rely on In general manually building spans is a bad idea. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree that its unfortunate that we can't get the span of the name directly, There is precedent:
https://github.com/rust-lang/rust/blob/master/src/librustc_lint/builtin.rs#L1197-L1205
//~^ ERROR static variable `bar` should have an upper case name such as `BAR` | ||
|
||
fn main() { | ||
const b: usize = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading the code correctly, const c: usize = 1
would hilight the c
in const
, not the one on the name.
I also agree that using |
What's the status on this PR? |
☔ The latest upstream changes (presumably #48449) made this pull request unmergeable. Please resolve the merge conflicts. |
Ping from the release team! @phansch thank you for this PR! Recently, #48449 was merged, changing the way the output is checked for ui tests. You should rebase this PR on the master branch, and update the expected stderr for the @Manishearth or @rust-lang/compiler, can someone recommend the author the best way to generate a span in this case? |
I don't think there is one. We could try and output the warning during parsing as a builtin lint. |
@@ -342,6 +342,17 @@ impl NonUpperCaseGlobals { | |||
fn check_upper_case(cx: &LateContext, sort: &str, name: ast::Name, span: Span) { | |||
if name.as_str().chars().any(|c| c.is_lowercase()) { | |||
let uc = NonSnakeCase::to_snake_case(&name.as_str()).to_uppercase(); | |||
|
|||
let start = cx.tcx.sess.codemap().span_to_snippet(span) | |||
.map(|snippet| snippet.find(name.as_str().chars().as_str()).unwrap_or(0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to avoid the issue mentioned below, you should probably find the end of the first occurrence of whitespace, and only then find
the name
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If adding a span to AST, then the above comment is moot.
Can you change the AST to include the span of the ident (there used to be SpannedIdent, but I think it might have changed to something else).
This 100 times! I did it a lot in save-analysis and it has been a source of lots of pain. |
I'm wary of bloating the AST for a single lint -- are you okay with that tradeoff? We throw away the AST at some point so it's not too bad I think. |
Yeah, definitely. I'd like the AST to have every span in it eventually and this should be useful for save-analysis/IDEs too. |
@phansch ping from triage! Multiple members of the compiler team replied to this thread, can you finish this PR so it can be merged? |
Thank you so much for this PR @phansch! Unfortunately, we haven't heard from you in more than two weeks, so I'm closing this to keep the queue clean. If you have more time to work on this in the future, please reopen the PR so we can merge this! |
This makes the suggestion span for the
non_upper_case_globals
lint a bitmore precise. Before it was highlighting the whole line. With this change
it will highlight only the variable name itself.
Currently the way the span is calculated doesn't look very nice.
Especially the
name.as_str().chars().as_str()
which I used to turn anInternedString
into a&str
. Unfortunately I couldn't find a better way.r? @Manishearth
Fixes #48103