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

Warn on call to Map.remove with unrelated type #57434

Closed
a14n opened this issue Feb 9, 2017 · 11 comments
Closed

Warn on call to Map.remove with unrelated type #57434

a14n opened this issue Feb 9, 2017 · 11 comments
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-request type-enhancement A request for a change that isn't a bug

Comments

@a14n
Copy link
Contributor

a14n commented Feb 9, 2017

final map = <String,String>{};
map.remove(1); // LINT
map.remove(""); // OK
@pq pq added the type-enhancement A request for a change that isn't a bug label Jun 29, 2018
@srawlins
Copy link
Member

Seems like this can just be folded into list_remove_unrelated_type? Can't imagine why someone would want to use one and not the other.

@a14n
Copy link
Contributor Author

a14n commented Oct 21, 2018

But the name of the lint is completely unrelated to map.

@bwilkerson
Copy link
Member

More importantly, extending a rule to produce lints where it never did before is, in some sense, a breaking change for anyone using the lint.

@srawlins
Copy link
Member

I don't think that the name should prevent us from re-using it. We can create an alias for list_remove_unrelated_type called remove_unrelated_type (or better yet, rename the rule and create an alias for the old name).

Is there a reason to not make a breaking change? Just bump the appropriate version number.

@a14n
Copy link
Contributor Author

a14n commented Oct 21, 2018

Is there a reason to not make a breaking change? Just bump the appropriate version number.

Unfortunately linter package is not like any other package. It's bundled in the sdk with the analyzer. So a simple new release of the sdk may come with a new version of linter. Actually the version number of linter is only used to link it into the sdk.

Note that it would be a normal package if it'd become an anlyzer plugin.

@bwilkerson
Copy link
Member

Note that it would be a normal package ...

Yes, but that comes with other costs, and it isn't clear to me that that's the best path forward for our users.

@srawlins
Copy link
Member

Since @pq has been working on deprecation recently, what say we deprecate list_remove_unrelated_type and rename to collection_remove_unrelated_type?

@bwilkerson
Copy link
Member

Sounds reasonable. And if we do that, then I'm going to want us to have a way to implement a quick-fix to change the name for users, which our current deprecation support doesn't include.

@munificent
Copy link
Member

Other operations we might want to either roll into the existing lints or define new lints for:

Map.containsKey(Object? key)
Map.containsValue(Object? value)
Map.operator[Object? value]
Set.contains(Object? value)
Set.lookup(Object? object)
Set.remove(Object? value)

@lrhn
Copy link
Member

lrhn commented Jul 8, 2021

The complete list (as I see it) would be:

Iterable.contains(Object?)  // inherited by List/Set/Queue too
List.remove(Object?)
Set.remove(Object?)
Set.lookup(Object?)
Set.containsAll(Iterable<Object?>)
Map.remove(Object?)
Map.containsKey(Object?)
Map.containsValue(Object?)
Map.operator [](Object?)
Queue.remove(Object?)

I've omitted the following:

Set.removeAll(Iterable<Object?>)
Set.retainAll(Iterable<Object?>)
Set.intersection(Set<Object?>)
Set.difference(Set<Object?>)

The intersection and difference makes sense for arbitrary set types, mathematically and type-wise, it's really a symmetric operation, and it's only lack of intersection types that make us choose the type of the first operand.
The removeAll and retainAllare destructive versions of the same operations, so I'd keep allowing any element type there too.
These differ from containsAll where having any member that is not a potential element of the set will guarantee a false result. There might still be situations where that can be useful, but I think it's, on average, better to check the iterable type to avoid mistakes.

(I have also changed Characters.contains from package:characters to have type bool contains(covariant String ...) just to get better warnings.)

@srawlins
Copy link
Member

Complete with dart-lang/linter#3692

@devoncarew devoncarew added analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants