Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lint rule: collection_methods_unrelated_type #3692

Merged
merged 7 commits into from
Sep 29, 2022

Conversation

srawlins
Copy link
Member

@srawlins srawlins commented Sep 16, 2022

Description

Fixes #1307

This reports when an "unrelated" argument is given for the following methods:

  • Iterable.contains,
  • List.remove,
  • Map.containsKey, Map.containsValue, Map.remove, Map.[],
  • Queue.remove,
  • Set.containsAll, Set.difference, Set.intersection, Set.lookup, Set.remove, Set.removeAll, Set.retainAll.

@github-actions github-actions bot added the set-core Affects a rule in the core Dart rule set label Sep 16, 2022
@srawlins srawlins marked this pull request as draft September 16, 2022 15:55
@srawlins
Copy link
Member Author

We need to wait on an analyzer release with https://dart-review.googlesource.com/c/sdk/+/259506 in order to test.

@coveralls
Copy link

coveralls commented Sep 16, 2022

Coverage Status

Coverage increased (+0.03%) to 95.711% when pulling bad44e5 on colletion-methods-unrelated into 2986236 on main.

Copy link
Member

@bwilkerson bwilkerson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I review this further, I'd like to understand what criteria you used to decide that a single monolithic lint would be better that individual lints. I think there are more good reasons for having smaller lints, but would like to understand your reasoning.

lib/src/rules/collection_methods_unrelated_type.dart Outdated Show resolved Hide resolved
lib/src/rules/collection_methods_unrelated_type.dart Outdated Show resolved Hide resolved
ExpectedArgumentKind.assignableToCollectionTypeArgument,
),
// TODO(srawlins): Queue? It's not in mock SDK or [TypeProvider].
// Argument to `Queue<E>.remove` should be assignable to `E`.
Copy link
Member

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 that analyzer needs to use to validate the code. You'll need to add it to the mock sdk and then use something like typeProvider.objectElement.library.getClass('Queue') to get the element.

lib/src/util/unrelated_types_visitor.dart Outdated Show resolved Hide resolved
lib/src/util/unrelated_types_visitor.dart Outdated Show resolved Hide resolved
@srawlins
Copy link
Member Author

@bwilkerson A monolithic lint rule just because in the discussion at #1307, folks voiced that they couldn't imagine wanting to enable a subset of the rules. With the advent of package:lints, maybe it is not a high cost to spell out each rule.

You noted in that discussion that it would be simpler for a user to bulk-ignore, for example, all Set.contains reports by name (// ignore_for_file: set_contains_unrelated_types or disable in analysis_options).

I have no strong opinion.

@bwilkerson
Copy link
Member

You noted in that discussion that it would be simpler for a user to bulk-ignore ...

But harder for a user to bulk-ignore a subset of the cases. I'm not sure it matters, especially if we can deprecate the existing individual rules. But I was curious. Thanks.

copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Sep 22, 2022
This is needed in order to test
dart-lang/linter#3692

Bug: dart-lang/linter#3692
Change-Id: I8be22ca319647fba0e6ff9a462571bd972e383c5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260501
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
@srawlins srawlins marked this pull request as ready for review September 29, 2022 14:19
@srawlins
Copy link
Member Author

This PR is now ready for review. I resolved all of the comments left on it while it was in draft.

lib/src/util/unrelated_types_visitor.dart Outdated Show resolved Hide resolved
lib/src/util/unrelated_types_visitor.dart Outdated Show resolved Hide resolved
var argumentType = argument.staticType;
if (argumentType == null) return;

switch (methodDefinition.expectedArgumentKind) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a method to MethodDefinition that takes the argumentType and collectionType and returns true if the lint should be reported. I think that would simplify the code, and might even eliminate the need for the ExpectedArgumentKind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible... I think it would actually add complexity. There are a few particulars that we'd have to push into MethodDefinition like:

  • it would also need the TypeProvider, only for the Set.containsAll, Set.removeAll, and Set.retainAll cases,
  • it would still need to be this big switch based on ExpectedArgumentKind; I don't think we could remove it.

@srawlins srawlins merged commit e5a079a into main Sep 29, 2022
@srawlins srawlins deleted the colletion-methods-unrelated branch September 29, 2022 18:18
mockturtl added a commit to mockturtl/tidy that referenced this pull request Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
set-core Affects a rule in the core Dart rule set
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: avoid_collection_methods_with_unrelated_types
3 participants