Skip to content

Commit

Permalink
Fix missing 'equivalenceKey' warning for code fixes (#7554)
Browse files Browse the repository at this point in the history
  • Loading branch information
csaba-sagi-sonarsource authored Jul 14, 2023
1 parent 511bb35 commit 4a09955
Show file tree
Hide file tree
Showing 97 changed files with 3,018 additions and 1,089 deletions.
4 changes: 0 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,6 @@ dotnet_diagnostic.CA1822.severity = warning # Increase visibility for Member 'xx
dotnet_diagnostic.CS8785.severity = error # Do not hide root cause for: Generator 'xxx' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'xxx' with message 'xxx'
dotnet_diagnostic.RS2008.severity = none # Enable analyzer release tracking - we don't use the release tracking analyzer

# This should be removed in https://github.com/SonarSource/sonar-dotnet/issues/3364
# RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer.
dotnet_diagnostic.RS1010.severity = none

# ----------------------------------------------------------------------------------------------------------------------
# SyleCop.Analyzers rules - note that the URLs below are for tag 1.1.118
# ----------------------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Formatting;

Expand All @@ -36,7 +35,7 @@ public override ImmutableArray<string> FixableDiagnosticIds
}
}

protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
protected override Task RegisterCodeFixesAsync(SyntaxNode root, SonarCodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
Expand All @@ -47,25 +46,24 @@ protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext c
}

context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var expression = syntaxNode.Operand.RemoveParentheses();
var newBinary = ChangeOperator((BinaryExpressionSyntax)expression);
Title,
c =>
{
var expression = syntaxNode.Operand.RemoveParentheses();
var newBinary = ChangeOperator((BinaryExpressionSyntax)expression);
if (syntaxNode.Parent is ExpressionSyntax &&
!(syntaxNode.Parent is AssignmentExpressionSyntax))
{
newBinary = SyntaxFactory.ParenthesizedExpression(newBinary);
}
if (syntaxNode.Parent is ExpressionSyntax &&
!(syntaxNode.Parent is AssignmentExpressionSyntax))
{
newBinary = SyntaxFactory.ParenthesizedExpression(newBinary);
}
var newRoot = root.ReplaceNode(
syntaxNode,
newBinary.WithAdditionalAnnotations(Formatter.Annotation));
var newRoot = root.ReplaceNode(
syntaxNode,
newBinary.WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
},
context.Diagnostics);

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Formatting;

Expand All @@ -31,7 +30,7 @@ public sealed class BooleanLiteralUnnecessaryCodeFix : SonarCodeFix
public override ImmutableArray<string> FixableDiagnosticIds =>
ImmutableArray.Create(BooleanLiteralUnnecessary.DiagnosticId);

protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
protected override Task RegisterCodeFixesAsync(SyntaxNode root, SonarCodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
Expand Down Expand Up @@ -87,72 +86,66 @@ protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext c
return Task.CompletedTask;
}

