This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
New lint rule: collection_methods_unrelated_type #3692
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
676d911
New lint rule: collection_methods_unrelated_type
srawlins 916faf5
Merge branch 'main' into colletion-methods-unrelated
srawlins d8e564c
Nearly there; index is next
srawlins 06bdf74
Complete error arguments
srawlins 7623079
all.yaml
srawlins c44fc7e
whitespace
srawlins bad44e5
feedback
srawlins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// 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 '../analyzer.dart'; | ||
import '../util/unrelated_types_visitor.dart'; | ||
|
||
const _desc = 'Invocation of various collection methods with arguments of ' | ||
'unrelated types.'; | ||
|
||
const _details = r''' | ||
**DON'T** invoke certain collection method with an argument with an unrelated | ||
type. | ||
|
||
Doing this will invoke `==` on the collection's elements and most likely will | ||
return `false`. | ||
|
||
An argument passed to a collection method should relate to the collection type | ||
as follows: | ||
|
||
* an argument to `Iterable<E>.contains` should be related to `E` | ||
* an argument to `List<E>.remove` should be related to `E` | ||
* an argument to `Map<K, V>.containsKey` should be related to `K` | ||
* an argument to `Map<K, V>.containsValue` should be related to `V` | ||
* an argument to `Map<K, V>.remove` should be related to `K` | ||
* an argument to `Map<K, V>.[]` should be related to `K` | ||
* an argument to `Queue<E>.remove` should be related to `E` | ||
* an argument to `Set<E>.containsAll` should be related to `Iterable<E>` | ||
* an argument to `Set<E>.difference` should be related to `Set<E>` | ||
* an argument to `Set<E>.intersection` should be related to `Set<E>` | ||
* an argument to `Set<E>.lookup` should be related to `E` | ||
* an argument to `Set<E>.remove` should be related to `E` | ||
* an argument to `Set<E>.removeAll` should be related to `Iterable<E>` | ||
* an argument to `Set<E>.retainAll` should be related to `Iterable<E>` | ||
|
||
**BAD:** | ||
```dart | ||
void someFunction() { | ||
var list = <int>[]; | ||
if (list.contains('1')) print('someFunction'); // LINT | ||
} | ||
``` | ||
|
||
**BAD:** | ||
```dart | ||
void someFunction() { | ||
var set = <int>{}; | ||
set.removeAll({'1'}); // LINT | ||
} | ||
``` | ||
|
||
**GOOD:** | ||
```dart | ||
void someFunction() { | ||
var list = <int>[]; | ||
if (list.contains(1)) print('someFunction'); // OK | ||
} | ||
``` | ||
|
||
**GOOD:** | ||
```dart | ||
void someFunction() { | ||
var set = <int>{}; | ||
set.removeAll({1}); // OK | ||
} | ||
``` | ||
|
||
'''; | ||
|
||
class CollectionMethodsUnrelatedType extends LintRule { | ||
static const LintCode code = LintCode('collection_methods_unrelated_type', | ||
"The argument type '{0}' isn't related to '{1}'."); | ||
|
||
CollectionMethodsUnrelatedType() | ||
: super( | ||
name: 'collection_methods_unrelated_type', | ||
description: _desc, | ||
details: _details, | ||
group: Group.errors); | ||
|
||
@override | ||
LintCode get lintCode => code; | ||
|
||
@override | ||
void registerNodeProcessors( | ||
NodeLintRegistry registry, LinterContext context) { | ||
var visitor = _Visitor(this, context.typeSystem, context.typeProvider); | ||
registry.addIndexExpression(this, visitor); | ||
registry.addMethodInvocation(this, visitor); | ||
} | ||
} | ||
|
||
class _Visitor extends UnrelatedTypesProcessors { | ||
_Visitor(super.rule, super.typeSystem, super.typeProvider); | ||
|
||
@override | ||
List<MethodDefinition> get methods => [ | ||
// Argument to `Iterable<E>.contains` should be assignable to `E`. | ||
MethodDefinitionForElement( | ||
typeProvider.iterableElement, | ||
'contains', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
// Argument to `List<E>.remove` should be assignable to `E`. | ||
MethodDefinitionForElement( | ||
typeProvider.listElement, | ||
'remove', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
// Argument to `Map<K, V>.containsKey` should be assignable to `K`. | ||
MethodDefinitionForElement( | ||
typeProvider.mapElement, | ||
'containsKey', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
// Argument to `Map<K, V>.containsValue` should be assignable to `V`. | ||
MethodDefinitionForElement( | ||
typeProvider.mapElement, | ||
'containsValue', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
typeArgumentIndex: 1, | ||
), | ||
// Argument to `Map<K, V>.remove` should be assignable to `K`. | ||
MethodDefinitionForElement( | ||
typeProvider.mapElement, | ||
'remove', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
// Argument to `Queue<E>.remove` should be assignable to `E`. | ||
MethodDefinitionForName( | ||
'dart.collection', | ||
'Queue', | ||
'remove', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
// Argument to `Set<E>.containsAll` should be assignable to `Set<E>`. | ||
MethodDefinitionForElement( | ||
typeProvider.setElement, | ||
'containsAll', | ||
ExpectedArgumentKind.assignableToIterableOfTypeArgument, | ||
), | ||
// Argument to `Set<E>.difference` should be assignable to `Set<E>`. | ||
MethodDefinitionForElement( | ||
typeProvider.setElement, | ||
'difference', | ||
ExpectedArgumentKind.assignableToCollection, | ||
), | ||
// Argument to `Set<E>.intersection` should be assignable to `Set<E>`. | ||
MethodDefinitionForElement( | ||
typeProvider.setElement, | ||
'intersection', | ||
ExpectedArgumentKind.assignableToCollection, | ||
), | ||
// Argument to `Set<E>.lookup` should be assignable to `E`. | ||
MethodDefinitionForElement( | ||
typeProvider.setElement, | ||
'lookup', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
// Argument to `Set<E>.remove` should be assignable to `E`. | ||
MethodDefinitionForElement( | ||
typeProvider.setElement, | ||
'remove', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
// Argument to `Set<E>.removeAll` should be assignable to `Set<E>`. | ||
MethodDefinitionForElement( | ||
typeProvider.setElement, | ||
'removeAll', | ||
ExpectedArgumentKind.assignableToIterableOfTypeArgument, | ||
), | ||
// Argument to `Set<E>.retainAll` should be assignable to `Set<E>`. | ||
MethodDefinitionForElement( | ||
typeProvider.setElement, | ||
'retainAll', | ||
ExpectedArgumentKind.assignableToIterableOfTypeArgument, | ||
), | ||
]; | ||
|
||
@override | ||
List<MethodDefinition> get indexOperators => [ | ||
// Argument to `Map<K, V>.[]` should be assignable to `K`. | ||
MethodDefinitionForElement( | ||
typeProvider.mapElement, | ||
'[]', | ||
ExpectedArgumentKind.assignableToCollectionTypeArgument, | ||
), | ||
]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only have accessors in
TypeProvide
for the types from the SDK that are referenced in the spec (because those are the ones thatanalyzer
needs to use to validate the code. You'll need to add it to the mock sdk and then use something liketypeProvider.objectElement.library.getClass('Queue')
to get the element.