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 RCS1179 #1129

Merged
merged 11 commits into from
Jul 26, 2023
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix [RCS0005](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0005) ([#1114](https://github.com/JosefPihrt/Roslynator/pull/1114)).
- Fix [RCS1176](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1176.md) ([#1122](https://github.com/JosefPihrt/Roslynator/pull/1122)).
- Fix [RCS1085](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1085.md) ([#1120](https://github.com/josefpihrt/roslynator/pull/1120)).
- Fix [RCS1179](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1179.md) ([#1129](https://github.com/JosefPihrt/Roslynator/pull/1129)).

## [4.3.0] - 2023-04-24

Expand Down
12 changes: 8 additions & 4 deletions src/Analyzers/CSharp/Analysis/UnnecessaryAssignmentAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
if (!IsLocalDeclaredInScopeOrNonRefOrOutParameterOfEnclosingSymbol(symbol, statementsInfo.Parent, semanticModel, cancellationToken))
return;

ITypeSymbol returnTypeSymbol = semanticModel.GetTypeInfo(expression, cancellationToken).Type;
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved

foreach (IfStatementOrElseClause ifOrElse in ifStatement.AsCascade())
{
StatementSyntax statement = ifOrElse.Statement;
Expand All @@ -78,7 +80,7 @@ private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
statement = ((BlockSyntax)statement).Statements.LastOrDefault();

if (!statement.IsKind(SyntaxKind.ThrowStatement)
&& !IsSymbolAssignedInStatement(symbol, statement, semanticModel, cancellationToken))
&& !IsSymbolAssignedInStatementWithCorrectType(symbol, statement, semanticModel, returnTypeSymbol, cancellationToken))
{
return;
}
Expand Down Expand Up @@ -113,6 +115,7 @@ private static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
CancellationToken cancellationToken = context.CancellationToken;

ISymbol symbol = semanticModel.GetSymbol(expression, cancellationToken);
ITypeSymbol returnTypeSymbol = semanticModel.GetTypeInfo(expression, cancellationToken).Type;

if (symbol is null)
return;
Expand All @@ -136,7 +139,7 @@ private static void AnalyzeSwitchStatement(SyntaxNodeAnalysisContext context)
case SyntaxKind.BreakStatement:
{
if (statements.Count == 1
|| !IsSymbolAssignedInStatement(symbol, statements.LastButOne(), semanticModel, cancellationToken))
|| !IsSymbolAssignedInStatementWithCorrectType(symbol, statements.LastButOne(), semanticModel, returnTypeSymbol, cancellationToken))
{
return;
}
Expand Down Expand Up @@ -204,11 +207,12 @@ private static bool IsLocalDeclaredInScopeOrNonRefOrOutParameterOfEnclosingSymbo
return false;
}

private static bool IsSymbolAssignedInStatement(ISymbol symbol, StatementSyntax statement, SemanticModel semanticModel, CancellationToken cancellationToken)
private static bool IsSymbolAssignedInStatementWithCorrectType(ISymbol symbol, StatementSyntax statement, SemanticModel semanticModel, ITypeSymbol typeSymbol, CancellationToken cancellationToken)
{
SimpleAssignmentStatementInfo assignmentInfo = SyntaxInfo.SimpleAssignmentStatementInfo(statement);

return assignmentInfo.Success
&& SymbolEqualityComparer.Default.Equals(semanticModel.GetSymbol(assignmentInfo.Left, cancellationToken), symbol);
&& SymbolEqualityComparer.Default.Equals(semanticModel.GetSymbol(assignmentInfo.Left, cancellationToken), symbol)
&& typeSymbol.Equals(semanticModel.GetTypeInfo(assignmentInfo.Right, cancellationToken).Type);
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
}
}
61 changes: 61 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1179UnnecessaryAssignmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,67 @@ int M()
}
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryAssignment)]
public async Task Test_NoDiagnostic_ForPolymorphicIf()
{
await VerifyNoDiagnosticAsync(
@"
class A {}
class B {}
class C
{
void M()
{
var fun = (bool flag) =>
{
object x;
if (flag)
{
x = new A();
}
else
{
x = new B();
}

return x;
};
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessaryAssignment)]
public async Task Test_NoDiagnostic_ForPolymorphicSwitch()
{
await VerifyNoDiagnosticAsync(
@"
class A {}
class B {}
class C
{
void M()
{
var fun = (object o) =>
{
object x;
switch(o)
{
case int:
x = new A();
break;
default:
x = new B();
break;
}

return x;
};
}
}
");
}
}