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

incorrect suggestion of CA1854 Prefer a "TryGetValue" call over a Dictionary indexer access guarded by a "ContainsKey" check to avoid double lookup #7295

Open
wstaelens opened this issue Apr 17, 2024 · 0 comments · May be fixed by #7309

Comments

@wstaelens
Copy link

Analyzer

Diagnostic ID: CA1854 Prefer a "TryGetValue" call over a Dictionary indexer access guarded by a "ContainsKey" check to avoid double lookup

Analyzer source

latest .net 8 version and vs pro 2022 17.9.6

Describe the bug

    public Dictionary<int, Dictionary<int, bool?>> PermissionsData { get; set; }

 public void Combine(IPermission p) {
  // ...... some logic
     if (p == null)
        return;

      TypePermission otherP = (TypePermission)p;

      foreach (int objId in otherP.PermissionsData.Keys)
      {
        if (PermissionsData.ContainsKey(objId))
          CombineEntityPerms(objId, otherP.PermissionsData[objId]);
        else
          PermissionsData.Add(objId, otherP.PermissionsData[objId]);
      }
// some logic...
}

image

it suggest to do a trygetvalue and use value but doesn't see that PermissionsData.ContainsKey(objId) and otherP.PermissionsData[objId] are different.

@CollinAlpert CollinAlpert linked a pull request May 10, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant