Skip to content

Replace Dictionary<,>.Keys.Contains with ContainsKey #35343

Closed
dotnet/roslyn-analyzers
#4687

Description

@drewnoakes

This is a proposal for a new analyzer/codefix for:

// given
Dictionary<int, string> dic = ...;

dic.Keys.Contains(1);
dic.Values.Contains("");

// Could instead be
dic.ContainsKey(1);
dic.ContainsValue("");

I found this code in some design-time build targets today.

EDIT I'd previously incorrectly stated that the former form was O(N), but System.Linq.Enumerable.Contains does type test for ICollection<> and delegate to ICollection<>.Contains, which is also O(1). There is some overhead associated with that however, which could be avoided.

Activity

  1. mavasani commented on Apr 2, 2020

    @mavasani

    @jeffhandley @terrajobst want to move this proposal to runtime repo under your radar?

  2. Evangelink commented on Apr 15, 2020

    @Evangelink
    Member

    I can take care of this implementation if you wish. I am wondering if we could somehow improve the banned API analyzer to allow catching such cases, basically you would want to forbid a specific method call in some specific context...

  3. drewnoakes commented on Apr 16, 2020

    @drewnoakes
    MemberAuthor

    basically you would want to forbid a specific method call in some specific context...

    In this case I think it would be enough to set the default diagnostic level to warning.

  4. Evangelink commented on Apr 19, 2020

    @Evangelink
    Member

    @drewnoakes Do you think there are cases where it would make sense to call Contains on a Dictionary<TKey,TValue>.KeyCollection or Dictionary<TKey,TValue>.ValueCollection? If not maybe the easiest is to actually ban System.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection.Contains(TKey) and System.Collections.Generic.Dictionary<TKey,TValue>.ValueCollection.Contains(TValue). WDYT?

    Tagging @mavasani for a second opinion.

  5. drewnoakes commented on Apr 21, 2020

    @drewnoakes
    MemberAuthor

    The banned API analyzer tends to be for banning APIs that are specific to a given code base. I don't think any code base has a use case for calling either of those methods. It may be possible to ship a framework-specific analyzer that's based on the banned API analyzer and bundles a known set of APIs, such as these.

  6. Evangelink commented on Apr 21, 2020

    @Evangelink
    Member

    @drewnoakes My idea behind the use of the "Banned API" analyzer was to provide a way where we won't have to implement a new analyzer for each new member call we want to prevent. At the same time, the code in itself is pretty easy to write so there might be no point in having an infrastructure in place.

  7. Evangelink commented on Apr 23, 2020

    @Evangelink
    Member

    @drewnoakes @mavasani Do you think that's better to report only on specific dictionary instances or also on the interface? Reporting also on the interface might catch more cases but could potentially lead to FP for custom implementations of the dictionary. I think the risk is pretty low so I would be in favor of catching everything that implements IDictionary<TKey, TValue>/IReadOnlyDictionary<TKey, TValue>. WDYT?

  8. Evangelink commented on Apr 23, 2020

    @Evangelink
    Member

    In addition to my previous comment, I am wondering if we should extend the rule to also cover Count(). For IDictionary and Dictionary that seems to be fine because Keys returns KeyCollection with the property so there is another rule kicking-in to say use the property instead. But for Keys where the type is IEnumerable there is no rule reporting.

  9. mavasani commented on Apr 23, 2020

    @mavasani

    @Evangelink This issue likely needs to be moved to https://github.com/dotnet/runtime with code-analyzer label and would need an approval on the design and/or implementation.

    @jeffhandley @bartonjs @stephentoub Can one of you please port this issue OR give an approval on the proposal here?

  10. Dotnet-GitSync-Bot commented on Apr 23, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

  11. added
    untriaged
    on Apr 23, 2020
  12. added
    api-suggestion
    code-analyzer
    and removed
    untriaged
    on Apr 23, 2020
  13. 8 remaining items

  14. added
    help wanted
    on Apr 28, 2020
  15. Mrnikbobjeff commented on Aug 30, 2020

    @Mrnikbobjeff

    I would like to have this assigned to me. I just completed my work on this analyzer including the code fix provider. One flaw still unadressed is the fact that only Dictionary<T,K> has a ContainsValue method, the interfaces do not. I would suggest taking the simple route and only apply ContainsValue if we know the member which is accessed is a Dictionary<T,K>. This is the way I implemented it.

  16. removed
    help wanted
    on Feb 4, 2021
  17. carlossanlop commented on Feb 4, 2021

    @carlossanlop
    Contributor

    @NewellClark thanks for starting working on this. I'm trying to assign to you but I can't for some reason, but consider it yours.

    @Mrnikbobjeff apologies for not assigning this to you when you requested it, but your offer is appreciated. We have many more analyzers marked as up-for-grabs and would love to get your help on those others too.

  18. jeffhandley commented on Feb 5, 2021

    @jeffhandley
    Member

    @NewellClark We figured out why we cannot assign this to you. You must comment on the issue before you show up in the assignees list.

  19. NewellClark commented on Feb 5, 2021

    @NewellClark
    Contributor

    @jeffhandley Gotcha.

  20. stephentoub commented on Aug 12, 2021

    @stephentoub
    Member

    Can this be closed (dotnet/roslyn-analyzers#4687)?

  21. buyaa-n commented on Aug 17, 2021

    @buyaa-n
    Contributor

    Yes, closing

  22. ghost locked as resolved and limited conversation to collaborators on Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Collectionscode-analyzercode-fixer

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions