Skip to content

Commit 1b11177

Browse files
authored
Changed QueryAttrs to be more iterator based. (#7889)
1 parent 8f197ae commit 1b11177

File tree

7 files changed

+172
-173
lines changed

7 files changed

+172
-173
lines changed

crates/cairo-lang-defs/src/db.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -846,30 +846,28 @@ fn collect_extra_allowed_attributes(
846846
plugin_diagnostics: &mut Vec<PluginDiagnostic>,
847847
) -> OrderedHashSet<String> {
848848
let mut extra_allowed_attributes = OrderedHashSet::default();
849-
for attr in item.attributes_elements(db) {
850-
if attr.attr(db).as_syntax_node().get_text_without_trivia(db) == ALLOW_ATTR_ATTR {
851-
let args = attr.clone().structurize(db).args;
852-
if args.is_empty() {
853-
plugin_diagnostics.push(PluginDiagnostic::error(
854-
attr.stable_ptr(db),
855-
"Expected arguments.".to_string(),
856-
));
857-
continue;
858-
}
859-
for arg in args {
860-
if let Some(ast::Expr::Path(path)) = try_extract_unnamed_arg(db, &arg.arg) {
861-
if let Some([ast::PathSegment::Simple(segment)]) =
862-
path.segments(db).elements(db).collect_array()
863-
{
864-
extra_allowed_attributes.insert(segment.ident(db).text(db).into());
865-
continue;
866-
}
849+
for attr in item.query_attr(db, ALLOW_ATTR_ATTR) {
850+
let args = attr.clone().structurize(db).args;
851+
if args.is_empty() {
852+
plugin_diagnostics.push(PluginDiagnostic::error(
853+
attr.stable_ptr(db),
854+
"Expected arguments.".to_string(),
855+
));
856+
continue;
857+
}
858+
for arg in args {
859+
if let Some(ast::Expr::Path(path)) = try_extract_unnamed_arg(db, &arg.arg) {
860+
if let Some([ast::PathSegment::Simple(segment)]) =
861+
path.segments(db).elements(db).collect_array()
862+
{
863+
extra_allowed_attributes.insert(segment.ident(db).text(db).into());
864+
continue;
867865
}
868-
plugin_diagnostics.push(PluginDiagnostic::error(
869-
arg.arg.stable_ptr(db),
870-
"Expected simple identifier.".to_string(),
871-
));
872866
}
867+
plugin_diagnostics.push(PluginDiagnostic::error(
868+
arg.arg.stable_ptr(db),
869+
"Expected simple identifier.".to_string(),
870+
));
873871
}
874872
}
875873
extra_allowed_attributes

crates/cairo-lang-plugins/src/plugins/external_attributes_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn get_diagnostics<Item: QueryAttrs>(
4040
item: &Item,
4141
) -> Option<Vec<PluginDiagnostic>> {
4242
let mut diagnostics: Vec<PluginDiagnostic> = Vec::new();
43-
item.query_attr(db, DOC_ATTR).into_iter().for_each(|attr| {
43+
item.query_attr(db, DOC_ATTR).for_each(|attr| {
4444
let args = attr.clone().structurize(db).args;
4545
if args.is_empty() {
4646
diagnostics.push(PluginDiagnostic::error(

crates/cairo-lang-plugins/src/plugins/panicable.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ fn generate_panicable_code(
5555
visibility: ast::Visibility,
5656
) -> PluginResult {
5757
let mut attrs = attributes.query_attr(db, PANIC_WITH_ATTR);
58-
if attrs.is_empty() {
58+
let Some(attr) = attrs.next() else {
59+
// No `#[panic_with]` attribute found.
5960
return PluginResult::default();
60-
}
61+
};
6162
let mut diagnostics = vec![];
62-
if attrs.len() > 1 {
63-
let extra_attr = attrs.swap_remove(1);
63+
if let Some(extra_attr) = attrs.next() {
6464
diagnostics.push(PluginDiagnostic::error(
6565
extra_attr.stable_ptr(db),
6666
"`#[panic_with]` cannot be applied multiple times to the same item.".into(),
@@ -79,7 +79,6 @@ fn generate_panicable_code(
7979
return PluginResult { code: None, diagnostics, remove_original_item: false };
8080
};
8181

82-
let attr = attrs.swap_remove(0);
8382
let mut builder = PatchBuilder::new(db, &attr);
8483
let attr = attr.structurize(db);
8584

crates/cairo-lang-semantic/src/items/feature_kind.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use cairo_lang_syntax::node::helpers::QueryAttrs;
1313
use cairo_lang_syntax::node::{Terminal, TypedStablePtr, TypedSyntaxNode, ast};
1414
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
1515
use cairo_lang_utils::try_extract_matches;
16+
use itertools::Itertools;
1617
use smol_str::SmolStr;
1718

1819
use crate::SemanticDiagnostic;
@@ -38,9 +39,9 @@ impl FeatureKind {
3839
diagnostics: &mut DiagnosticsBuilder<SemanticDiagnostic>,
3940
attrs: &ast::AttributeList,
4041
) -> Self {
41-
let unstable_attrs = attrs.query_attr(db, UNSTABLE_ATTR);
42-
let deprecated_attrs = attrs.query_attr(db, DEPRECATED_ATTR);
43-
let internal_attrs = attrs.query_attr(db, INTERNAL_ATTR);
42+
let unstable_attrs = attrs.query_attr(db, UNSTABLE_ATTR).collect_vec();
43+
let deprecated_attrs = attrs.query_attr(db, DEPRECATED_ATTR).collect_vec();
44+
let internal_attrs = attrs.query_attr(db, INTERNAL_ATTR).collect_vec();
4445
if unstable_attrs.is_empty() && deprecated_attrs.is_empty() && internal_attrs.is_empty() {
4546
return Self::Stable;
4647
};

crates/cairo-lang-starknet/src/plugin/starknet_module/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fn handle_contract_item(
284284
}
285285
}
286286
ast::ModuleItem::ImplAlias(alias_ast) => {
287-
let abi_attrs = alias_ast.query_attr(db, ABI_ATTR);
287+
let abi_attrs = alias_ast.query_attr(db, ABI_ATTR).collect_vec();
288288
if abi_attrs.is_empty() {
289289
return;
290290
}

crates/cairo-lang-starknet/src/plugin/storage_interfaces.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use cairo_lang_syntax::node::db::SyntaxGroup;
1313
use cairo_lang_syntax::node::helpers::QueryAttrs;
1414
use cairo_lang_syntax::node::{TypedSyntaxNode, ast};
1515
use indoc::formatdoc;
16-
use itertools::zip_eq;
16+
use itertools::{Itertools, zip_eq};
1717

1818
use super::starknet_module::backwards_compatible_storage;
1919
use super::utils::{has_derive, validate_v0};
@@ -67,7 +67,7 @@ impl MacroPlugin for StorageInterfacesPlugin {
6767
metadata: &MacroPluginMetadata<'_>,
6868
) -> PluginResult {
6969
let mut diagnostics = vec![];
70-
let storage_node_attrs = item_ast.query_attr(db, STORAGE_NODE_ATTR);
70+
let storage_node_attrs = item_ast.query_attr(db, STORAGE_NODE_ATTR).collect_vec();
7171
if !matches!(item_ast, ast::ModuleItem::Struct(_)) && !storage_node_attrs.is_empty() {
7272
for attr in &storage_node_attrs {
7373
diagnostics.push(PluginDiagnostic::error(
@@ -76,7 +76,7 @@ impl MacroPlugin for StorageInterfacesPlugin {
7676
));
7777
}
7878
}
79-
let sub_pointers_attrs = item_ast.query_attr(db, STORAGE_SUB_POINTERS_ATTR);
79+
let sub_pointers_attrs = item_ast.query_attr(db, STORAGE_SUB_POINTERS_ATTR).collect_vec();
8080
if !matches!(item_ast, ast::ModuleItem::Enum(_)) && !sub_pointers_attrs.is_empty() {
8181
for attr in &sub_pointers_attrs {
8282
diagnostics.push(PluginDiagnostic::error(

0 commit comments

Comments
 (0)