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 RCS1146 #1098

Merged
merged 6 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix [RCS1169](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1169.md) ([#1092](https://github.com/JosefPihrt/Roslynator/pull/1092)).
- Recognize more shapes of IAsyncEnumerable as being Async ([RCS1047](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1047.md)) ([#1084](https://github.com/josefpihrt/roslynator/pull/1084)).
- Fix [RCS1197](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1197.md) ([#1093](https://github.com/JosefPihrt/Roslynator/pull/1093)).
- Fix [RCS1146](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1146.md) ([#1098](https://github.com/JosefPihrt/Roslynator/pull/1098)).

## [4.3.0] - 2023-04-24

Expand Down
8 changes: 8 additions & 0 deletions src/Analyzers/CSharp/Analysis/UseConditionalAccessAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ private static bool ValidateRightExpression(
case SyntaxKind.GreaterThanOrEqualExpression:
case SyntaxKind.EqualsExpression:
{
var leftTypeSymbol = semanticModel.GetTypeSymbol(((BinaryExpressionSyntax)expression).Left);
if (leftTypeSymbol.IsValueType && leftTypeSymbol.SpecialType is SpecialType.None)
Copy link
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.

{
// If the LHS is a ValueTypes then making the expression conditional would change the type of the expression
// and hence we would call a different overload (a valid overload may not exists)
return false;
}

expression = ((BinaryExpressionSyntax)expression)
.Right?
.WalkDownParentheses();
Expand Down
20 changes: 20 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1146UseConditionalAccessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,26 @@ void M()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConditionalAccess)]
public async Task TestNoDiagnostic_LogicalAnd_ValueTypeFieldAccess()
{
await VerifyNoDiagnosticAsync(@"
struct Foo
{
public static bool operator ==(Foo left, string right) => left.Equals(right);
public static bool operator !=(Foo left, string right) => !(left == right);
}
class C
{
public Foo F { get; }
void M(C c)
{
if (c != null && c.F == ""someStr"") { }
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConditionalAccess)]
public async Task TestNoDiagnostic_LogicalAnd_NullableType()
{
Expand Down