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 false positive for CA1854 across instances #7309

Merged
merged 3 commits into from
Oct 16, 2024

Conversation

CollinAlpert
Copy link
Contributor

@CollinAlpert CollinAlpert commented May 10, 2024

Affected analyzer: PreferDictionaryTryMethodsOverContainsKeyGuardAnalyzer
Affected diagnostic IDs: CA1854

This PR fixes an issue where the analyzer considers two dictionaries equal when they come from different instances of a type.

Fixes #7295

@CollinAlpert CollinAlpert requested a review from a team as a code owner May 10, 2024 14:43
Copy link

codecov bot commented May 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.50%. Comparing base (8c173ce) to head (e15cd4a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7309      +/-   ##
==========================================
- Coverage   96.50%   96.50%   -0.01%     
==========================================
  Files        1448     1448              
  Lines      346869   346889      +20     
  Branches    11402    11404       +2     
==========================================
+ Hits       334739   334753      +14     
- Misses       9243     9248       +5     
- Partials     2887     2888       +1     

Comment on lines 612 to 621
string syntax1 = instance1.Syntax
.WithoutTrivia()
.ToString()
.Replace("this.", string.Empty)
.Replace("Me.", string.Empty);
string syntax2 = instance2.Syntax
.WithoutTrivia()
.ToString()
.Replace("this.", string.Empty)
.Replace("Me.", string.Empty);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: SyntaxNode.ToString() doc says Returns the string representation of this node, not including its leading and trailing trivia.

Suggested change
string syntax1 = instance1.Syntax
.WithoutTrivia()
.ToString()
.Replace("this.", string.Empty)
.Replace("Me.", string.Empty);
string syntax2 = instance2.Syntax
.WithoutTrivia()
.ToString()
.Replace("this.", string.Empty)
.Replace("Me.", string.Empty);
string syntax1 = instance1.Syntax
.ToString()
.Replace("this.", string.Empty)
.Replace("Me.", string.Empty);
string syntax2 = instance2.Syntax
.ToString()
.Replace("this.", string.Empty)
.Replace("Me.", string.Empty);

case IPropertyReferenceOperation source when targetReference is IPropertyReferenceOperation target:
return target.Property.Equals(source.Property, SymbolEqualityComparer.Default);
return target.Property.Equals(source.Property, SymbolEqualityComparer.Default) && AreInstancesEqual(source, target);
Copy link
Contributor

@buyaa-n buyaa-n Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect to add something like && source.Instance == target.Instance, but it doesn't work.

Comparing string representation looks bit weird, though don't see any other way (no action needed here)

}

[Fact, WorkItem(7295, "https://github.com/dotnet/roslyn-analyzers/issues/7295")]
public Task WhenDifferentLocalInstancesWithThisQualifierContainingDictionary_NoDiagnostic()
Copy link
Contributor

@buyaa-n buyaa-n Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: The test name doesn't match the with the test, seems forgot to update the name or the test, maybe Local part should be removed and NoDiagnostic => Diagnostic

@CollinAlpert
Copy link
Contributor Author

Thanks for the review, I have applied your remarks!

Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @CollinAlpert!

@buyaa-n buyaa-n merged commit 82cdc84 into dotnet:main Oct 16, 2024
11 checks passed
@CollinAlpert CollinAlpert deleted the issue-7295 branch October 16, 2024 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants