-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new lint macro_no_longer_exported (#1031)
Addresses checkbox in #946 --------- Co-authored-by: Predrag Gruevski <[email protected]>
- Loading branch information
1 parent
679fb16
commit 8177494
Showing
9 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}!(...);"#, | ||
)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
test_outputs/query_execution/macro_no_longer_exported.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(...);' |