-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add module_style lint to style #7543
Conversation
r? @Manishearth (rust-highfive has picked a reviewer for you, use r? to override) |
clippy_lints/src/module_style.rs
Outdated
let p = p.to_string_lossy(); | ||
let path = Path::new(p.trim_start_matches(trim_to_src.as_ref())); | ||
|
||
if path.ends_with("mod.rs") { |
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 should not trigger for something_mod.rs
.
I would just emit the lint right here. It's okay to emit for every module. Also, you should be able to get a Span
using Span::new(file.start_pos, file.start_pos, SyntaxContext::root())
.
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.
It shouldn't Path::ends_with
is different that str::ends_with
, it only considers whole segments.
I can't emit until I know if there are any missing mod.rs
files this is the mod_file
check. I could still collect all the folder names (after src
) and collect all the mod.rs file paths and remove all the folders that contain mod files, emitting for all the folders that are left after. I would have to keep more info around and haven't figured out all the details but it would probably work, then the error messages would at least be more consistent.
I think it would be simpler to make this two complimentary restriction lints - |
So we discussed this in the meeting yesterday and it's not really clear to me if this is an accepted style preference; in my experience codebases are fine with mixing them since the approaches sometimes have their own benefits. I think this should be a |
I did see the meeting and my suggestion is in addition to that. (I should have clarified that) |
Oh, I wasn't responding to you, I had just forgotten to update the PR 😄 |
Oh I see how it is! 😆 |
clippy_lints/src/module_style.rs
Outdated
/// stuff.rs | ||
/// lib.rs | ||
/// ``` | ||
pub MOD_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.
thoughts on improving the lint 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.
mod_module_files
and the other could be self_named_module_files
🤷 I was hoping when I looked up the RFC that introduced the new module system there would be a good name for it but there is not.
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.
@camsteffen thoughts?
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.
Hmmm. I think @DevinR528's idea is good. Another approach is mod_file_within_dir
and mod_file_outside_dir
but I don't like that better.
@@ -4,7 +4,7 @@ error: `mod.rs` files are required, found `/bad/inner.rs` | |||
LL | pub mod stuff; | |||
| ^ | |||
| | |||
= note: `-D clippy::self-named-mod-files` implied by `-D warnings` | |||
= note: `-D clippy::self-named-module-files` implied by `-D warnings` | |||
= help: create a `/bad/inner/mod.rs` file and remove `/bad/inner.rs` |
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.
Should the suggestion simple be "move bad/inner.rs
to bad/inner/mod.rs
, rather than "create", "remove"?
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 changed both error messages to the above suggestion
Should I squash the commits or are there any more things to fix? |
Nah I should just review this 😄 |
Squashing would be appreciated though! |
1bc9d5c
to
5a69424
Compare
Squashed! |
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.
almost there!
clippy_lints/src/module_style.rs
Outdated
return; | ||
}; | ||
|
||
let mut segments = FxHashSet::default(); |
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.
nit: can we have some comments on what these variables contain
clippy_lints/src/module_style.rs
Outdated
} | ||
} | ||
|
||
fn check_mod_file_exists( |
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.
nit: also document the purpose of the function
especially since this does a different kind of "check" than the other one
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 renamed this function to process_paths_for_mod_files
, as you pointed out there is no check there so it didn't make much sense hope this works
Add tests for disallowed_mod in ui-cargo test section Use correct algorithm to determine if mod.rs is missing Move to two lints and remove config option Switch lint names so they read "warn on ..." Emit the same help info for self_named_mod_file warnings Bail when both lints are Allow Reword help message for both module_style lints
5a69424
to
31738b1
Compare
@bors r+ Thanks for your patience! |
📌 Commit 31738b1 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Thanks for helping me along! |
This lint is going into the style group which means it will be enabled by default. Has a crater run been performed to assess how much churn this will cause? |
@lopopolo If you have a look at the description and discussion above, you will see that this lint was never enabled by default. Originally it was a |
changelog: Add new [
module_style
] style lintThis is a configurable (no mod file/mod file) lint that determines if
mod.rs
is used consistently or ifmod.rs
is never used (using the new mod layout).