Skip to content

Commit 15a4b39

Browse files
authored
Add rationale & example to prefer_for_elements_to_map_fromIterable (dart-archive/linter#3287)
* Add rationale & example to prefer_for_elements_to_map_fromIterable * Add single quotes to for statements To improve clarity of the messages.
1 parent 3b7ae74 commit 15a4b39

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

lib/src/rules/prefer_for_elements_to_map_fromIterable.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ import 'package:analyzer/dart/ast/visitor.dart';
88

99
import '../analyzer.dart';
1010

11-
const _desc = r'Prefer for elements when building maps from iterables.';
11+
const _desc = r"Prefer 'for' elements when building maps from iterables.";
1212

1313
const _details = r'''
14-
When building maps from iterables, it is preferable to use for elements.
14+
When building maps from iterables, it is preferable to use 'for' elements.
15+
16+
Using 'for' elements brings several benefits including:
17+
18+
- Performance
19+
- Flexibility
20+
- Readability
21+
- Improved type inference
22+
- Improved interaction with null safety
23+
1524
1625
**BAD:**
1726
```dart
@@ -30,6 +39,16 @@ return {
3039
'${demo.routeName}': demo.buildRoute,
3140
};
3241
```
42+
43+
**GOOD:**
44+
```dart
45+
// Map<int, Student> is not required, type is inferred automatically.
46+
final pizzaRecipients = {
47+
...studentLeaders,
48+
for (var student in classG)
49+
if (student.isPassing) student.id: student,
50+
};
51+
```
3352
''';
3453

3554
class PreferForElementsToMapFromIterable extends LintRule {

0 commit comments

Comments
 (0)