Skip to content

Commit

Permalink
add unnecessary_to_list_in_spreads
Browse files Browse the repository at this point in the history
  • Loading branch information
a14n committed May 17, 2022
1 parent 7c714a2 commit 8d446a7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ linter:
- unnecessary_string_escapes
- unnecessary_string_interpolations
- unnecessary_this
- unnecessary_to_list_in_spreads
- unrelated_type_equality_checks
- unsafe_html
- use_build_context_synchronously
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ import 'rules/unnecessary_statements.dart';
import 'rules/unnecessary_string_escapes.dart';
import 'rules/unnecessary_string_interpolations.dart';
import 'rules/unnecessary_this.dart';
import 'rules/unnecessary_to_list_in_spreads.dart';
import 'rules/unrelated_type_equality_checks.dart';
import 'rules/unsafe_html.dart';
import 'rules/use_build_context_synchronously.dart';
Expand Down Expand Up @@ -401,6 +402,7 @@ void registerLintRules({bool inTestMode = false}) {
..register(UnnecessaryStringEscapes())
..register(UnnecessaryStringInterpolations())
..register(UnnecessaryThis())
..register(UnnecessaryToListInSpreads())
..register(UnrelatedTypeEqualityChecks())
..register(UnsafeHtml())
..register(UseBuildContextSynchronously(inTestMode: inTestMode))
Expand Down
65 changes: 65 additions & 0 deletions lib/src/rules/unnecessary_to_list_in_spreads.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';

import '../analyzer.dart';
import '../util/dart_type_utilities.dart';

const _desc = r'Unnecessary toList() in spreads.';

const _details = r'''
Unnecessary `toList()` in spreads.
**BAD:**
```dart
children: <Widget>[
...['foo', 'bar', 'baz'].map((String s) => Text(s)).toList(),
]
```
**GOOD:**
```dart
children: <Widget>[
...['foo', 'bar', 'baz'].map((String s) => Text(s)),
]
```
''';

class UnnecessaryToListInSpreads extends LintRule {
UnnecessaryToListInSpreads()
: super(
name: 'unnecessary_to_list_in_spreads',
description: _desc,
details: _details,
group: Group.style,
);

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this);
registry.addSpreadElement(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;

_Visitor(this.rule);

@override
void visitSpreadElement(SpreadElement node) {
var expression = node.expression;
if (expression is MethodInvocation &&
expression.methodName.name == 'toList' &&
DartTypeUtilities.implementsInterface(
expression.target?.staticType, 'Iterable', 'dart.core')) {
rule.reportLint(expression.methodName);
}
}
}
25 changes: 25 additions & 0 deletions test_data/rules/unnecessary_to_list_in_spreads.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// test w/ `dart test -N unnecessary_to_list_in_spreads`

var ok = [
...[1, 2].whereType<int>(), // OK
];

var t1 = [
...[1, 2].toList(), // LINT
];
var t2 = [
...{1, 2}.toList(), // LINT
];
var t3 = [
...?[1, 2].toList(), // LINT
];
var t4 = [
...?{1, 2}.toList(), // LINT
];
var t5 = [
...[1, 2].whereType<int>().toList(), // LINT
];

0 comments on commit 8d446a7

Please sign in to comment.