Skip to content

Commit be0f750

Browse files
committed
micro opt: stop emitting __internal_config_statement from test attr
1 parent ff9fda4 commit be0f750

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

crates/snforge-scarb-plugin/src/attributes/test.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{AttributeInfo, ErrorExt, internal_config_statement::InternalConfigSt
22
use crate::asserts::assert_is_used_once;
33
use crate::common::{has_fuzzer_attribute, has_test_case_attribute};
44
use crate::external_inputs::ExternalInput;
5-
use crate::utils::create_single_token;
5+
use crate::utils::{create_single_token, process_statements};
66
use crate::{
77
args::Arguments,
88
common::{into_proc_macro_result, with_parsed_values},
@@ -81,9 +81,6 @@ fn test_internal(
8181
let signature = SyntaxNodeWithDb::new(&signature, db);
8282
let signature = quote! { #signature };
8383

84-
let body = func.body(db).as_syntax_node();
85-
let body = SyntaxNodeWithDb::new(&body, db);
86-
8784
let attributes = func.attributes(db).as_syntax_node();
8885
let attributes = SyntaxNodeWithDb::new(&attributes, db);
8986

@@ -98,13 +95,27 @@ fn test_internal(
9895

9996
let test_func_with_attrs = test_func_with_attrs(&test_func, &called_func, &call_args);
10097

98+
let statements = func
99+
.body(db)
100+
.statements(db)
101+
.elements(db)
102+
.collect::<Vec<_>>();
103+
let (statements, if_content) = process_statements(db, &statements);
104+
101105
Ok(quote!(
102106
#test_func_with_attrs
103107

104108
#attributes
105-
#[#internal_config]
106109
fn #func_ident #signature
107-
#body
110+
{
111+
if snforge_std::_internals::is_config_run() {
112+
#if_content
113+
114+
return;
115+
}
116+
117+
#statements
118+
}
108119
))
109120
} else {
110121
Ok(quote!(

crates/snforge-scarb-plugin/src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ pub fn get_statements(
8484
.statements(db)
8585
.elements(db)
8686
.collect::<Vec<_>>();
87+
process_statements(db, &statements)
88+
}
8789

90+
pub fn process_statements(
91+
db: &SimpleParserDatabase,
92+
statements: &[Statement],
93+
) -> (TokenStream, TokenStream) {
8894
let if_content = statements.first().and_then(|stmt| {
8995
// first statement is `if`
9096
let Statement::Expr(expr) = stmt else {
@@ -133,7 +139,7 @@ pub fn get_statements(
133139
let statements = if if_content.is_some() {
134140
&statements[1..]
135141
} else {
136-
&statements[..]
142+
statements
137143
}
138144
.iter()
139145
.map(|stmt| stmt.to_token_stream(db))

0 commit comments

Comments
 (0)