From 5ffa31dd90db201e305c3fe0baabab3e7977f624 Mon Sep 17 00:00:00 2001 From: Malena Gruevski <45406876+malenaohl@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:26:52 -0500 Subject: [PATCH] Add lint derive_proc_macro_missing (#1027) Addresses a checkbox in https://github.com/obi1kenobi/cargo-semver-checks/issues/946 --- src/lints/derive_proc_macro_missing.ron | 41 +++++++++++++++++++ src/query.rs | 1 + .../derive_proc_macro_missing/new/Cargo.toml | 10 +++++ .../derive_proc_macro_missing/new/src/lib.rs | 16 ++++++++ .../derive_proc_macro_missing/old/Cargo.toml | 10 +++++ .../derive_proc_macro_missing/old/src/lib.rs | 21 ++++++++++ .../derive_proc_macro_missing.snap | 14 +++++++ 7 files changed, 113 insertions(+) create mode 100644 src/lints/derive_proc_macro_missing.ron create mode 100644 test_crates/derive_proc_macro_missing/new/Cargo.toml create mode 100644 test_crates/derive_proc_macro_missing/new/src/lib.rs create mode 100644 test_crates/derive_proc_macro_missing/old/Cargo.toml create mode 100644 test_crates/derive_proc_macro_missing/old/src/lib.rs create mode 100644 test_outputs/query_execution/derive_proc_macro_missing.snap diff --git a/src/lints/derive_proc_macro_missing.ron b/src/lints/derive_proc_macro_missing.ron new file mode 100644 index 00000000..b8dcbb53 --- /dev/null +++ b/src/lints/derive_proc_macro_missing.ron @@ -0,0 +1,41 @@ +SemverQuery( + id: "derive_proc_macro_missing", + human_readable_name: "derive macro removed", + description: "A derive macro that was previously available can no longer be used.", + required_update: Major, + lint_level: Deny, + reference_link: Some("https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros"), + query: r#" + { + CrateDiff { + baseline { + item { + ... on DeriveProcMacro { + macro_name: name @output @tag + visibility_limit @filter(op: "=", value: ["$public"]) + + span_: span @optional { + filename @output + begin_line @output + } + } + } + } + current @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { + item { + ... on DeriveProcMacro { + visibility_limit @filter(op: "=", value: ["$public"]) + name @filter(op: "=", value: ["%macro_name"]) + } + } + } + } + }"#, + arguments: { + "public": "public", + "zero": 0, + }, + error_message: "A derive macro has been removed. Type definitions using #[derive(...)] with this macro will fail to compile.", + per_result_error_template: Some("macro {{macro_name}} in {{span_filename}}:{{span_begin_line}}"), + witness: None, +) diff --git a/src/query.rs b/src/query.rs index 81be133f..a519896d 100644 --- a/src/query.rs +++ b/src/query.rs @@ -1054,6 +1054,7 @@ add_lints!( constructible_struct_adds_private_field, constructible_struct_changed_type, declarative_macro_missing, + derive_proc_macro_missing, derive_trait_impl_removed, enum_marked_non_exhaustive, enum_missing, diff --git a/test_crates/derive_proc_macro_missing/new/Cargo.toml b/test_crates/derive_proc_macro_missing/new/Cargo.toml new file mode 100644 index 00000000..9ede3067 --- /dev/null +++ b/test_crates/derive_proc_macro_missing/new/Cargo.toml @@ -0,0 +1,10 @@ +[package] +publish = false +name = "derive_proc_macro_missing" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] diff --git a/test_crates/derive_proc_macro_missing/new/src/lib.rs b/test_crates/derive_proc_macro_missing/new/src/lib.rs new file mode 100644 index 00000000..45ea3612 --- /dev/null +++ b/test_crates/derive_proc_macro_missing/new/src/lib.rs @@ -0,0 +1,16 @@ +use proc_macro::TokenStream; + +#[proc_macro_derive(StaysPresent)] +pub fn stays_present(_input: TokenStream) -> TokenStream { + TokenStream::new() +} + +#[proc_macro_attribute] +pub fn my_attribute(_attr: TokenStream, _item: TokenStream) -> TokenStream { + TokenStream::new() +} + +#[proc_macro] +pub fn my_macro(_input: TokenStream) -> TokenStream { + TokenStream::new() +} diff --git a/test_crates/derive_proc_macro_missing/old/Cargo.toml b/test_crates/derive_proc_macro_missing/old/Cargo.toml new file mode 100644 index 00000000..9ede3067 --- /dev/null +++ b/test_crates/derive_proc_macro_missing/old/Cargo.toml @@ -0,0 +1,10 @@ +[package] +publish = false +name = "derive_proc_macro_missing" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] diff --git a/test_crates/derive_proc_macro_missing/old/src/lib.rs b/test_crates/derive_proc_macro_missing/old/src/lib.rs new file mode 100644 index 00000000..4674ced3 --- /dev/null +++ b/test_crates/derive_proc_macro_missing/old/src/lib.rs @@ -0,0 +1,21 @@ +use proc_macro::TokenStream; + +#[proc_macro_derive(MyDerive)] +pub fn my_derive(_input: TokenStream) -> TokenStream { + TokenStream::new() +} + +#[proc_macro_derive(StaysPresent)] +pub fn stays_present(_input: TokenStream) -> TokenStream { + TokenStream::new() +} + +#[proc_macro_attribute] +pub fn my_attribute(_attr: TokenStream, _item: TokenStream) -> TokenStream { + TokenStream::new() +} + +#[proc_macro] +pub fn my_macro(_input: TokenStream) -> TokenStream { + TokenStream::new() +} diff --git a/test_outputs/query_execution/derive_proc_macro_missing.snap b/test_outputs/query_execution/derive_proc_macro_missing.snap new file mode 100644 index 00000000..388ec32b --- /dev/null +++ b/test_outputs/query_execution/derive_proc_macro_missing.snap @@ -0,0 +1,14 @@ +--- +source: src/query.rs +expression: "&query_execution_results" +snapshot_kind: text +--- +{ + "./test_crates/derive_proc_macro_missing/": [ + { + "macro_name": String("MyDerive"), + "span_begin_line": Uint64(4), + "span_filename": String("src/lib.rs"), + }, + ], +}