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

Don't guard Dictionary<K, V>.Remove(key) by ContainsKey(key) #33797

Closed
Tracked by #57797
terrajobst opened this issue Mar 19, 2020 · 10 comments
Closed
Tracked by #57797

Don't guard Dictionary<K, V>.Remove(key) by ContainsKey(key) #33797

terrajobst opened this issue Mar 19, 2020 · 10 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Collections code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@terrajobst
Copy link
Member

The pair can be combined into just the Remove call.

Category: Performance

@terrajobst terrajobst added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections code-analyzer Marks an issue that suggests a Roslyn analyzer labels Mar 19, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Mar 19, 2020
@dotnet dotnet deleted a comment from Dotnet-GitSync-Bot Mar 19, 2020
@jeffhandley jeffhandley added this to the Future milestone Mar 20, 2020
@jeffhandley jeffhandley added the code-fixer Marks an issue that suggests a Roslyn code fixer label Mar 21, 2020
@jeffhandley
Copy link
Member

Estimates:

  • Analyzer: Medium
  • Fixer: Medium

@terrajobst terrajobst removed the untriaged New issue has not been triaged by the area owner label Mar 21, 2020
@carlossanlop
Copy link
Member

Suggested severity: Info

Example

// Before
        public void RemoveValue(Dictionary<string, int> data, string key)
        {​​
            if (data.ContainsKey(key))
                data.Remove(key);
            // or
            if (data.ContainsKey(key))
                data.Remove(key, out var value);
        }​​

// After
        public void RemoveValue(Dictionary<string, int> data, string key)
        {​​
            data.Remove(key);
            // or
            data.Remove(key, out var value1);
        }​​

Avoid triggering the analyzer if there's additional code inside the if statement:

        public void DontTrigger(Dictionary<string, int> data, string key, Dictionary<string, int> otherData)
        {​​
            if (data.ContainsKey(key))
            {
                otherData[key] = data[key]; // Unrelated code
                data.Remove(key);
            }
        }​​

@carlossanlop carlossanlop added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jan 15, 2021
@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Feb 4, 2021
@terrajobst
Copy link
Member Author

terrajobst commented Feb 4, 2021

Video

  • Makes sense.

We could further improve the fixer to handle this as well:

// Before
if (data.ContainsKey(key))
{
    data.Remove(key);
    SomeUnrelatedCode();
}
// After
if (data.Remove(key))
{
    SomeUnrelatedCode();
}

But it's not necessary -- we can start simple and take it from there.

@chucker
Copy link

chucker commented Feb 11, 2021

I have the analyzer mostly ready to go for both C# and VB. No fixer yet, though.

Do you want a PR, or should I first write a fixer?

@carlossanlop
Copy link
Member

Thanks for your help, @chucker! I'm assigning the issue to you. Feel free to submit a PR whenever you get a chance, and we can take a look.

@chucker
Copy link

chucker commented Feb 11, 2021

(The PR linked above does now include the fixer, too.)

@paulomorgado
Copy link
Contributor

is there an issue like this for HashSet<T>?

  • Don't guard HashSet<T>.Remove(item) by HashSet<T>.Contains(item)?
  • Don't guard HashSet<T>.Add(item) by HashSet<T>.Contains(item)?

@carlossanlop
Copy link
Member

@paulomorgado if you can't find any issue, please create an API proposal.

@paulomorgado
Copy link
Contributor

An API proposal????

@chucker
Copy link

chucker commented Oct 13, 2021

@paulomorgado yes — make a new issue over here

See also these guidelines.

@carlossanlop carlossanlop added the in-pr There is an active PR which will close this issue when it is merged label Jul 6, 2022
@jeffhandley jeffhandley modified the milestones: 7.0.0, Future Jul 9, 2022
@buyaa-n buyaa-n closed this as completed Jul 21, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Collections code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

No branches or pull requests

7 participants