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 RCS1235 #1047

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix ([RCS1077](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1077.md)) ([#1023](https://github.com/josefpihrt/roslynator/pull/1023)).
- Fix ([RCS1097](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1097.md)) ([#1037](https://github.com/JosefPihrt/Roslynator/pull/1037)).
- Do not report ([RCS1170](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1170.md)) when `Microsoft.AspNetCore.Components.InjectAttribute` is used ([#1046](https://github.com/JosefPihrt/Roslynator/pull/1046)).
- Fix ([RCS1235](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1235.md)) ([#1047](https://github.com/JosefPihrt/Roslynator/pull/1047)).

## [4.2.0] - 2022-11-27

Expand Down
27 changes: 22 additions & 5 deletions src/Analyzers/CSharp/Analysis/OptimizeMethodCallAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslynator.CSharp.Syntax;
using Roslynator.CSharp.SyntaxWalkers;

namespace Roslynator.CSharp.Analysis;

Expand Down Expand Up @@ -357,11 +358,27 @@ public static void OptimizeAdd(SyntaxNodeAnalysisContext context, in SimpleMembe
&& (block is null
|| SyntaxTriviaAnalysis.IsExteriorTriviaEmptyOrWhitespace(block.OpenBraceToken)))
{
DiagnosticHelpers.ReportDiagnostic(
context,
DiagnosticRules.OptimizeMethodCall,
invocationInfo.Name,
"Add");
var forEachVariableSymbol = context.SemanticModel.GetDeclaredSymbol(forEachStatement, context.CancellationToken) as ILocalSymbol;

if (forEachVariableSymbol is not null)
{
ContainsLocalOrParameterReferenceWalker walker = ContainsLocalOrParameterReferenceWalker.GetInstance(forEachVariableSymbol, context.SemanticModel, context.CancellationToken);

walker.Visit(invocationInfo.Expression);

bool containsReference = walker.Result;

ContainsLocalOrParameterReferenceWalker.Free(walker);

if (!containsReference)
{
DiagnosticHelpers.ReportDiagnostic(
context,
DiagnosticRules.OptimizeMethodCall,
invocationInfo.Name,
"Add");
}
}

break;
}
Expand Down
24 changes: 24 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1235OptimizeMethodCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,30 @@ void M()
string s = string.Join(""x"", new object[] { """" });
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeMethodCall)]
public async Task TestNoDiagnostic_ForEachVariableUsedInExpression()
{
await VerifyNoDiagnosticAsync(@"
using System.Collections.Generic;

class C
{
(List<int>, List<int>) M(List<int> list)
{
var positive = new List<int>();
var negative = new List<int>();

foreach (int i in list)
{
((i >= 0) ? positive : negative).Add(i);
}

return (positive, negative);
}
}
");
}
}