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

Lint to check alphabetical order of uses, mods & crates #7546

Closed
wants to merge 2 commits into from

Conversation

@wafflespeanut
Copy link
Member

wafflespeanut commented Sep 4, 2015

fixes #7451

Review on Reviewable

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 4, 2015

@Manishearth There are two things left. For now, this only gets the names of extern crates and uses and puts them into a vector. So, how shall I go about getting the names of mods? And, how should I sort the strings in the vectors? (so that I can compare them later?).

@nox
Copy link
Member

nox commented Sep 7, 2015

This has the exact same problem as the Python lint, in which import lists aren't checked for sortedness, right?

I mean things like:

use mod::{foo, bar, qux};
@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 7, 2015

@nox No, only the individual declarations. I think checking for sortedness in import lists is probably a bad idea, since some of the giant files have a lot of lists and arranging them would be painful :)

@Manishearth
Copy link
Member

Manishearth commented Sep 7, 2015

If the lint outputs a sorted list to be used, it wouldn't be so bad. But this PR is fine too, we can discuss whether or not to augment the functionality.

@Manishearth
Copy link
Member

Manishearth commented Sep 7, 2015

Reviewed 3 of 3 files at r1.
Review status: all files reviewed at latest revision, 1 unresolved discussion, all commit checks successful.


components/plugins/lints/sorter.rs, line 37 [r1] (raw file):
We may want to glob import in the future, so blocking std::prelude is a better idea here. But this is okay too.


Comments from the review on Reviewable.io

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 8, 2015

Oh yeah, right. I now remember that we'll be actually suggesting how the stuff should be sorted :)

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 14, 2015

Now, there's a InternedString <--> String problem

@Manishearth
Copy link
Member

Manishearth commented Sep 14, 2015

You don't need to put r=Manishearth in your commits. Homu will do that in the merge commit

@wafflespeanut wafflespeanut force-pushed the wafflespeanut:alpha_lints branch 2 times, most recently from 5d8b319 to dda0804 Sep 15, 2015
@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 15, 2015

@Manishearth I'm not sure whether it's pretty, but we have a working lint! ^_^

Here's how crates are checked (same goes for mods)...

crates

Here's how use lists are checked (cc @nox)...

use_list

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 15, 2015

This still needs more polishing...

@jdm
Copy link
Member

jdm commented Sep 15, 2015

Calling the lint sorted_declarations means that deny(sorted_declarations) actually means "disallow unsorted declarations", which is a bit confusing.

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 15, 2015

Yeah, right. That should be deny(unsorted_declarations) :)

@Manishearth
Copy link
Member

Manishearth commented Sep 15, 2015

Reviewed 1 of 1 files at r2, 1 of 1 files at r3.
Review status: all files reviewed at latest revision, 5 unresolved discussions, some commit checks failed.


components/plugins/lints/sorter.rs, line 34 [r2] (raw file):
Also ignore mods with attributes


components/plugins/lints/sorter.rs, line 55 [r2] (raw file):
You need to sort it so that self comes at the beginning of the list


components/plugins/lints/sorter.rs, line 87 [r3] (raw file):
&Vec should be &[..]

Also, T: Deref<Target=String> should be enough, reallyx


components/plugins/lints/sorter.rs, line 93 [r2] (raw file):
Error message should be better.

Something like

{} should be in alphabetical order\n\texpected: {}\n\tfound: {}

where the first {} is either "use statements", "module declarations", or "crate declarations"


Comments from the review on Reviewable.io

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 17, 2015

Now, all that's left is to sort the declarations in the existing files (which is gonna be a pain!)

@jdm jdm removed the S-fails-tidy label Sep 17, 2015
use syntax::codemap::{Pos, BytePos, Span};
use syntax::print::pprust::path_to_string;

