Skip to content

Commit e559258

Browse files
committed
Fix #[itest] not preserving attributes
1 parent 96f2c32 commit e559258

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

godot-macros/src/bench.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use proc_macro2::TokenStream;
99
use quote::quote;
1010

11-
use crate::util::{bail, KvParser};
11+
use crate::util::{bail, retain_attributes_except, KvParser};
1212
use crate::ParseResult;
1313

1414
const DEFAULT_REPETITIONS: usize = 100;
@@ -42,7 +42,11 @@ pub fn attribute_bench(input_decl: venial::Item) -> ParseResult<TokenStream> {
4242

4343
let body = &func.body;
4444

45+
// Filter out #[bench] itself, but preserve other attributes like #[allow], #[expect], etc.
46+
let other_attributes = retain_attributes_except(&func.attributes, "bench");
47+
4548
Ok(quote! {
49+
#(#other_attributes)*
4650
pub fn #bench_name() {
4751
for _ in 0..#repetitions {
4852
let __ret: #ret = #body;

godot-macros/src/itest.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use proc_macro2::TokenStream;
99
use quote::{quote, ToTokens};
1010

11-
use crate::util::{bail, extract_typename, ident, path_ends_with, KvParser};
11+
use crate::util::{
12+
bail, extract_typename, ident, path_ends_with, retain_attributes_except, KvParser,
13+
};
1214
use crate::ParseResult;
1315

1416
pub fn attribute_itest(input_item: venial::Item) -> ParseResult<TokenStream> {
@@ -85,7 +87,11 @@ pub fn attribute_itest(input_item: venial::Item) -> ParseResult<TokenStream> {
8587
plugin_name = ident("__GODOT_ITEST");
8688
};
8789

90+
// Filter out #[itest] itself, but preserve other attributes like #[allow], #[expect], etc.
91+
let other_attributes = retain_attributes_except(&func.attributes, "itest");
92+
8893
Ok(quote! {
94+
#(#other_attributes)*
8995
pub fn #test_name(#param) #return_tokens {
9096
#body
9197
}

godot-macros/src/util/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ pub(crate) use bail;
8585
pub(crate) use error;
8686
pub(crate) use require_api_version;
8787

88+
/// Keeps all attributes except the one specified (e.g. `"itest"`).
89+
pub fn retain_attributes_except<'a>(
90+
attributes: &'a [venial::Attribute],
91+
macro_name: &'a str,
92+
) -> impl Iterator<Item = &'a venial::Attribute> {
93+
attributes.iter().filter(move |attr| {
94+
attr.get_single_path_segment()
95+
.is_none_or(|segment| segment != macro_name)
96+
})
97+
}
98+
8899
pub fn reduce_to_signature(function: &venial::Function) -> venial::Function {
89100
let mut reduced = function.clone();
90101
reduced.vis_marker = None; // retained outside in the case of #[signal].

itest/rust/src/engine_tests/codegen_test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,11 @@ trait TraitA {
190190
impl TraitA for CodegenTest3 {
191191
fn exit_tree(&mut self) {}
192192
}
193+
194+
// Verifies that attributes (here #[expect]) are preserved by #[itest] macro.
195+
// See retain_attributes_except() function.
196+
#[itest]
197+
#[expect(unused_variables)]
198+
fn test_itest_macro_attribute_retention() {
199+
let unused_var = 42; // Should not generate warning.
200+
}

0 commit comments

Comments
 (0)