private static void RegisterForStatementConditionRemoval(CodeFixContext context, SyntaxNode root, ForStatementSyntax forStatement) =>
private static void RegisterForStatementConditionRemoval(SonarCodeFixContext context, SyntaxNode root, ForStatementSyntax forStatement) =>
context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var newRoot = root.ReplaceNode(
forStatement,
forStatement.WithCondition(null).WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
Title,
c =>
{
var newRoot = root.ReplaceNode(
forStatement,
forStatement.WithCondition(null).WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
},
context.Diagnostics);

private static void RegisterBinaryExpressionRemoval(CodeFixContext context, SyntaxNode root, LiteralExpressionSyntax literal, BinaryExpressionSyntax binaryParent)
private static void RegisterBinaryExpressionRemoval(SonarCodeFixContext context, SyntaxNode root, LiteralExpressionSyntax literal, BinaryExpressionSyntax binaryParent)
{
var otherNode = binaryParent.Left.RemoveParentheses().Equals(literal)
? binaryParent.Right
: binaryParent.Left;

context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var newExpression = GetNegatedExpression(otherNode);
var newRoot = root.ReplaceNode(binaryParent, newExpression
.WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
Title,
c =>
{
var newExpression = GetNegatedExpression(otherNode);
var newRoot = root.ReplaceNode(binaryParent, newExpression
.WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
},
context.Diagnostics);
}

private static void RegisterConditionalExpressionRewrite(CodeFixContext context, SyntaxNode root, LiteralExpressionSyntax literal, ConditionalExpressionSyntax conditionalParent) =>
private static void RegisterConditionalExpressionRewrite(SonarCodeFixContext context, SyntaxNode root, LiteralExpressionSyntax literal, ConditionalExpressionSyntax conditionalParent) =>
context.RegisterCodeFix(
CodeAction.Create(
Title,
c => Task.FromResult(RewriteConditional(context.Document, root, literal, conditionalParent))),
Title,
c => Task.FromResult(RewriteConditional(context.Document, root, literal, conditionalParent)),
context.Diagnostics);

private static void RegisterBooleanInversion(CodeFixContext context, SyntaxNode root, LiteralExpressionSyntax literal) =>
private static void RegisterBooleanInversion(SonarCodeFixContext context, SyntaxNode root, LiteralExpressionSyntax literal) =>
context.RegisterCodeFix(
CodeAction.Create(
Title,
c => Task.FromResult(RemovePrefixUnary(context.Document, root, literal))),
Title,
c => Task.FromResult(RemovePrefixUnary(context.Document, root, literal)),
context.Diagnostics);

private static void RegisterConditionalExpressionRemoval(CodeFixContext context, SyntaxNode root, ConditionalExpressionSyntax conditional) =>
private static void RegisterConditionalExpressionRemoval(SonarCodeFixContext context, SyntaxNode root, ConditionalExpressionSyntax conditional) =>
context.RegisterCodeFix(
CodeAction.Create(
Title,
c => Task.FromResult(RemoveConditional(context.Document, root, conditional))),
Title,
c => Task.FromResult(RemoveConditional(context.Document, root, conditional)),
context.Diagnostics);

private static void RegisterBinaryExpressionReplacement(CodeFixContext context, SyntaxNode root, SyntaxNode syntaxNode, BinaryExpressionSyntax binary) =>
private static void RegisterBinaryExpressionReplacement(SonarCodeFixContext context, SyntaxNode root, SyntaxNode syntaxNode, BinaryExpressionSyntax binary) =>
context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var keepThisNode = FindNodeToKeep(binary);
var newRoot = root.ReplaceNode(syntaxNode, keepThisNode
.WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
Title,
c =>
{
var keepThisNode = FindNodeToKeep(binary);
var newRoot = root.ReplaceNode(syntaxNode, keepThisNode
.WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
},
context.Diagnostics);

private static SyntaxNode FindNodeToKeep(BinaryExpressionSyntax binary)
Expand Down
14 changes: 3 additions & 11 deletions analyzers/src/SonarAnalyzer.CSharp/Rules/CatchRethrowCodeFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Formatting;

Expand All @@ -28,15 +27,9 @@ namespace SonarAnalyzer.Rules.CSharp
public sealed class CatchRethrowCodeFix : SonarCodeFix
{
internal const string Title = "Remove redundant catch";
public override ImmutableArray<string> FixableDiagnosticIds
{
get
{
return ImmutableArray.Create(CatchRethrow.DiagnosticId);
}
}
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CatchRethrow.DiagnosticId);

protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
protected override Task RegisterCodeFixesAsync(SyntaxNode root, SonarCodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
Expand All @@ -48,13 +41,12 @@ protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext c
}

context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var newRoot = CalculateNewRoot(root, syntaxNode, tryStatement);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
},
context.Diagnostics);

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;

namespace SonarAnalyzer.Rules.CSharp
Expand All @@ -29,7 +28,7 @@ public sealed class CheckFileLicenseCodeFix : SonarCodeFix
internal const string Title = "Add or update license header";
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CheckFileLicense.DiagnosticId);

protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
protected override Task RegisterCodeFixesAsync(SyntaxNode root, SonarCodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
if (!diagnostic.Properties.Any() ||
Expand All @@ -48,14 +47,13 @@ protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext c
var syntaxNode = root.FindNode(diagnosticSpan);

context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var fileHeaderTrivias = CreateFileHeaderTrivias(diagnostic.Properties[CheckFileLicense.HeaderFormatPropertyKey]);
var newRoot = root.ReplaceNode(syntaxNode, syntaxNode.WithLeadingTrivia(fileHeaderTrivias));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
Title,
c =>
{
var fileHeaderTrivias = CreateFileHeaderTrivias(diagnostic.Properties[CheckFileLicense.HeaderFormatPropertyKey]);
var newRoot = root.ReplaceNode(syntaxNode, syntaxNode.WithLeadingTrivia(fileHeaderTrivias));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
},
context.Diagnostics);

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Formatting;

Expand All @@ -33,7 +32,7 @@ public sealed class CollectionEmptinessCheckingCodeFix : SonarCodeFix

private readonly CSharpFacade language = CSharpFacade.Instance;

protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
protected override Task RegisterCodeFixesAsync(SyntaxNode root, SonarCodeFixContext context)
{
if (root.FindNode(context.Diagnostics.First().Location.SourceSpan)?.FirstAncestorOrSelf<BinaryExpressionSyntax>() is { } binary)
{
Expand All @@ -52,12 +51,13 @@ protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext c
return Task.CompletedTask;
}

public static void Simplify(SyntaxNode root, ExpressionSyntax expression, ExpressionSyntax countExpression, CountComparisonResult comparisonResult, CodeFixContext context) =>
public static void Simplify(SyntaxNode root, ExpressionSyntax expression, ExpressionSyntax countExpression, CountComparisonResult comparisonResult, SonarCodeFixContext context) =>
context.RegisterCodeFix(
CodeAction.Create(Title, c => Replacement(root, expression, (InvocationExpressionSyntax)countExpression, comparisonResult, context)),
Title,
c => Replacement(root, expression, (InvocationExpressionSyntax)countExpression, comparisonResult, context),
context.Diagnostics);

private static Task<Document> Replacement(SyntaxNode root, ExpressionSyntax expression, InvocationExpressionSyntax count, CountComparisonResult comparison, CodeFixContext context)
private static Task<Document> Replacement(SyntaxNode root, ExpressionSyntax expression, InvocationExpressionSyntax count, CountComparisonResult comparison, SonarCodeFixContext context)
{
var any = IsExtension(count)
? AnyFromExtension(count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;

namespace SonarAnalyzer.Rules.CSharp;
Expand All @@ -31,17 +29,17 @@ public sealed class CommentedOutCodeCodeFix : SonarCodeFix

public override FixAllProvider GetFixAllProvider() => null;

protected override Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
protected override Task RegisterCodeFixesAsync(SyntaxNode root, SonarCodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var comment = root.FindTrivia(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);

if (comment.IsKind(SyntaxKind.SingleLineCommentTrivia) || IsCode(comment))
{
context.RegisterCodeFix(CodeAction.Create(
context.RegisterCodeFix(
CommentedOutCode.MessageFormat,
c => new Context(comment).ChangeDocument(context.Document, root)),
diagnostic);
c => new Context(comment).ChangeDocument(context.Document, root),
context.Diagnostics);
}
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Formatting;

Expand All @@ -31,7 +30,7 @@ public sealed class ConditionalSimplificationCodeFix : SonarCodeFix

public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(ConditionalSimplification.DiagnosticId);

protected override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
protected override async Task RegisterCodeFixesAsync(SyntaxNode root, SonarCodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var oldNode = root.FindNode(diagnostic.Location.SourceSpan);
Expand All @@ -44,14 +43,17 @@ protected override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixCon
var newNode = Simplify(diagnostic, semanticModel, oldNode);
if (newNode != null)
{
context.RegisterCodeFix(CodeAction.Create(Title, c =>
{
var replacement = newNode.WithTriviaFrom(oldNode).WithAdditionalAnnotations(Formatter.Annotation);
context.RegisterCodeFix(
Title,
c =>
{
var replacement = newNode.WithTriviaFrom(oldNode).WithAdditionalAnnotations(Formatter.Annotation);
var newRoot = root.ReplaceNode(oldNode, replacement);
var newRoot = root.ReplaceNode(oldNode, replacement);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}), context.Diagnostics);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
},
context.Diagnostics);
}
}

Expand Down
Loading

0 comments on commit 4a09955

Please sign in to comment.