dart-decimate 0.0.45 → 0.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Cargo.lock CHANGED
@@ -102,7 +102,7 @@ checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
102
102
 
103
103
  [[package]]
104
104
  name = "dart-decimate"
105
- version = "0.0.45"
105
+ version = "0.0.47"
106
106
  dependencies = [
107
107
  "clap",
108
108
  "glob",
package/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "dart-decimate"
3
- version = "0.0.45"
3
+ version = "0.0.47"
4
4
  edition = "2024"
5
5
  rust-version = "1.90"
6
6
  description = "Rust-native codebase intelligence for Dart and Flutter module graphs."
package/README.md CHANGED
@@ -90,7 +90,7 @@ You do not need to install anything permanently. Use the command above.
90
90
  For repeatable automation, pin the version:
91
91
 
92
92
  ```bash
93
- npx --yes dart-decimate@0.0.45
93
+ npx --yes dart-decimate@0.0.47
94
94
  ```
95
95
 
96
96
  Add this to `package.json` if you want a short project command:
@@ -101,7 +101,7 @@ Add this to `package.json` if you want a short project command:
101
101
  "dart-decimate": "dart-decimate json ."
102
102
  },
103
103
  "devDependencies": {
104
- "dart-decimate": "0.0.45"
104
+ "dart-decimate": "0.0.47"
105
105
  }
106
106
  }
107
107
  ```
@@ -115,7 +115,7 @@ npm run dart-decimate
115
115
  If you prefer Cargo, install the matching tagged source:
116
116
 
117
117
  ```bash
118
- cargo install --git https://github.com/sgaabdu4/dart-decimate.git --tag v0.0.45 --locked
118
+ cargo install --git https://github.com/sgaabdu4/dart-decimate.git --tag v0.0.47 --locked
119
119
  ```
120
120
 
121
121
  The npm release `X.Y.Z` and Cargo tag `vX.Y.Z` are verified against each other
@@ -126,7 +126,7 @@ Confirm a Cargo build's version with:
126
126
 
127
127
  ```bash
128
128
  dart-decimate --version
129
- # dart-decimate 0.0.45
129
+ # dart-decimate 0.0.47
130
130
  ```
131
131
 
132
132
  ## npx
@@ -536,7 +536,7 @@ Example shape:
536
536
  {
537
537
  "schema_version": "dart-decimate.report.v1",
538
538
  "kind": "combined",
539
- "tool": "dart-decimate 0.0.45",
539
+ "tool": "dart-decimate 0.0.47",
540
540
  "command": "check",
541
541
  "verdict": "fail",
542
542
  "summary": {
@@ -552,7 +552,7 @@ Example shape:
552
552
 
553
553
  The `dart-decimate.report.v1` field layout is unchanged. Its existing `tool`
554
554
  string includes the producing version so saved reports identify the exact
555
- build, for example `dart-decimate 0.0.45`.
555
+ build, for example `dart-decimate 0.0.47`.
556
556
 
557
557
  When grouped security findings hide additional occurrences, `next_steps` can
558
558
  include `review-security-surface`, which reruns
@@ -640,7 +640,7 @@ you need the installed binary's exact list.
640
640
  For a complete repository check, including discovered local packages:
641
641
 
642
642
  ```bash
