From 533cb3a982e508ea5729ce45ce2cca13bf63d5e9 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sat, 9 Sep 2023 21:48:07 -0400 Subject: [PATCH] New "module_missing" lint to detect missing, renamed, or non-visible modules (#534) * Update lockfile to latest trustfall-rustdoc-adapter(s) * Add a new "module_missing" lint Resolves #482. * module_missing: Add expected positives for trait_missing* * fixup! Add a new "module_missing" lint Tweak error message. * fixup! Add a new "module_missing" lint Adjust formatting on test_crates/module_missing * Update test_crates/module_missing/new/src/lib.rs --------- Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- Cargo.lock | 24 +++++------ src/lints/module_missing.ron | 46 +++++++++++++++++++++ src/query.rs | 1 + test_crates/module_missing/new/Cargo.toml | 7 ++++ test_crates/module_missing/new/src/lib.rs | 24 +++++++++++ test_crates/module_missing/old/Cargo.toml | 7 ++++ test_crates/module_missing/old/src/lib.rs | 13 ++++++ test_outputs/module_missing.output.ron | 49 +++++++++++++++++++++++ 8 files changed, 159 insertions(+), 12 deletions(-) create mode 100644 src/lints/module_missing.ron create mode 100644 test_crates/module_missing/new/Cargo.toml create mode 100644 test_crates/module_missing/new/src/lib.rs create mode 100644 test_crates/module_missing/old/Cargo.toml create mode 100644 test_crates/module_missing/old/src/lib.rs create mode 100644 test_outputs/module_missing.output.ron diff --git a/Cargo.lock b/Cargo.lock index 7e18dc05..1b019f70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3176,9 +3176,9 @@ dependencies = [ [[package]] name = "trustfall-rustdoc-adapter" -version = "23.6.0" +version = "23.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99f8e47989facdc13cd06bea85e0c1a9d87a6f17a05ab9d1d9d8de8add1ba8e5" +checksum = "ece63d2d0d19a07724d43f03dd9daea592ce3faed3ee12d0354c681c23a9f922" dependencies = [ "rustdoc-types 0.19.0", "trustfall", @@ -3186,9 +3186,9 @@ dependencies = [ [[package]] name = "trustfall-rustdoc-adapter" -version = "24.5.0" +version = "24.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb2158e9583a0487c2a77e372636819bd03fee25cedb048e580b788ae054cf5f" +checksum = "8900ed1075c0fddef370e47b73920368b33562e0602019c37476f8abf0ff7a09" dependencies = [ "rustdoc-types 0.20.0", "trustfall", @@ -3196,9 +3196,9 @@ dependencies = [ [[package]] name = "trustfall-rustdoc-adapter" -version = "26.2.0" +version = "26.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c3b046aa70f7199223e1ef4dc46b2d904ce42f4a3fb726ee712c98ea44e782e" +checksum = "f208dd20ab7220f9d54aee3c6082377376d224ae3b4d3d0b980c02f2070786e2" dependencies = [ "rustdoc-types 0.22.0", "trustfall", @@ -3206,9 +3206,9 @@ dependencies = [ [[package]] name = "trustfall-rustdoc-adapter" -version = "27.0.0" +version = "27.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dd0afa66d619e0b540eac19b6cc6892fb4f5dd6a02e9c5171d3433e6f29cbb3" +checksum = "437e8fadfb04a4b708e96552a3bb1232861a81df5d1a14ae488c6da62764496f" dependencies = [ "rustdoc-types 0.23.0", "trustfall", @@ -3253,10 +3253,10 @@ dependencies = [ "serde", "serde_json", "trustfall", - "trustfall-rustdoc-adapter 23.6.0", - "trustfall-rustdoc-adapter 24.5.0", - "trustfall-rustdoc-adapter 26.2.0", - "trustfall-rustdoc-adapter 27.0.0", + "trustfall-rustdoc-adapter 23.6.1", + "trustfall-rustdoc-adapter 24.5.1", + "trustfall-rustdoc-adapter 26.2.1", + "trustfall-rustdoc-adapter 27.0.1", ] [[package]] diff --git a/src/lints/module_missing.ron b/src/lints/module_missing.ron new file mode 100644 index 00000000..19f44e55 --- /dev/null +++ b/src/lints/module_missing.ron @@ -0,0 +1,46 @@ +SemverQuery( + id: "module_missing", + human_readable_name: "pub module removed or renamed", + description: "A module can no longer be imported by its prior path", + required_update: Major, + reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#item-remove"), + query: r#" + { + CrateDiff { + baseline { + item { + ... on Module { + visibility_limit @filter(op: "=", value: ["$public"]) @output + name @output + + importable_path { + path @output @tag + } + + span_: span @optional { + filename @output + begin_line @output + } + } + } + } + current @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { + item { + ... on Module { + visibility_limit @filter(op: "=", value: ["$public"]) + + importable_path { + path @filter(op: "=", value: ["%path"]) + } + } + } + } + } + }"#, + arguments: { + "public": "public", + "zero": 0, + }, + error_message: "A publicly-visible module cannot be imported by its prior path. A `pub use` may have been removed, or the module may have been renamed, removed, or made non-public.", + per_result_error_template: Some("{mod {{join \"::\" path}}, previously in file {{span_filename}}:{{span_begin_line}}"), +) diff --git a/src/query.rs b/src/query.rs index b06b22ff..9bce4d61 100644 --- a/src/query.rs +++ b/src/query.rs @@ -514,4 +514,5 @@ add_lints!( pub_module_level_const_missing, pub_static_missing, trait_removed_associated_type, + module_missing, ); diff --git a/test_crates/module_missing/new/Cargo.toml b/test_crates/module_missing/new/Cargo.toml new file mode 100644 index 00000000..4a00c6b2 --- /dev/null +++ b/test_crates/module_missing/new/Cargo.toml @@ -0,0 +1,7 @@ +[package] +publish = false +name = "module_missing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/module_missing/new/src/lib.rs b/test_crates/module_missing/new/src/lib.rs new file mode 100644 index 00000000..f03cd380 --- /dev/null +++ b/test_crates/module_missing/new/src/lib.rs @@ -0,0 +1,24 @@ +// Removing this, but no warning should happen: +// it isn't public. +// +// mod a {} + +mod b { + // Removing this, but no warning should happen: + // it isn't visible. + // pub mod b {} +} + +pub mod bb { + // Removing this should cause a warning. + // + // pub mod will_remove {} +} + +// Making this private should cause a warning +pub(crate) mod will_make_private { + mod e {} +} + +// Adding a module shouldn't cause problems. +pub mod new_module {} diff --git a/test_crates/module_missing/old/Cargo.toml b/test_crates/module_missing/old/Cargo.toml new file mode 100644 index 00000000..4a00c6b2 --- /dev/null +++ b/test_crates/module_missing/old/Cargo.toml @@ -0,0 +1,7 @@ +[package] +publish = false +name = "module_missing" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/module_missing/old/src/lib.rs b/test_crates/module_missing/old/src/lib.rs new file mode 100644 index 00000000..15fb4e39 --- /dev/null +++ b/test_crates/module_missing/old/src/lib.rs @@ -0,0 +1,13 @@ +mod a {} + +mod b { + pub mod b {} +} + +pub mod bb { + pub mod will_remove {} +} + +pub mod will_make_private { + mod e {} +} diff --git a/test_outputs/module_missing.output.ron b/test_outputs/module_missing.output.ron new file mode 100644 index 00000000..a658bf04 --- /dev/null +++ b/test_outputs/module_missing.output.ron @@ -0,0 +1,49 @@ +{ + "./test_crates/module_missing/": [ + { + "name": String("will_remove"), + "path": List([ + String("module_missing"), + String("bb"), + String("will_remove"), + ]), + "span_begin_line": Uint64(8), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "name": String("will_make_private"), + "path": List([ + String("module_missing"), + String("will_make_private"), + ]), + "span_begin_line": Uint64(11), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + ], + "./test_crates/trait_missing/": [ + { + "name": String("my_pub_mod"), + "path": List([ + String("trait_missing"), + String("my_pub_mod"), + ]), + "span_begin_line": Uint64(5), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + ], + "./test_crates/trait_missing_with_major_bump/": [ + { + "name": String("my_pub_mod"), + "path": List([ + String("trait_missing_with_major_bump"), + String("my_pub_mod"), + ]), + "span_begin_line": Uint64(5), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + ], +}