-
Notifications
You must be signed in to change notification settings - Fork 466
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
Issue 33798: Prefer Dictionary<K, V>.TryGetValue() over guarded indexer access #4851
Conversation
# Conflicts: # src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md
...alyzers/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpPreferDictionaryTryGetValueFixer.cs
Show resolved
Hide resolved
...alyzers/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpPreferDictionaryTryGetValueFixer.cs
Outdated
Show resolved
Hide resolved
...alyzers/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpPreferDictionaryTryGetValueFixer.cs
Outdated
Show resolved
Hide resolved
...alyzers/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpPreferDictionaryTryGetValueFixer.cs
Outdated
Show resolved
Hide resolved
...alyzers/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpPreferDictionaryTryGetValueFixer.cs
Outdated
Show resolved
Hide resolved
var dictionaryAccessNode = root.FindNode(dictionaryAccessLocation.SourceSpan); | ||
var dictionaryAccess = dictionaryAccessNode as ElementAccessExpressionSyntax ?? (dictionaryAccessNode as ArgumentSyntax)?.Expression as ElementAccessExpressionSyntax; | ||
if (dictionaryAccess is null | ||
|| root.FindNode(context.Span) is not InvocationExpressionSyntax containsKeyInvocation |
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.
Same as above, do we need getInnermostNodeForTie
here? If so, needs a test that demonstrates that.
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.
We do not, as I still need to access the InvocationExpressionSyntax
later on.
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.
if we do not need InvocationExpressionSyntax
set, should we remove this?
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
} | ||
|
||
var additionalLocations = ImmutableArray.Create(propertyReference.Syntax.GetLocation()); | ||
context.ReportDiagnostic(Diagnostic.Create(ContainsKeyRule, containsKeyInvocation.Syntax.GetLocation(), additionalLocations)); |
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.
You may want to use the CreateDiagnostic extension method.
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? What advantages do I gain?
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...ers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueMethodsTests.cs
Show resolved
Hide resolved
...ers/VisualBasic/Microsoft.NetCore.Analyzers/Runtime/BasicPreferDictionaryTryGetValueFixer.vb
Outdated
Show resolved
Hide resolved
...ers/VisualBasic/Microsoft.NetCore.Analyzers/Runtime/BasicPreferDictionaryTryGetValueFixer.vb
Outdated
Show resolved
Hide resolved
...ers/VisualBasic/Microsoft.NetCore.Analyzers/Runtime/BasicPreferDictionaryTryGetValueFixer.vb
Outdated
Show resolved
Hide resolved
@CollinAlpert I think it might be better to start with
Thanks, i see dotnet/runtime#33799 is also assigned to you, you must have already noticed that they are very closely related and preferably one analyzer (could have a different ID if needed). What are you planning for that analyzer? i think to cover both it's better lookup |
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...ers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueMethodsTests.cs
Outdated
Show resolved
Hide resolved
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...alyzers/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpPreferDictionaryTryGetValueFixer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf
Outdated
Show resolved
Hide resolved
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
Seems need to run |
...ers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueMethodsTests.cs
Outdated
Show resolved
Hide resolved
# Conflicts: # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
@buyaa-n I just merged the
|
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
Probably caused by that missing quota, if that not fixes the issue try reverting all changes in all generated files:
And run the |
...NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueAnalyzer.cs
Outdated
Show resolved
Hide resolved
...ers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueMethodsTests.cs
Outdated
Show resolved
Hide resolved
...ers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferDictionaryTryGetValueMethodsTests.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.
Looks good to me, thank you for your contribution @CollinAlpert
Added analyzer and code fix for guarded dictionary indexer accesses.
The analyzer looks for instances of a dictionary indexer access and checks if it is guarded by a
ContainsKey
check. If so, a diagnostic is raised and a code fix is offered. When applied, theContainsKey
check is replaced with aTryGetValue
invocation.Fixes issue #33798.