Skip to content

Fix RCS1146#1098

Merged
josefpihrt merged 6 commits into
dotnet:mainfrom
jamesHargreaves12:fix/RCS1146
Jun 27, 2023
Merged

Fix RCS1146#1098
josefpihrt merged 6 commits into
dotnet:mainfrom
jamesHargreaves12:fix/RCS1146

Conversation

@jamesHargreaves12

Copy link
Copy Markdown
Contributor

If we have the following expression and the field F is a value type then we cannot apply RCS1146 as this will mean that a different overload of == is used (and one may not exist).

c != null && c.F == "someStr"

We know that the == operator exists and works correctly for in-built value types like int so the diagnostic can still be suggested for those cases.

case SyntaxKind.EqualsExpression:
{
var leftTypeSymbol = semanticModel.GetTypeSymbol(((BinaryExpressionSyntax)expression).Left);
if (leftTypeSymbol.IsValueType && leftTypeSymbol.SpecialType is SpecialType.None)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would rather use CSharpFacts.IsPredefinedType instead of is SpecialTypen.None.

Comment thread src/Analyzers/CSharp/Analysis/UseConditionalAccessAnalyzer.cs
@josefpihrt josefpihrt merged commit 16ff010 into dotnet:main Jun 27, 2023
@josefpihrt

Copy link
Copy Markdown
Collaborator

👍

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 this pull request may close these issues.

2 participants