Skip to content

Commit

Permalink
Add new lint macro_no_longer_exported (#1031)
Browse files Browse the repository at this point in the history
Addresses checkbox in
#946

---------

Co-authored-by: Predrag Gruevski <[email protected]>
  • Loading branch information
malenaohl and obi1kenobi authored Dec 8, 2024
1 parent 679fb16 commit 8177494
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/lints/macro_no_longer_exported.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
SemverQuery(
id: "macro_no_longer_exported",
human_readable_name: "macro is no longer exported",
description: "A macro_rules macro that was previously exported is no longer exported.",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope"),
query: r#"
{
CrateDiff {
baseline {
item {
... on Macro {
name @output @tag
public_api_eligible @filter(op: "=", value: ["$true"])
}
}
}
current {
item {
... on Macro {
name @filter(op: "=", value: ["%name"])
# Check the macro still exists but is no longer public API
# and isn't doc(hidden) (which would be caught by another lint)
public_api_eligible @filter(op: "!=", value: ["$true"])
doc_hidden @filter(op: "!=", value: ["$true"])
span_: span @optional {
filename @output
begin_line @output
}
}
}
}
}
}"#,
arguments: {
"true": true,
},
error_message: "A macro that was previously exported with #[macro_export] is no longer exported. This breaks downstream code that used the macro.",
per_result_error_template: Some("macro {{name}} in {{span_filename}}:{{span_begin_line}}"),
witness: Some((
hint_template: r#"{{name}}!(...);"#,
)),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ add_lints!(
inherent_method_must_use_added,
inherent_method_now_doc_hidden,
inherent_method_unsafe_added,
macro_no_longer_exported,
macro_now_doc_hidden,
method_parameter_count_changed,
module_missing,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/macro_no_longer_exported/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "macro_no_longer_exported"
version = "0.1.0"
edition = "2021"

[dependencies]
22 changes: 22 additions & 0 deletions test_crates/macro_no_longer_exported/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// No longer exported but not hidden - should trigger
macro_rules! example_macro {
() => {
println!("Hello from macro!");
};
}

// No longer exported but is hidden - should NOT trigger (caught by different lint)
#[doc(hidden)]
macro_rules! will_be_hidden {
() => {
println!("Will become hidden");
};
}

// Now exported - should not trigger
#[macro_export]
macro_rules! internal_macro {
() => {
println!("Internal macro");
};
}
7 changes: 7 additions & 0 deletions test_crates/macro_no_longer_exported/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "macro_no_longer_exported"
version = "0.1.0"
edition = "2021"

[dependencies]
20 changes: 20 additions & 0 deletions test_crates/macro_no_longer_exported/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[macro_export]
macro_rules! example_macro {
() => {
println!("Hello from macro!");
};
}

#[macro_export]
macro_rules! will_be_hidden {
() => {
println!("Will become hidden");
};
}

// Internal macro - should not trigger when made public
macro_rules! internal_macro {
() => {
println!("Internal macro");
};
}
28 changes: 28 additions & 0 deletions test_outputs/query_execution/macro_no_longer_exported.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: src/query.rs
expression: "&query_execution_results"
snapshot_kind: text
---
{
"./test_crates/declarative_macro_missing/": [
{
"name": String("will_no_longer_be_exported"),
"span_begin_line": Uint64(1),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/macro_no_longer_exported/": [
{
"name": String("example_macro"),
"span_begin_line": Uint64(2),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/macro_now_doc_hidden/": [
{
"name": String("becomes_non_exported"),
"span_begin_line": Uint64(36),
"span_filename": String("src/lib.rs"),
},
],
}
7 changes: 7 additions & 0 deletions test_outputs/query_execution/macro_now_doc_hidden.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ snapshot_kind: text
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/macro_no_longer_exported/": [
{
"name": String("will_be_hidden"),
"span_begin_line": Uint64(10),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/macro_now_doc_hidden/": [
{
"name": String("will_be_hidden"),
Expand Down
20 changes: 20 additions & 0 deletions test_outputs/witnesses/macro_no_longer_exported.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: src/query.rs
description: "Lint `macro_no_longer_exported` did not have the expected witness output.\nSee https://github.com/obi1kenobi/cargo-semver-checks/blob/main/CONTRIBUTING.md#testing-witnesses\nfor more information."
expression: "&actual_witnesses"
snapshot_kind: text
---
[["./test_crates/declarative_macro_missing/"]]
filename = 'src/lib.rs'
begin_line = 1
hint = 'will_no_longer_be_exported!(...);'

[["./test_crates/macro_no_longer_exported/"]]
filename = 'src/lib.rs'
begin_line = 2
hint = 'example_macro!(...);'

[["./test_crates/macro_now_doc_hidden/"]]
filename = 'src/lib.rs'
begin_line = 36
hint = 'becomes_non_exported!(...);'

0 comments on commit 8177494

Please sign in to comment.