declare_lint!(UNSORTED_DECLARATIONS, Deny,

This comment has been minimized.

@Manishearth

Manishearth Sep 17, 2015

Member

This should be Warn. Deny is going to be very annoying when you're fiddling with imports temporarily before checking in.

let suggestion = format!("use lists should be in alphabetical order!\
\n\texpected order of list: {{{}}}\
\n\tlist found: {{{}}}",
new_list.join(", "), old_list.join(", "));

This comment has been minimized.

@Manishearth

Manishearth Sep 17, 2015

Member

Not like this.

Use lists should be in alphabetical order.
try:
use foo::bar::baz::{a,b,c,d};

print out a fully copyable use statement

if name != new_slice[i] {
let suggestion = format!("{} should be in alphabetical order!\n\t\
expected: {}\n\tfound: {}",
kind, new_slice[i], name);

This comment has been minimized.

@wafflespeanut wafflespeanut force-pushed the wafflespeanut:alpha_lints branch from 4ee0c6a to 6e18618 Sep 18, 2015
@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 18, 2015

This is still incomplete. Firstly, I gotta cripple the lint, so that it doesn't walk into servo/target/... and check the generated DOM bindings. And second, I haven't yet given any preference to the pub stuff. So far, the sorting is based on the names of the declarations.

Sidenote: I'm still not sure whether this is efficient (I've tried my best), and so, I welcome suggestions :)

@jdm
Copy link
Member

jdm commented Sep 18, 2015

Alternatively, we could fix the generated bindings.

@wafflespeanut wafflespeanut force-pushed the wafflespeanut:alpha_lints branch from ad3cbbe to f417d56 Sep 18, 2015
@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 18, 2015

This just keeps getting better and better! Now, we gotta filter the additional attributes that come clinging with the mod declarations (for instance, this shows #[allow(non_camel_case_types)] even though it's not actually declared there, but here).

Also, the lint doesn't care about renamed declarations (for now).

extern crate foobar as foo    // this is interpreted as `extern crate foo`

... which should also be fixed as well.

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 19, 2015

(current status)

screenshot

@bors-servo
Copy link
Contributor

bors-servo commented Sep 19, 2015

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

@wafflespeanut wafflespeanut force-pushed the wafflespeanut:alpha_lints branch 4 times, most recently from ed2e797 to 03a3999 Sep 20, 2015
@wafflespeanut wafflespeanut force-pushed the wafflespeanut:alpha_lints branch from 03a3999 to b064189 Sep 20, 2015
@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 20, 2015

Alright, tidy doesn't give up. It expects pub mod before mod, but our sorting is something like #[macro_use] stuff at the start, followed by the private mods and then followed by pub mods.

@jdm
Copy link
Member

jdm commented Sep 20, 2015

It seems to me that the main downside of doing this check in a lint vs. in the python script is that we lose the immediate feedback that travis provides, so we won't notice that a PR needs amending until it fails to merge.

@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 21, 2015

@jdm Agreed. Also, since the lint only throws warnings, we can't know whether a PR has merged with warnings. (sigh) after all that work... :/

@Manishearth maybe we should go back to throwing errors! :)

@Manishearth
Copy link
Member

Manishearth commented Sep 21, 2015

No. No errors. Go does this with unused imports, and it's extremely annoying when this happens while you're developing. So much that a myriad of solutions, ranging from auto-import-fixers to modified compilers have come out.

Style lints should not be errors.

@wafflespeanut wafflespeanut deleted the wafflespeanut:alpha_lints branch Sep 21, 2015
@wafflespeanut
Copy link
Member Author

wafflespeanut commented Sep 21, 2015

Okay, since this lint doesn't fit very well here (nor rustfmt), I've planned to move this elsewhere. I'll just submit a PR for the files (well, I can't just abandon all the work, it's consumed a lot of my time! :P)

bors-servo pushed a commit that referenced this pull request Sep 23, 2015
sorted the declarations in various files...

This is a direct extract from my abandoned PR for a lint (#7546), along with some rather clumsy modifications (only on `components/script/dom/mod.rs` and `components/style/lib.rs`), because I had to sort some of the files again to make peace with tidy, which hasn't been educated about sorting yet!

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7698)
<!-- Reviewable:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

6 participants
You can’t perform that action at this time.