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

More precise suggestion for non_upper_case_globals #48361

Conversation

phansch
Copy link
Member

@phansch phansch commented Feb 19, 2018

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.

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 an
InternedString into a &str. Unfortunately I couldn't find a better way.

r? @Manishearth

Fixes #48103

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.
@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 19, 2018
@zackmdavis
Copy link
Member

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

warning: static variable `hello` should have an upper case name
 --> src/main.rs:3:1
  |
3 | static hello: &str = "Hello";
  |        ^^^^^ use an uppercase name: `HELLO`
  |

@Manishearth
Copy link
Member

Hm, it seems like we can't get the span of the name directly. That's unfortunate -- we can't rely on span_to_snippet because of macros and such; so this seems pretty fragile. I'm not sure what we can do here.

In general manually building spans is a bad idea.

Copy link
Contributor

@oli-obk oli-obk left a 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;
Copy link
Contributor

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.

@phansch
Copy link
Member Author

phansch commented Feb 20, 2018

I also agree that using span_to_snippet is not very robust, it also caused some issues with attribute linting and macros in clippy. I saw some other lints in rustc that seem to highlight the variable name correctly, but they rely on hir::Pat instead of hir::Item as far as I can tell.

@pietroalbini
Copy link
Member

What's the status on this PR?

@bors
Copy link
Contributor

bors commented Feb 27, 2018

☔ The latest upstream changes (presumably #48449) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 27, 2018
@pietroalbini
Copy link
Member

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 lint-group-style test.

@Manishearth or @rust-lang/compiler, can someone recommend the author the best way to generate a span in this case?

@Manishearth
Copy link
Member

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))
Copy link
Contributor

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.

Copy link
Contributor

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.

@nrc
Copy link
Member

nrc commented Mar 6, 2018

Hm, it seems like we can't get the span of the name directly.

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).

In general manually building spans is a bad idea.

This 100 times! I did it a lot in save-analysis and it has been a source of lots of pain.

@Manishearth
Copy link
Member

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.

@nrc
Copy link
Member

nrc commented Mar 7, 2018

are you okay with that tradeoff?

Yeah, definitely. I'd like the AST to have every span in it eventually and this should be useful for save-analysis/IDEs too.

@pietroalbini
Copy link
Member

@phansch ping from triage! Multiple members of the compiler team replied to this thread, can you finish this PR so it can be merged?

@pietroalbini
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants