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

Fix S4158: Rule should consider .NetCore 2.0+ 'Dictionary.TryAdd' method #2341

Closed
evan-h opened this issue Mar 13, 2019 · 2 comments
Closed
Assignees
Labels
Area: C# C# rules related issues.
Milestone

Comments

@evan-h
Copy link

evan-h commented Mar 13, 2019

Quality check misunderstands 'Dictionary.TryAdd' and raises the following issue

Remove this call, the collection is known to be empty here

Example:

var contents = new Dictionary<string, CmsContent>();

if (!contents.TryAdd(item.ID, new CmsContent { Text = item.Value.Value }))
{
//do something
}

@Corniel
Copy link
Contributor

Corniel commented Mar 20, 2019

This correct. You just can add the item without first trying if it is already there.

@Evangelink Evangelink changed the title Quality check misunderstands 'Dictionary.TryAdd' Fix S4158: Rule should consider .NetCore 2.0+ 'Dictionary.TryAdd' method Mar 21, 2019
@Evangelink Evangelink self-assigned this Mar 21, 2019
@Evangelink Evangelink added Type: Improvement Area: C# C# rules related issues. labels Mar 21, 2019
@Evangelink Evangelink added this to the 7.12 milestone Mar 21, 2019
@Evangelink
Copy link
Contributor

Hi there,

Thank you for the feedback!
I confirm that I can reproduce the issue and that this is a False Positive on our side. The problem is that we actually don't cover this TryAdd method as it is a .NetCore specific method.

I'll create, shortly, a PR to fix this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues.
Projects
None yet
Development

No branches or pull requests

3 participants