643
- npx --yes dart-decimate@0.0.45 --strict
643
+ npx --yes dart-decimate@0.0.47 --strict
644
644
  ```
645
645
 
646
646
  This runs every enabled check from the repository root. The default duplication
@@ -683,7 +683,7 @@ This repository forbids `unsafe_code`.
683
683
 
684
684
  ## Release Flow
685
685
 
686
- Current source version: `0.0.45`.
686
+ Current source version: `0.0.47`.
687
687
 
688
688
  After the first public release, changes should go through pull requests. Every
689
689
  PR to `main` must bump both `Cargo.toml` and `package.json` above the base
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dart-decimate",
3
- "version": "0.0.45",
3
+ "version": "0.0.47",
4
4
  "description": "Rust-native codebase intelligence for Dart and Flutter module graphs.",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -13,6 +13,7 @@ use super::{
13
13
  registry_api::{VisibleNonRouteRegistryApi, visible_non_route_registry_api},
14
14
  route_extension_navigation_call, route_location_argument, route_location_expression_member,
15
15
  typed_route_navigation_call, visit_named,
16
+ wrappers::helper_has_typed_route_wrapper_call,
16
17
  };
17
18
  use crate::{
18
19
  DartFile, DependencyCycle, DependencyKind, Location, ResolvedDependency, scan::ScannedProject,
@@ -173,6 +174,14 @@ fn is_typed_go_router_navigation_helper(
173
174
  return false;
174
175
  }
175
176
  helper_has_typed_route_navigation_call(root, source, &route_classes)
177
+ || helper_has_typed_route_wrapper_call(
178
+ helper_file,
179
+ root,
180
+ source,
181
+ &route_classes,
182
+ files_by_path,
183
+ dependencies,
184
+ )
176
185
  }
177
186
 
178
187
  fn helper_references_imported_api(
@@ -224,7 +224,7 @@ fn is_route_registry_infrastructure_declaration(name: &str) -> bool {
224
224
  )
225
225
  }
226
226
 
227
- fn dependency_imports_name(dependency: &ResolvedDependency, name: &str) -> bool {
227
+ pub(super) fn dependency_imports_name(dependency: &ResolvedDependency, name: &str) -> bool {
228
228
  let mut show_seen = false;
229
229
  let mut shown = false;
230
230
  for combinator in &dependency.visibility.combinators {
@@ -0,0 +1,238 @@
1
+ use std::collections::{BTreeMap, BTreeSet};
2
+ use std::fs;
3
+ use std::path::PathBuf;
4
+
5
+ use tree_sitter::Node;
6
+
7
+ use super::{
8
+ argument_list,
9
+ navigation::{go_router_symbol_import, term_identifier_shadowed_at},
10
+ registry_api::dependency_imports_name,
11
+ route_constructor_receiver, visit_named,
12
+ };
13
+ use crate::{DartFile, DependencyKind, ResolvedDependency};
14
+
15
+ pub(super) fn helper_has_typed_route_wrapper_call(
16
+ helper_file: &DartFile,
17
+ root: Node<'_>,
18
+ source: &str,
19
+ route_classes: &BTreeSet<String>,
20
+ files_by_path: &BTreeMap<PathBuf, &DartFile>,
21
+ dependencies: &[ResolvedDependency],
22
+ ) -> bool {
23
+ let mut found = false;
24
+ let mut wrapper_results = BTreeMap::new();
25
+ visit_named(root, &mut |call| {
26
+ if found || call.kind() != "call_expression" {
27
+ return;
28
+ }
29
+ let Some(function) = call.child_by_field_name("function") else {
30
+ return;
31
+ };
32
+ let Some((function, prefix)) = wrapper_call_name_nodes(function) else {
33
+ return;
34
+ };
35
+ let Ok(function_name) = function.utf8_text(source.as_bytes()) else {
36
+ return;
37
+ };
38
+ let prefix_name = prefix.and_then(|node| node.utf8_text(source.as_bytes()).ok());
39
+ if prefix.is_some() && prefix_name.is_none() {
40
+ return;
41
+ }
42
+ if (prefix.is_none()
43
+ && helper_file
44
+ .declarations
45
+ .iter()
46
+ .any(|declaration| declaration.name == function_name))
47
+ || prefix.is_some_and(|node| {
48
+ term_identifier_shadowed_at(root, node, prefix_name.unwrap_or_default(), source)
49
+ })
50
+ || (prefix.is_none()
51
+ && term_identifier_shadowed_at(root, function, function_name, source))
52
+ {
53
+ return;
54
+ }
55
+ let Some(arguments) = argument_list(call) else {
56
+ return;
57
+ };
58
+ let mut cursor = arguments.walk();
59
+ for argument in arguments.named_children(&mut cursor) {
60
+ let Some((parameter_name, value)) = named_argument(argument, source) else {
61
+ continue;
62
+ };
63
+ if !route_constructor_receiver(root, value, route_classes, source) {
64
+ continue;
65
+ }
66
+ found = dependencies.iter().any(|import| {
67
+ import.kind == DependencyKind::Import
68
+ && import.from_path == helper_file.path
69
+ && import.visibility.prefix.as_deref() == prefix_name
70
+ && dependency_imports_name(import, function_name)
71
+ && files_by_path
72
+ .get(&import.to_path)
73
+ .is_some_and(|wrapper_file| {
74
+ *wrapper_results
75
+ .entry((
76
+ import.to_path.clone(),
77
+ function_name.to_owned(),
78
+ parameter_name.to_owned(),
79
+ ))
80
+ .or_insert_with(|| {
81
+ wrapper_navigates_parameter(
82
+ wrapper_file,
83
+ function_name,
84
+ parameter_name,
85
+ )
86
+ })
87
+ })
88
+ });
89
+ if found {
90
+ break;
91
+ }
92
+ }
93
+ });
94
+ found
95
+ }
96
+
97
+ fn wrapper_call_name_nodes(function: Node<'_>) -> Option<(Node<'_>, Option<Node<'_>>)> {
98
+ if function.kind() == "identifier" {
99
+ return Some((function, None));
100
+ }
101
+ if function.kind() == "member_expression" {
102
+ let receiver = function.child_by_field_name("object")?;
103
+ let receiver = callback_body_identifier(receiver)
104
+ .or_else(|| (receiver.kind() == "identifier").then_some(receiver))?;
105
+ let method = function.child_by_field_name("property")?;
106
+ return (method.kind() == "identifier").then_some((method, Some(receiver)));
107
+ }
108
+ // Dart Tree-Sitter represents `() => navigate(...)` as a call whose
109
+ // function is the closure and whose body is the callee identifier.
110
+ callback_body_identifier(function).map(|name| (name, None))
111
+ }
112
+
113
+ fn callback_body_identifier(function: Node<'_>) -> Option<Node<'_>> {
114
+ if function.kind() != "function_expression" {
115
+ return None;
116
+ }
117
+ let parameters = function.child_by_field_name("parameters")?;
118
+ if parameters.named_child_count() != 0 {
119
+ return None;
120
+ }
121
+ let body = function.child_by_field_name("body")?;
122
+ if body.kind() != "function_expression_body" || body.named_child_count() != 1 {
123
+ return None;
124
+ }
125
+ let name = body.named_child(0)?;
126
+ (name.kind() == "identifier").then_some(name)
127
+ }
128
+
129
+ fn named_argument<'tree, 'source>(
130
+ argument: Node<'tree>,
131
+ source: &'source str,
132
+ ) -> Option<(&'source str, Node<'tree>)> {
133
+ if argument.kind() != "named_argument" {
134
+ return None;
135
+ }
136
+ let mut cursor = argument.walk();
137
+ let mut children = argument.named_children(&mut cursor);
138
+ let label = children.next()?;
139
+ if label.kind() != "label" {
140
+ return None;
141
+ }
142
+ let name = label.named_child(0)?.utf8_text(source.as_bytes()).ok()?;
143
+ Some((name, children.next()?))
144
+ }
145
+
146
+ fn wrapper_navigates_parameter(file: &DartFile, function_name: &str, parameter_name: &str) -> bool {
147
+ let Ok(source) = fs::read_to_string(&file.path) else {
148
+ return false;
149
+ };
150
+ let Ok(parsed) = crate::dart_parser::parse_dart_source_lossy(&file.path, &source) else {
151
+ return false;
152
+ };
153
+ let root = parsed.tree().root_node();
154
+ let source = parsed.source();
155
+ if !go_router_symbol_import(root, None, "GoRouteData", source) {
156
+ return false;
157
+ }
158
+ let mut cursor = root.walk();
159
+ root.named_children(&mut cursor).any(|function| {
160
+ if function.kind() != "function_declaration" {
161
+ return false;
162
+ }
163
+ let Some(signature) = function.child_by_field_name("signature") else {
164
+ return false;
165
+ };
166
+ if field_text(signature, "name", source) != Some(function_name) {
167
+ return false;
168
+ }
169
+ let Some(parameters) = signature.child_by_field_name("parameters") else {
170
+ return false;
171
+ };
172
+ if !typed_parameter(parameters, parameter_name, "GoRouteData", source) {
173
+ return false;
174
+ }
175
+ let Some(body) = function.child_by_field_name("body") else {
176
+ return false;
177
+ };
178
+ let mut navigates = false;
179
+ visit_named(body, &mut |call| {
180
+ if navigates || call.kind() != "call_expression" {
181
+ return;
182
+ }
183
+ let Some(member) = call.child_by_field_name("function") else {
184
+ return;
185
+ };
186
+ if member.kind() != "member_expression"
187
+ || field_text(member, "object", source) != Some(parameter_name)
188
+ || !matches!(
189
+ field_text(member, "property", source),
190
+ Some("go" | "push" | "pushReplacement" | "replace")
191
+ )
192
+ {
193
+ return;
194
+ }
195
+ let Some(destination) = member.child_by_field_name("object") else {
196
+ return;
197
+ };
198
+ if term_identifier_shadowed_at(body, destination, parameter_name, source) {
199
+ return;
200
+ }
201
+ let Some(arguments) = argument_list(call) else {
202
+ return;
203
+ };
204
+ let Some(context) = arguments.named_child(0) else {
205
+ return;
206
+ };
207
+ let Ok(context_name) = context.utf8_text(source.as_bytes()) else {
208
+ return;
209
+ };
210
+ navigates = context.kind() == "identifier"
211
+ && !term_identifier_shadowed_at(body, context, context_name, source)
212
+ && typed_parameter(parameters, context_name, "BuildContext", source);
213
+ });
214
+ navigates
215
+ })
216
+ }
217
+
218
+ fn typed_parameter(parameters: Node<'_>, name: &str, type_name: &str, source: &str) -> bool {
219
+ let mut found = false;
220
+ visit_named(parameters, &mut |parameter| {
221
+ if found || parameter.kind() != "formal_parameter" {
222
+ return;
223
+ }
224
+ let mut cursor = parameter.walk();
225
+ found = field_text(parameter, "name", source) == Some(name)
226
+ && parameter
227
+ .named_children(&mut cursor)
228
+ .find(|child| child.kind() == "type")
229
+ .is_some_and(|kind| kind.utf8_text(source.as_bytes()).ok() == Some(type_name));
230
+ });
231
+ found
232
+ }
233
+
234
+ fn field_text<'source>(node: Node<'_>, field: &str, source: &'source str) -> Option<&'source str> {
235
+ node.child_by_field_name(field)?
236
+ .utf8_text(source.as_bytes())
237
+ .ok()
238
+ }
@@ -11,6 +11,7 @@ mod registry_api;
11
11
  mod state_context;
12
12
  #[cfg(test)]
13
13
  mod tests;
14
+ mod wrappers;
14
15
 
15
16
  use aliases::{route_alias_receiver_node, route_alias_receiver_text, route_aliases_at};
16
17
  pub(super) use classification::{
@@ -103,7 +103,6 @@ const PATTERN_ANCESTORS: &[&str] = &[
103
103
  "null_assert_pattern",
104
104
  "null_check_pattern",
105
105
  "object_pattern",
106
- "pattern_variable_declaration",
107
106
  "record_pattern",
108
107
  "rest_pattern",
109
108
  "variable_pattern",
@@ -135,6 +134,7 @@ fn is_widget_member_access(body: Node<'_>, node: Node<'_>, name: &str, source: &
135
134
  let Some(object) = node.child_by_field_name("object") else {
136
135
  return false;
137
136
  };
137
+ let object = state_member_receiver(object).unwrap_or(object);
138
138
  let Some(object_name @ ("widget" | "oldWidget")) = object.utf8_text(source.as_bytes()).ok()
139
139
  else {
140
140
  return false;
@@ -142,6 +142,20 @@ fn is_widget_member_access(body: Node<'_>, node: Node<'_>, name: &str, source: &
142
142
  state_root_receiver_available(body, object, object_name, source)
143
143
  }
144
144
 
145
+ fn state_member_receiver(object: Node<'_>) -> Option<Node<'_>> {
146
+ if object.kind() != "function_expression" {
147
+ return None;
148
+ }
149
+ // The Dart grammar attaches `().onSubmit` to the closure instead of its
150
+ // expression body for `() => widget.onSubmit?.call()`.
151
+ let body = object.child_by_field_name("body")?;
152
+ if body.kind() != "function_expression_body" || body.named_child_count() != 1 {
153
+ return None;
154
+ }
155
+ let receiver = body.named_child(0)?;
156
+ matches!(receiver.kind(), "identifier" | "identifier_dollar_escaped").then_some(receiver)
157
+ }
158
+
145
159
  fn state_root_receiver_available(
146
160
  boundary: Node<'_>,
147
161
  node: Node<'_>,
@@ -408,6 +422,11 @@ pub(super) fn scoped_header_binding_exists(
408
422
  ) {
409
423
  return false;
410
424
  }
425
+ if for_in_iterable(scope, path_child) {
426
+ // The iterable is evaluated in the enclosing scope, so even
427
+ // `for (final widget in widget.items)` reads the outer `widget`.
428
+ return false;
429
+ }
411
430
  if header_field_binding_exists(scope, path_child, usage_start, name, source) {
412
431
  return true;
413
432
  }
@@ -418,7 +437,7 @@ pub(super) fn scoped_header_binding_exists(
418
437
  }
419
438
  if header_binding_child(scope.kind(), child)
420
439
  && child.end_byte() <= usage_start
421
- && node_contains_binding_name(child, name, source)
440
+ && header_child_binds_name(child, name, source)
422
441
  {
423
442
  return true;
424
443
  }
@@ -426,6 +445,22 @@ pub(super) fn scoped_header_binding_exists(
426
445
  false
427
446
  }
428
447
 
448
+ fn for_in_iterable(scope: Node<'_>, path_child: Node<'_>) -> bool {
449
+ matches!(scope.kind(), "for_element" | "for_statement")
450
+ && scope
451
+ .child_by_field_name("value")
452
+ .is_some_and(|value| same_node(value, path_child))
453
+ }
454
+
455
+ fn header_child_binds_name(child: Node<'_>, name: &str, source: &str) -> bool {
456
+ // A classic `for` initializer binds only its declared names; its
457
+ // initializer expressions are reads.
458
+ if child.kind() == "local_variable_declaration" {
459
+ return declaration_binds_name(child, name, source);
460
+ }
461
+ node_contains_binding_name(child, name, source)
462
+ }
463
+
429
464
  fn catch_header_binding_exists(
430
465
  scope: Node<'_>,
431
466
  path_child: Node<'_>,
@@ -523,10 +558,13 @@ fn pattern_or_local_binding_owner(node: Node<'_>) -> bool {
523
558
  }
524
559
 
525
560
  fn header_binding_child(scope_kind: &str, child: Node<'_>) -> bool {
526
- if matches!(scope_kind, "for_element" | "for_statement") {
527
- return !is_body_statement_child(child.kind())
528
- || child.kind() == "local_variable_declaration";
561
+ if matches!(scope_kind, "for_element" | "for_statement")
562
+ && child.kind() == "local_variable_declaration"
563
+ {
564
+ return true;
529
565
  }
566
+ // Loop iterables, conditions, updates, and element types are expressions,
567
+ // not bindings; only pattern-bearing header children can bind names.
530
568
  !is_body_statement_child(child.kind()) && node_can_contain_pattern_binding(child)
531
569
  }
532
570
 
@@ -308,6 +308,36 @@ class _UsedViaStateState extends State<UsedViaState> {
308
308
  Ok(())
309
309
  }
310
310
 
311
+ #[test]
312
+ fn record_rhs_reads_and_state_callback_reads_count_widget_fields()
313
+ -> Result<(), Box<dyn std::error::Error>> {
314
+ let source = r"
315
+ class RecordCard extends StatelessWidget {
316
+ RecordCard({required this.data, required this.unused});
317
+ final String data;
318
+ final String unused;
319
+ Widget build(BuildContext context) {
320
+ final (:value) = (value: data);
321
+ return Text(value);
322
+ }
323
+ }
324
+ class CallbackCard extends StatefulWidget {
325
+ CallbackCard({this.onSubmit, this.unused});
326
+ final void Function()? onSubmit;
327
+ final String? unused;
328
+ State<CallbackCard> createState() => _CallbackCardState();
329
+ }
330
+ class _CallbackCardState extends State<CallbackCard> {
331
+ Widget build(BuildContext context) => Tile(
332
+ onPressed: () => widget.onSubmit?.call(),
333
+ );
334
+ }
335
+ ";
336
+ let targets = unused_param_targets(parse_findings(source)?.unused_params);
337
+ assert_eq!(targets, vec!["RecordCard.unused", "CallbackCard.unused"]);
338
+ Ok(())
339
+ }
340
+
311
341
  #[test]
312
342
  fn state_field_reads_ignore_widget_root_shadows() -> Result<(), Box<dyn std::error::Error>> {
313
343
  let targets =
@@ -2233,3 +2263,215 @@ fn unused_param_targets(unused: Vec<UnusedWidgetParam>) -> Vec<String> {
2233
2263
  .map(|param| format!("{}.{}", param.widget_class, param.param_name))
2234
2264
  .collect()
2235
2265
  }
2266
+
2267
+ const FOR_IN_ITERABLE_WIDGET_READS_SOURCE: &str = r"
2268
+ class CatalogSection extends StatefulWidget {
2269
+ CatalogSection({required this.items, this.selectedIds = const {}, this.contextId, this.unused});
2270
+ final List<Item> items;
2271
+ final Set<String> selectedIds;
2272
+ final String? contextId;
2273
+ final String? unused;
2274
+ State<CatalogSection> createState() => _CatalogSectionState();
2275
+ }
2276
+ class _CatalogSectionState extends State<CatalogSection> {
2277
+ Widget build(BuildContext context) => Column(children: [
2278
+ for (final item in widget.items)
2279
+ Tile(
2280
+ enabled: !widget.selectedIds.contains(item.id),
2281
+ contextId: widget.contextId,
2282
+ ),
2283
+ ]);
2284
+ }
2285
+ class StatementLoopSection extends StatefulWidget {
2286
+ const StatementLoopSection({super.key, required this.items, required this.label, this.unused});
2287
+ final List<String> items;
2288
+ final String label;
2289
+ final String? unused;
2290
+ State<StatementLoopSection> createState() => _StatementLoopSectionState();
2291
+ }
2292
+ class _StatementLoopSectionState extends State<StatementLoopSection> {
2293
+ Widget build(BuildContext context) {
2294
+ for (final item in widget.items) {
2295
+ Text('$item ${widget.label}');
2296
+ }
2297
+ return const SizedBox();
2298
+ }
2299
+ }
2300
+ class PatternLoopSection extends StatefulWidget {
2301
+ const PatternLoopSection({super.key, required this.pairs, required this.label, this.unused});
2302
+ final List<(String, String)> pairs;
2303
+ final String label;
2304
+ final String? unused;
2305
+ State<PatternLoopSection> createState() => _PatternLoopSectionState();
2306
+ }
2307
+ class _PatternLoopSectionState extends State<PatternLoopSection> {
2308
+ Widget build(BuildContext context) => Column(children: [
2309
+ for (final (left, right) in widget.pairs) Text('$left $right ${widget.label}'),
2310
+ ]);
2311
+ }
2312
+ class IndexedLoopSection extends StatefulWidget {
2313
+ const IndexedLoopSection({super.key, required this.items, required this.label, this.unused});
2314
+ final List<String> items;
2315
+ final String label;
2316
+ final String? unused;
2317
+ State<IndexedLoopSection> createState() => _IndexedLoopSectionState();
2318
+ }
2319
+ class _IndexedLoopSectionState extends State<IndexedLoopSection> {
2320
+ Widget build(BuildContext context) => Column(children: [
2321
+ for (var i = 0; i < widget.items.length; i++) Text(widget.label),
2322
+ ]);
2323
+ }
2324
+ class TypedLoopSection extends StatefulWidget {
2325
+ const TypedLoopSection({super.key, required this.items, required this.label, this.unused});
2326
+ final List<String> items;
2327
+ final String label;
2328
+ final String? unused;
2329
+ State<TypedLoopSection> createState() => _TypedLoopSectionState();
2330
+ }
2331
+ class _TypedLoopSectionState extends State<TypedLoopSection> {
2332
+ Widget build(BuildContext context) {
2333
+ for (String item in widget.items) {
2334
+ Text('$item ${widget.label}');
2335
+ }
2336
+ return const SizedBox();
2337
+ }
2338
+ }
2339
+ class AwaitLoopSection extends StatefulWidget {
2340
+ const AwaitLoopSection({super.key, required this.stream, required this.label, this.unused});
2341
+ final Stream<String> stream;
2342
+ final String label;
2343
+ final String? unused;
2344
+ State<AwaitLoopSection> createState() => _AwaitLoopSectionState();
2345
+ }
2346
+ class _AwaitLoopSectionState extends State<AwaitLoopSection> {
2347
+ Future<void> listen() async {
2348
+ await for (final event in widget.stream) {
2349
+ Text('$event ${widget.label}');
2350
+ }
2351
+ }
2352
+ Widget build(BuildContext context) => const SizedBox();
2353
+ }
2354
+ class ObjectPatternLoopSection extends StatefulWidget {
2355
+ const ObjectPatternLoopSection({super.key, required this.items, required this.label, this.unused});
2356
+ final List<Item> items;
2357
+ final String label;
2358
+ final String? unused;
2359
+ State<ObjectPatternLoopSection> createState() => _ObjectPatternLoopSectionState();
2360
+ }
2361
+ class _ObjectPatternLoopSectionState extends State<ObjectPatternLoopSection> {
2362
+ Widget build(BuildContext context) => Column(children: [
2363
+ for (final Item(:id) in widget.items) Text('$id ${widget.label}'),
2364
+ ]);
2365
+ }
2366
+ class InitializerLoopSection extends StatefulWidget {
2367
+ const InitializerLoopSection({super.key, required this.start, required this.label, this.unused});
2368
+ final int start;
2369
+ final String label;
2370
+ final String? unused;
2371
+ State<InitializerLoopSection> createState() => _InitializerLoopSectionState();
2372
+ }
2373
+ class _InitializerLoopSectionState extends State<InitializerLoopSection> {
2374
+ Widget build(BuildContext context) => Column(children: [
2375
+ for (var i = widget.start; i < 3; i++) Text(widget.label),
2376
+ ]);
2377
+ }
2378
+ class ShadowInitializerLoopSection extends StatefulWidget {
2379
+ const ShadowInitializerLoopSection({super.key, required this.title});
2380
+ final String title;
2381
+ State<ShadowInitializerLoopSection> createState() => _ShadowInitializerLoopSectionState();
2382
+ }
2383
+ class _ShadowInitializerLoopSectionState extends State<ShadowInitializerLoopSection> {
2384
+ Widget build(BuildContext context) => Column(children: [
2385
+ for (var widget = const ShadowInitializerLoopSection(title: 'local'); widget.title.isEmpty;)
2386
+ Text(widget.title),
2387
+ ]);
2388
+ }
2389
+ class ShadowLoopSection extends StatefulWidget {
2390
+ const ShadowLoopSection({super.key, required this.items, required this.title});
2391
+ final List<ShadowLoopSection> items;
2392
+ final String title;
2393
+ State<ShadowLoopSection> createState() => _ShadowLoopSectionState();
2394
+ }
2395
+ class _ShadowLoopSectionState extends State<ShadowLoopSection> {
2396
+ Widget build(BuildContext context) => Column(children: [
2397
+ for (final widget in widget.items) Text(widget.title),
2398
+ ]);
2399
+ }
2400
+ class ShadowStatementLoopSection extends StatefulWidget {
2401
+ const ShadowStatementLoopSection({super.key, required this.items, required this.title});
2402
+ final List<ShadowStatementLoopSection> items;
2403
+ final String title;
2404
+ State<ShadowStatementLoopSection> createState() => _ShadowStatementLoopSectionState();
2405
+ }
2406
+ class _ShadowStatementLoopSectionState extends State<ShadowStatementLoopSection> {
2407
+ Widget build(BuildContext context) {
2408
+ for (final widget in widget.items) {
2409
+ Text(widget.title);
2410
+ }
2411
+ return const SizedBox();
2412
+ }
2413
+ }
2414
+ class ShadowPatternLoopSection extends StatefulWidget {
2415
+ const ShadowPatternLoopSection({super.key, required this.pairs, required this.title});
2416
+ final List<(ShadowPatternLoopSection, int)> pairs;
2417
+ final String title;
2418
+ State<ShadowPatternLoopSection> createState() => _ShadowPatternLoopSectionState();
2419
+ }
2420
+ class _ShadowPatternLoopSectionState extends State<ShadowPatternLoopSection> {
2421
+ Widget build(BuildContext context) => Column(children: [
2422
+ for (final (widget, _) in widget.pairs) Text(widget.title),
2423
+ ]);
2424
+ }
2425
+ ";
2426
+
2427
+ #[test]
2428
+ fn state_field_reads_survive_for_in_iterables_over_widget() -> Result<(), Box<dyn std::error::Error>>
2429
+ {
2430
+ let targets =
2431
+ unused_param_targets(parse_findings(FOR_IN_ITERABLE_WIDGET_READS_SOURCE)?.unused_params);
2432
+
2433
+ assert_eq!(
2434
+ targets,
2435
+ vec![
2436
+ "CatalogSection.unused",
2437
+ "StatementLoopSection.unused",
2438
+ "PatternLoopSection.unused",
2439
+ "IndexedLoopSection.unused",
2440
+ "TypedLoopSection.unused",
2441
+ "AwaitLoopSection.unused",
2442
+ "ObjectPatternLoopSection.unused",
2443
+ "InitializerLoopSection.unused",
2444
+ "ShadowInitializerLoopSection.title",
2445
+ "ShadowLoopSection.title",
2446
+ "ShadowStatementLoopSection.title",
2447
+ "ShadowPatternLoopSection.title"
2448
+ ]
2449
+ );
2450
+ Ok(())
2451
+ }
2452
+
2453
+ #[test]
2454
+ fn direct_field_reads_survive_for_in_iterables_named_like_the_loop_variable()
2455
+ -> Result<(), Box<dyn std::error::Error>> {
2456
+ let source = r"
2457
+ class TagsCard extends StatelessWidget {
2458
+ const TagsCard({super.key, required this.tags, this.unused});
2459
+ final String tags;
2460
+ final String? unused;
2461
+ Widget build(BuildContext context) => Column(children: [
2462
+ for (final tags in tags.split(',')) Text(tags),
2463
+ ]);
2464
+ }
2465
+ class ShadowTagsCard extends StatelessWidget {
2466
+ const ShadowTagsCard({super.key, required this.title});
2467
+ final String title;
2468
+ Widget build(BuildContext context) => Column(children: [
2469
+ for (final title in ['local']) Text(title),
2470
+ ]);
2471
+ }
2472
+ ";
2473
+ let targets = unused_param_targets(parse_findings(source)?.unused_params);
2474
+
2475
+ assert_eq!(targets, vec!["TagsCard.unused", "ShadowTagsCard.title"]);
2476
+ Ok(())
2477
+ }