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

S1656: Add support for deconstruction #5784

Merged
merged 6 commits into from
Jun 28, 2022
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
15 changes: 7 additions & 8 deletions analyzers/src/SonarAnalyzer.CSharp/Rules/SelfAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using SonarAnalyzer.Extensions;
using SonarAnalyzer.Helpers;
using StyleCop.Analyzers.Lightup;

Expand All @@ -32,25 +34,22 @@ public sealed class SelfAssignment : SelfAssignmentBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => CSharpFacade.Instance;

protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterSyntaxNodeActionInNonGenerated(c =>
{
var expression = (AssignmentExpressionSyntax) c.Node;
var expression = (AssignmentExpressionSyntax)c.Node;

if (expression.Parent is InitializerExpressionSyntax)
{
return;
}

if (CSharpEquivalenceChecker.AreEquivalent(expression.Left, expression.Right))
foreach (var assigment in expression.MapAssignmentArguments().Where(x => CSharpEquivalenceChecker.AreEquivalent(x.Left, x.Right)))
{
c.ReportIssue(Diagnostic.Create(Rule, c.Node.GetLocation()));
c.ReportIssue(Diagnostic.Create(Rule, assigment.Left.GetLocation(), additionalLocations: new[] { assigment.Right.GetLocation() }));
}
},
SyntaxKind.SimpleAssignmentExpression,
SyntaxKindEx.CoalesceAssignmentExpression);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ namespace SonarAnalyzer.UnitTest.Rules
[TestClass]
public class SelfAssignmentTest
{
private readonly VerifierBuilder builderCS = new VerifierBuilder<CS.SelfAssignment>();
private readonly VerifierBuilder builderVB = new VerifierBuilder<VB.SelfAssignment>();

[TestMethod]
public void SelfAssignment_CSharp() =>
OldVerifier.VerifyAnalyzer(@"TestCases\SelfAssignment.cs",
new CS.SelfAssignment(),
ParseOptionsHelper.FromCSharp8);
builderCS.AddPaths("SelfAssignment.cs").WithOptions(ParseOptionsHelper.FromCSharp8).Verify();

#if NET
[TestMethod]
public void SelfAssignment_CSharp10() =>
OldVerifier.VerifyAnalyzerFromCSharp10Console(@"TestCases\SelfAssignment.CSharp10.cs", new CS.SelfAssignment());
builderCS.AddPaths("SelfAssignment.CSharp10.cs").WithOptions(ParseOptionsHelper.FromCSharp10).WithTopLevelStatements().Verify();
#endif

[TestMethod]
public void SelfAssignment_VisualBasic() =>
OldVerifier.VerifyAnalyzer(@"TestCases\SelfAssignment.vb", new VB.SelfAssignment());
builderVB.AddPaths("SelfAssignment.vb").Verify();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
int x = 42;

(x, var y) = (x, 42); // FN
(x, var y) = (x, 42);
// ^ Noncompliant
// ^ Secondary@-1
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ public SelfAssignment(int Prop1)
{
int? value = null;

value ??= value; // Noncompliant
value ??= value;
// ^^^^^ Noncompliant
// ^^^^^ Secondary@-1
}

public int Prop1 { get; set; }

public void Test()
{
var Prop1 = 5;
Prop1 = Prop1; //Noncompliant
// ^^^^^^^^^^^^^
Prop1 = Prop1;
// ^^^^^ Noncompliant
// ^^^^^ Secondary@-1

Prop1 = 2*Prop1;

var y = 5;
y = /*comment*/ y; //Noncompliant {{Remove or correct this useless self-assignment.}}
y = /*comment*/ y; // Noncompliant {{Remove or correct this useless self-assignment.}}
// Secondary@-1

var x = new SelfAssignment
{
Expand Down