From 965d62aadec125662060a70ec970c721fda5d13e Mon Sep 17 00:00:00 2001 From: Malena Gruevski <45406876+malenaohl@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:57:05 -0500 Subject: [PATCH] Add lint attribute_proc_macro_missing (#1026) Addresses a checkbox in /~https://github.com/obi1kenobi/cargo-semver-checks/issues/946 --- src/lints/attribute_proc_macro_missing.ron | 40 +++++++++++++++++++ src/query.rs | 1 + .../new/Cargo.toml | 10 +++++ .../new/src/lib.rs | 12 ++++++ .../old/Cargo.toml | 10 +++++ .../old/src/lib.rs | 18 +++++++++ .../attribute_proc_macro_missing.snap | 14 +++++++ 7 files changed, 105 insertions(+) create mode 100644 src/lints/attribute_proc_macro_missing.ron create mode 100644 test_crates/attribute_proc_macro_missing/new/Cargo.toml create mode 100644 test_crates/attribute_proc_macro_missing/new/src/lib.rs create mode 100644 test_crates/attribute_proc_macro_missing/old/Cargo.toml create mode 100644 test_crates/attribute_proc_macro_missing/old/src/lib.rs create mode 100644 test_outputs/query_execution/attribute_proc_macro_missing.snap diff --git a/src/lints/attribute_proc_macro_missing.ron b/src/lints/attribute_proc_macro_missing.ron new file mode 100644 index 00000000..d20e807b --- /dev/null +++ b/src/lints/attribute_proc_macro_missing.ron @@ -0,0 +1,40 @@ +SemverQuery( + id: "attribute_proc_macro_missing", + human_readable_name: "attribute proc macro removed", + description: "An attribute proc macro that previously existed is no longer available.", + required_update: Major, + lint_level: Deny, + reference_link: Some("https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros"), + query: r#" + { + CrateDiff { + baseline { + item { + ... on AttributeProcMacro { + visibility_limit @filter(op: "=", value: ["$public"]) + name @output @tag + span_: span @optional { + filename @output + begin_line @output + } + } + } + } + current @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { + item { + ... on AttributeProcMacro { + visibility_limit @filter(op: "=", value: ["$public"]) + name @filter(op: "=", value: ["%name"]) + } + } + } + } + }"#, + arguments: { + "public": "public", + "zero": 0, + }, + error_message: "A previously available attribute macro has been removed, breaking code that uses attribute syntax on annotated items.", + per_result_error_template: Some("macro #[{{name}}] in {{span_filename}}:{{span_begin_line}}"), + witness: None, +) diff --git a/src/query.rs b/src/query.rs index c20f5178..81be133f 100644 --- a/src/query.rs +++ b/src/query.rs @@ -1048,6 +1048,7 @@ macro_rules! add_lints { // The following add_lints! invocation is programmatically edited by scripts/make_new_lint.sh // If you must manually edit it, be sure to read the "Requirements" comments in that script first add_lints!( + attribute_proc_macro_missing, auto_trait_impl_removed, constructible_struct_adds_field, constructible_struct_adds_private_field, diff --git a/test_crates/attribute_proc_macro_missing/new/Cargo.toml b/test_crates/attribute_proc_macro_missing/new/Cargo.toml new file mode 100644 index 00000000..182bcd10 --- /dev/null +++ b/test_crates/attribute_proc_macro_missing/new/Cargo.toml @@ -0,0 +1,10 @@ +[package] +publish = false +name = "attribute_proc_macro_missing" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] diff --git a/test_crates/attribute_proc_macro_missing/new/src/lib.rs b/test_crates/attribute_proc_macro_missing/new/src/lib.rs new file mode 100644 index 00000000..8d71dc01 --- /dev/null +++ b/test_crates/attribute_proc_macro_missing/new/src/lib.rs @@ -0,0 +1,12 @@ +use proc_macro::TokenStream; + +// Keep other macro types to verify specific detection +#[proc_macro] +pub fn sql(_item: TokenStream) -> TokenStream { + TokenStream::new() +} + +#[proc_macro_derive(MyDerive)] +pub fn my_derive(_item: TokenStream) -> TokenStream { + TokenStream::new() +} diff --git a/test_crates/attribute_proc_macro_missing/old/Cargo.toml b/test_crates/attribute_proc_macro_missing/old/Cargo.toml new file mode 100644 index 00000000..182bcd10 --- /dev/null +++ b/test_crates/attribute_proc_macro_missing/old/Cargo.toml @@ -0,0 +1,10 @@ +[package] +publish = false +name = "attribute_proc_macro_missing" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] diff --git a/test_crates/attribute_proc_macro_missing/old/src/lib.rs b/test_crates/attribute_proc_macro_missing/old/src/lib.rs new file mode 100644 index 00000000..9632e9d8 --- /dev/null +++ b/test_crates/attribute_proc_macro_missing/old/src/lib.rs @@ -0,0 +1,18 @@ +use proc_macro::TokenStream; + +#[proc_macro_attribute] +pub fn logging(_attr: TokenStream, item: TokenStream) -> TokenStream { + item +} + +// Keep this to verify we don't trigger on function-like macros +#[proc_macro] +pub fn sql(_item: TokenStream) -> TokenStream { + TokenStream::new() +} + +// Keep this to verify we don't trigger on derive macros +#[proc_macro_derive(MyDerive)] +pub fn my_derive(_item: TokenStream) -> TokenStream { + TokenStream::new() +} diff --git a/test_outputs/query_execution/attribute_proc_macro_missing.snap b/test_outputs/query_execution/attribute_proc_macro_missing.snap new file mode 100644 index 00000000..57ed9eb8 --- /dev/null +++ b/test_outputs/query_execution/attribute_proc_macro_missing.snap @@ -0,0 +1,14 @@ +--- +source: src/query.rs +expression: "&query_execution_results" +snapshot_kind: text +--- +{ + "./test_crates/attribute_proc_macro_missing/": [ + { + "name": String("logging"), + "span_begin_line": Uint64(4), + "span_filename": String("src/lib.rs"), + }, + ], +}