-
Notifications
You must be signed in to change notification settings - Fork 1.7k
update_lints rewrite: Add structure and --print-only #2985
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
Conversation
clippy_dev/src/lib.rs
Outdated
use std::io::prelude::*; | ||
|
||
lazy_static! { | ||
static ref DEC_CLIPPY_LINT_RE: Regex = Regex::new(r#"declare_clippy_lint!\s*[\{(]\s*pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*(?P<cat>[a-z_]+)\s*,\s*"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]"#).unwrap(); |
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.
Is there a way to split these up over multiple lines like in the python script? That would make them much more readable.
https://github.com/rust-lang-nursery/rust-clippy/blob/97840090b78ce1f370c3380e8a703da420fe3a98/util/update_lints.py#L11-L15
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.
clippy_dev/src/main.rs
Outdated
"nursery", | ||
"restriction" | ||
]; | ||
// We could use itertools' group_by to make this much more concise: |
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.
Just saw that we already use itertools in Clippy, so I will use that in here, too.
clippy_dev/src/lib.rs
Outdated
|
||
pub fn collect_all() -> Vec<Lint> { | ||
let mut lints = vec![]; | ||
for direntry in lint_files() { |
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.
dir_entry
(instead of direntry
) is easier on my eyes
(will get back to this and the other PR and the end of the month) |
77fa255
to
cf89c58
Compare
9e674eb
to
fe7e76a
Compare
fe7e76a
to
be995dc
Compare
I think this is ready for a final review |
I think this is because it thinks it's part of the workspace. Not sure if that is resolveable |
clippy_dev/.gitignore
Outdated
@@ -0,0 +1,38 @@ | |||
# Used by Travis to be able to push: | |||
/.github/deploy_key |
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.
Is it possible to just update our main .gitignore?
clippy_dev/src/lib.rs
Outdated
} | ||
} | ||
|
||
pub fn active_lints(lints: &[Lint]) -> Vec<Lint> { |
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.
I kind of feel like all the Vec
returning functions here should be returning impl Iterator<Item=Lint>
. That does require some string allocations, but should generally be possible.
Not a blocker for this PR, just a nice to have. You can also open an issue and leave it as a "good first issue"
This makes the API of `lib.rs` a bit more flexible.
clippy_dev/src/lib.rs
Outdated
@@ -56,22 +57,22 @@ impl Lint { | |||
} | |||
} | |||
|
|||
pub fn collect_all() -> Vec<Lint> { | |||
pub fn gather_all() -> impl Iterator<Item=Lint> { |
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.
this entire function can probably be lint_files().flat_map(identity)
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.
This is my favorite refactoring in Ruby and I'm SO happy it's possible in Rust 🎉
clippy_dev/src/lib.rs
Outdated
let mut file = fs::File::open(dir_entry.path()).unwrap(); | ||
let mut content = String::new(); | ||
file.read_to_string(&mut content).unwrap(); | ||
parse_contents(&content, dir_entry.path().file_stem().unwrap().to_str().unwrap()) | ||
} | ||
|
||
fn parse_contents(content: &str, filename: &str) -> Vec<Lint> { | ||
fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item=Lint> { | ||
let mut lints: Vec<Lint> = DEC_CLIPPY_LINT_RE |
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.
chain the two iterators and remove the collect&append&into_iter
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 don't collect the map result, I run into a lot of lifetime issues, because contents
and filename
are used in the iterator and would outlive the function. I will push the changes in a bit with a comment.
clippy_dev/src/lib.rs
Outdated
} | ||
|
||
/// Collects all .rs files in the `clippy_lints/src` directory | ||
fn lint_files() -> Vec<fs::DirEntry> { | ||
fn lint_files() -> impl Iterator<Item=fs::DirEntry> { | ||
let paths = fs::read_dir("../clippy_lints/src").unwrap(); |
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.
remove the intermedate paths
variable
.captures_iter(content) | ||
.map(|m| Lint::new( &m["name"], "Deprecated", &m["desc"], Some(&m["desc"]), filename)); | ||
// Removing the `.collect::<Vec<Lint>>().into_iter()` causes some lifetime issues due to the map | ||
lints.chain(deprecated).collect::<Vec<Lint>>().into_iter() |
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 remove the collect().into_iter()
here, I get lifetime 'static' required
for content
and filename
, which makes sense because they are used in the iterator and consumed outside of this function. I feel like making them static doesn't make sense here and would make the code more complicated to read.
dedfb35
to
de36d42
Compare
Part of #2882
This adds a
clippy_dev
project that currently usesclap
,regex
andlazy_static
to print a list of lints. For now it only replaces the lint printing inupdate_lints.py
.Running
util/dev update_lints --print-only
from scratch takes 45s on my machine.I could not make it use a
stable
toolchain because somehow running cargo test in the subdirectory parses the Clippy Cargo.toml, too: