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

Additional "When to suppress warnings" should be demonstrated. CA1853 matching is too loose. #44193

Open
ciphernemo opened this issue Jan 9, 2025 · 0 comments

Comments

@ciphernemo
Copy link

ciphernemo commented Jan 9, 2025

Type of issue

Incomplete list of reasons to suppress this warning.

Description

The only item under "When to suppress warnings" is "It's safe to suppress a warning if performance isn't a concern." That's not the only reason.

It's worthwhile to check a Dictionary collection for .ContainsKey() before a .Remove() of that item if we're planning to alter the order of the Dictionary collection by adding the updated item at the end on identical match.

Example:

// "seed" is generated as an int earlier
Dictionary<int, object[]> a = [];
// ... population of "a" from other code ...
if (a.ContainsKey(seed))
{
	a.Remove(seed);
	Dictionary<int, object[]> a2 = [a];
	// alternatively: Dictionary<int, object[]> a2 = []; for (int i = 0; i < a.Count; i++) { a2.Add(a.ElementAt(i).Key, a.ElementAt(i).Value); }
	a.Clear();
	a = a2;
}
a.Add(seed, object[])

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1853

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1853.md

Document Version Independent Id

e01a0926-70a5-177f-017f-46b93b6c1463

Article author

@gewarren

Metadata

  • ID: 4fc29d18-6855-2bf4-b53c-856c4cde4499
  • Service: dotnet-fundamentals

Related Issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant