Skip to content
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

The Analyzer for CA1854: Prefer the IDictionary.TryGetValue Is Not Working Properly #6589

Closed
vsfeedback opened this issue Apr 17, 2023 · 5 comments
Assignees
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Negative No diagnostic is reported for a problematic case good first issue help wanted The issue is up-for-grabs, and can be claimed by commenting
Milestone

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


The analyzer for CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method is not working properly. For example it did not create a warning (as I have setup in my EditorConfig) for this code:

if (oldTopNames.ContainsKey(key) && !string. IsNullOrEmpty(kvDI.Value.OriginalTopLevelPlanName)) {
    kvDI.Value.OriginalTopLevelPlanName = oldTopNames[key];
}

It should have created a refactoring to change it to this code:

if (oldTopNames.TryGetValue(key, out var planName) && !string. IsNullOrEmpty(kvDI.Value.OriginalTopLevelPlanName)) {
    kvDI.Value.OriginalTopLevelPlanName = planName;
}

Original Comments

Feedback Bot on 4/5/2023, 07:38 PM:

(private comment, text removed)


Original Solutions

(no solutions)

@CollinAlpert
Copy link
Contributor

Hi, I developed this analyzer. It currently only emits a diagnostic when ContainsKey is the only condition in the if block. I'll see if I can get around to fixing that sometime soon.

@mavasani mavasani added Area-Microsoft.CodeAnalysis.NetAnalyzers help wanted The issue is up-for-grabs, and can be claimed by commenting False_Negative No diagnostic is reported for a problematic case good first issue labels Jun 23, 2023
@mavasani mavasani added this to the Unknown milestone Jun 23, 2023
@CollinAlpert
Copy link
Contributor

@mavasani Should be fixed by #6199.

@buyaa-n
Copy link
Member

buyaa-n commented Jun 28, 2023

@mavasani Should be fixed by #6199.

Did you add a test for this in #6199? If not, could you add one to verify this fixed? Thanks!

@CollinAlpert
Copy link
Contributor

I added an explicit test now.

@buyaa-n
Copy link
Member

buyaa-n commented Jun 28, 2023

Thank you!

@buyaa-n buyaa-n closed this as completed Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Negative No diagnostic is reported for a problematic case good first issue help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

No branches or pull requests

4 participants