You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Replace Dictionary<,>.Keys.Contains with ContainsKey #35343
This is a proposal for a new analyzer/codefix for:
// givenDictionary<int,string>dic=...;dic.Keys.Contains(1);dic.Values.Contains("");// Could instead bedic.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.
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...
@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?
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.
@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.
@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?
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.
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.
@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.
Activity
mavasani commentedon Apr 2, 2020
@jeffhandley @terrajobst want to move this proposal to runtime repo under your radar?
Evangelink commentedon Apr 15, 2020
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...
drewnoakes commentedon Apr 16, 2020
In this case I think it would be enough to set the default diagnostic level to warning.
Evangelink commentedon Apr 19, 2020
@drewnoakes Do you think there are cases where it would make sense to call
Containson aDictionary<TKey,TValue>.KeyCollectionorDictionary<TKey,TValue>.ValueCollection? If not maybe the easiest is to actually banSystem.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection.Contains(TKey)andSystem.Collections.Generic.Dictionary<TKey,TValue>.ValueCollection.Contains(TValue). WDYT?Tagging @mavasani for a second opinion.
drewnoakes commentedon Apr 21, 2020
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.
Evangelink commentedon Apr 21, 2020
@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.
Evangelink commentedon Apr 23, 2020
@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?Evangelink commentedon Apr 23, 2020
In addition to my previous comment, I am wondering if we should extend the rule to also cover
Count(). ForIDictionaryandDictionarythat seems to be fine becauseKeysreturnsKeyCollectionwith the property so there is another rule kicking-in to say use the property instead. But forKeyswhere the type isIEnumerablethere is no rule reporting.mavasani commentedon Apr 23, 2020
@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?
Dotnet-GitSync-Bot commentedon Apr 23, 2020
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.
8 remaining items
Mrnikbobjeff commentedon Aug 30, 2020
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.
carlossanlop commentedon Feb 4, 2021
@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.
jeffhandley commentedon Feb 5, 2021
@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.
NewellClark commentedon Feb 5, 2021
@jeffhandley Gotcha.
stephentoub commentedon Aug 12, 2021
Can this be closed (dotnet/roslyn-analyzers#4687)?
buyaa-n commentedon Aug 17, 2021
Yes, closing