-
Notifications
You must be signed in to change notification settings - Fork 468
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
Prefer Dictionary<K, V>.TryAdd(key) over guarded Add(key) #6199
Conversation
Ping for review :) |
...ts/Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddTests.cs
Outdated
Show resolved
Hide resolved
# Conflicts: # src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md
# Conflicts: # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif # src/NetAnalyzers/RulesMissingDocumentation.md # src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
private static bool DoesSignatureMatch(IMethodSymbol suspected, IMethodSymbol comparator) | ||
{ | ||
return suspected.OriginalDefinition.ReturnType.Name == comparator.ReturnType.Name | ||
&& suspected.Name == comparator.Name | ||
&& suspected.Parameters.Length == comparator.Parameters.Length | ||
&& suspected.Parameters.Zip(comparator.Parameters, (p1, p2) => p1.OriginalDefinition.Type.Name == p2.Type.Name).All(isParameterEqual => isParameterEqual); | ||
} |
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.
Why not use suspected.Equals(comparator, SymbolEqualityComparer.Default)
?
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.
Because symbolically, System.Collections.Generic.Dictionary<TKey, TValue>.ContainsKey(TKey)
and System.Collections.Generic.IDictionary<TKey, TValue>.ContainsKey(TKey)
are not equal.
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.
@CollinAlpert There is an IsImplementationOfInterfaceMember
extension method that I think could work for this scenario.
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.
Yeah, I tried substituting the body of the method with return suspected.OriginalDefinition.IsImplementationOfInterfaceMember(comparator);
, but that also doesn't work.
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...rosoft.NetCore.Analyzers/Performance/CSharpPreferDictionaryTryAddValueOverGuardedAddFixer.cs
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/Performance/BasicPreferDictionaryTryAddValueOverGuardedAddFixer.vb
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/Performance/BasicPreferDictionaryTryAddValueOverGuardedAddFixer.vb
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/Performance/BasicPreferDictionaryTryAddValueOverGuardedAddFixer.vb
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/Performance/BasicPreferDictionaryTryAddValueOverGuardedAddFixer.vb
Outdated
Show resolved
Hide resolved
...ts/Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddTests.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddAnalyzer.cs
Outdated
Show resolved
Hide resolved
...ts/Microsoft.NetCore.Analyzers/Performance/PreferDictionaryTryAddValueOverGuardedAddTests.cs
Outdated
Show resolved
Hide resolved
That's weird. I double checked everything, and the IDs registered are all correct and passed to the appropriate properties. Unfortunately I don't use VS, so I can't check there. I consolidated the fixers now, maybe that will help? |
Thanks for the updates @CollinAlpert, now the fixer is working in VS, so I continued with analyzing all diagnostics and found these false positives:
XmlQualifiedName notationName = GetNameQualified(false);
SchemaNotation? notation = null;
if (!_schemaInfo.Notations.ContainsKey(notationName.Name))
{
_undeclaredNotations?.Remove(notationName.Name);
notation = new SchemaNotation(notationName);
_schemaInfo.Notations.Add(notation.Name.Name, notation);
}
Alos found one false negative case, it is not flagging the below case when it should: public void Test(Dictionary<XmlQualifiedName, SchemaElementDecl> _elementDecls )
{
foreach (KeyValuePair<XmlQualifiedName, SchemaElementDecl> entry in sinfo._elementDecls)
{
if (!_elementDecls.ContainsKey(entry.Key))
{
_elementDecls.Add(entry.Key, entry.Value);
}
}
} Please fix these issues, thank you! |
@buyaa-n Thanks for the hints. I have addressed them and added tests. Regarding the false negative, I don't think it is one. Behind a property reference (in this case |
# Conflicts: # src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif # src/NetAnalyzers/RulesMissingDocumentation.md
Thank you!
Well, the public KeyValuePair(TKey key, TValue value)
{
this.key = key;
this.value = value;
}
public TKey Key => key;
public TValue Value => value; Anyway, I guess you meant about any property not specific to |
Yes, I meant properties in general could have expensive computations in their getter, not specifically KeyValuePair. |
...t.NetCore.Analyzers/Performance/CSharpPreferDictionaryTryMethodsOverContainsKeyGuardFixer.cs
Outdated
Show resolved
Hide resolved
...soft.NetCore.Analyzers/Performance/PreferDictionaryTryMethodsOverContainsKeyGuardAnalyzer.cs
Show resolved
Hide resolved
...soft.NetCore.Analyzers/Performance/PreferDictionaryTryMethodsOverContainsKeyGuardAnalyzer.cs
Show resolved
Hide resolved
...soft.NetCore.Analyzers/Performance/PreferDictionaryTryMethodsOverContainsKeyGuardAnalyzer.cs
Outdated
Show resolved
Hide resolved
...soft.NetCore.Analyzers/Performance/PreferDictionaryTryMethodsOverContainsKeyGuardAnalyzer.cs
Outdated
Show resolved
Hide resolved
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.
Left some NIT, overall LGTM, thank you!
It would be great if you could add a test case for #6589
This PR adds an Analyzer to detect usages of
dictionary.Add()
guarded by adictionary.ContainsKey()
check. It also adds a CodeFix which offers to refactor this construct into a singledictionary.TryAdd()
call.Fixes dotnet/runtime#33799, #6589