new module style lint: inline_module#16732
new module style lint: inline_module#16732zihan0822 wants to merge 1 commit intorust-lang:masterfrom
inline_module#16732Conversation
|
r? @dswij rustbot has assigned @dswij. Use Why was this reviewer chosen?The reviewer was selected based on:
|
026b819 to
c49733c
Compare
|
Lintcheck changes for f10dc9d
This comment will be updated if you push new changes |
module changelog: new lint: [`inline_module`] Signed-off-by: Zihan <zihanli0822@gmail.com>
c49733c to
f10dc9d
Compare
|
r? clippy |
| 1 | mod foo {} | ||
| | ^^^^^^^ | ||
| | | ||
| = help: move to `src/stuff/foo/mod.rs` or `src/stuff/foo.rs` |
There was a problem hiding this comment.
Hm, I don't think this will even work, and there are not one but two interesting reasons!
First of all, it looks like you can't really "outline" inline modules called the same as their parent if the parent module is defined using #[path]. For example, in this code:
// `src/lib.rs`
#[path = "foo.rs"]
mod stuff;
// `src/foo.rs`
mod foo {
// contents
}you can't move the inline mod foo into a separate file, because wherever you move it, you'll need to leave mod foo; in its place, and that causes an error:
error: circular modules: src/foo.rs -> src/foo.rs
--> src/foo.rs:1:1
|
1 | mod foo;
| ^^^^^^^^
error: could not compile `test` (bin "test") due to 1 previous error
Secondly and relatedly, even if you rename the child module to something different:
// `src/lib.rs`
#[path = "foo.rs"]
mod stuff;
// `src/foo.rs`
mod bar {
/* contents of `bar` */
}, the currently suggested path would be src/stuff/bar.rs, but it looks like what the compiler actually wants is just src/bar.rs?.. Honestly I would've expected src/foo/bar.rs.
As you can see, very weird things are happening here. So imo it would be the easiest to just not deal with any modules whose parent (I think grandparents should be fine, but please check that) has the #[path] attribute
|
Reminder, once the PR becomes ready for a review, use |
fixes: #15966
add new module style restriction lint that checks for use of inline module, with an exception for test module.
changelog: new lint: [
inline_module]