Don't guard Dictionary<K, V>.Remove(key) by ContainsKey(key)#4836
Conversation
|
You'll need to rebase on .NET 6 release branch. @mavasani Should this go to preview 1 or preview 2 branches? TIP: I'd suggest hard-resetting the last commit (13c184b) before rebasing. This should decrease conflicts, then re-run pack target after rebasing. |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...ore/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.Fixer.cs
Outdated
Show resolved
Hide resolved
...ore/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.Fixer.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...ests/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKeyTests.cs
Outdated
Show resolved
Hide resolved
...ests/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKeyTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
New .NET6.0 analyzers must target release/6.0.1xx-preview2 branch, please retarget.
13c184b to
0a3bdb3
Compare
...ests/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKeyTests.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...ests/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKeyTests.cs
Outdated
Show resolved
Hide resolved
...ore/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.Fixer.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
|
Thanks @chucker. You will need to apply the code-fix to add the new rule entry. |
Not sure how I managed to write a code fix and not consider the possibility that adding this rule is as simple as invoking a code fix. :-) |
5a836cb to
8155018
Compare
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
@mavasani this PR is ready for review, need your approval as change requested |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
...zers/Core/Microsoft.NetCore.Analyzers/Performance/DoNotGuardDictionaryRemoveByContainsKey.cs
Outdated
Show resolved
Hide resolved
|
@buyaa-n Seems like there are real test failures. |
Failed on |
|
Thanks @chucker for your contribution, the PR is finally ready for a merge. Looks @carlossanlop requested a change long time ago to unify this PR with @CollinAlpert's PR, to ensure all 3 closely-related analyzers are handled together. But somehow @CollinAlpert were not able to push his changes to this PR, I had offline conversation with @carlossanlop about this issue and we decided to merge this PR first and let @CollinAlpert work on main branch instead. Seems Carlos forgot to remove the change request, as he is out in rest of the week I am dismissing it to unblock the PR |
We decided to merge this PR first
|
Thanks @buyaa-n. So I will just create a new branch off of |
Exactly |
|
I just merged |
This implements dotnet/runtime#33797, including a small number of tests for both C# and VB.
By design, the diagnostic will not be reported if the condition contains multiple statements, as suggested by @carlossanlop. That means it doesn't yet include @terrajobst's suggested further improvement of hoisting the
Removecall into the condition, and leaving the remaining statements inside the block.