-
Notifications
You must be signed in to change notification settings - Fork 228
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 missing 'equivalenceKey' warning for code fixes #7554
The head ref may contain hidden characters: "\u010Daba/SonarCodeFixContext"
Conversation
RedundantDeclarationCodeFix needed a bigger refactoring to adapt it to the new registration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First round.
I don't like the namespace choice for the SonarContext. It should be changed.
analyzers/src/SonarAnalyzer.Common/CodeFixContext/SonarCodeFixContext.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/CodeFixContext/SonarCodeFixContext.cs
Outdated
Show resolved
Hide resolved
@@ -184,7 +184,7 @@ private static SyntaxNodeOrToken GetReportedElement(Diagnostic diagnostic, Synta | |||
private static SyntaxNode RemoveAnnotationIfExists(SyntaxNode root, SyntaxAnnotation annotation) | |||
{ | |||
var element = root.GetAnnotatedNodesAndTokens(annotation).FirstOrDefault(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit odd that we do not loop here instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, and I would not do the change in this PR, this one is already huge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you make a note or an issue? Also, #7554 (comment) might be worth keeping documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forStatement, | ||
forStatement.WithCondition(null).WithAdditionalAnnotations(Formatter.Annotation)); | ||
|
||
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe as a follow up PR:
- All code fixer ignore the CancelationToken (as far as I can tell)
- All code fixer do
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
We may add an overload for RegisterCodeFix
public void RegisterCodeFix(string title, Func<SyntaxNode>> createNewRoot, ImmutableArray<Diagnostic> diagnostics) =>
context.RegisterCodeFix(CodeAction.Create(title, c => Task.FromResult(context.Document.WithSyntaxRoot(createNewRoot())), title), diagnostics);
Note: There might be problems with the lambda capture of context.
This call would be simplified to
() => root.ReplaceNode(
forStatement,
forStatement.WithCondition(null).WithAdditionalAnnotations(Formatter.Annotation));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I finish the sprint a bit earlier I will create a PR for it.
analyzers/src/SonarAnalyzer.CSharp/Rules/GetHashCodeMutableCodeFix.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/UnchangedLocalVariablesShouldBeConstCodeFix.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestFramework/CodeFixVerifier.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/CodeFixContext/SonarCodeFixContext.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. There is one code smell about to ignore. I don't see how equivalence for the context is relevant.
analyzers/src/SonarAnalyzer.Common/Common/FixAllProviders/DocumentBasedFixAllProvider.cs
Show resolved
Hide resolved
a27e3b4
to
fe98e29
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nitpicks in the UTs. Otherwise good.
analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarCodeFixContextTest.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarCodeFixContextTest.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarCodeFixContextTest.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarCodeFixContextTest.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/AnalysisContext/SonarCodeFixContextTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.UnitTest/Common/FixAllProviders/DocumentBasedFixAllProviderTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.UnitTest/Common/FixAllProviders/DocumentBasedFixAllProviderTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.UnitTest/Common/FixAllProviders/DocumentBasedFixAllProviderTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.UnitTest/Common/FixAllProviders/DocumentBasedFixAllProviderTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.UnitTest/Common/FixAllProviders/DocumentBasedFixAllProviderTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some optional suggestions. LGTM.
var literal = syntaxTree.GetRoot().DescendantNodesAndSelf().OfType<LiteralExpressionSyntax>().Single(); | ||
var cancellationToken = new CancellationToken(true); | ||
var diagnostic = Diagnostic.Create(new DiagnosticDescriptor("1", "title", "format", "category", DiagnosticSeverity.Hidden, false), literal.GetLocation()); | ||
var sonarCodefix = new SonarCodeFixContext(new CodeFixContext(document, diagnostic, (_, _) => Console.WriteLine("Hello world"), cancellationToken)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var sonarCodefix = new SonarCodeFixContext(new CodeFixContext(document, diagnostic, (_, _) => Console.WriteLine("Hello world"), cancellationToken)); | |
var sonarCodefix = new SonarCodeFixContext(new CodeFixContext(document, diagnostic, (_, _) => { }, cancellationToken)); |
public async Task GetFixAsync_ForSupportedScope_HasCorrectTitle(FixAllScope scope, string expectedTitle) | ||
{ | ||
var codeFix = new DummyCodeFixCS(); | ||
var document = CreateProject().FindDocument(Path.GetFileName("MyFile1.cs")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some more Path.GetFileName
in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was sure I removed all. Sorry.
.AddSnippet(@"int number1 = 1;", "MyFile1.cs") | ||
.AddSnippet(@"int number2 = 2;", "MyFile2.cs"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two files with top-level statements don't compile, right? You may want to fix this.
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
Fixes #3364