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 S1172: code fix does not delete unused parameter in function call #9534

Closed
wants to merge 3 commits into from

Conversation

sebastien-marichal
Copy link
Contributor

Fixes #8187

It does not support changing usage outside of the current SyntaxTree (e.g.: in case of usage in partial class).

@sebastien-marichal
Copy link
Contributor Author

Low coverage is due to defensive coding.
I'm not sure I can improve; @Tim-Pohlmann, if you have any ideas, I'm all ears.

@denis-troller
Copy link

Can we make sure we find a way to deal with cases where the argument to remove has a side effect?

//...
MyFunction(a, i++);
//... use I later
//...

public void MyFunction(int a, int b)
{
// ... only use a
}

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review 1

return Task.CompletedTask;
arguments = callers
.SelectMany(x => x.Locations)
.Select(x => GetArgumentFromLocation(x, parameterIndex));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work with out of order named arguments.

_ =>
{
var newRoot = root.RemoveNodes(
[parameter, ..arguments.Where(x => x is not null)],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the Where to the assignment of arguments.

Comment on lines 45 to 61
if (parameter?.Parent.Parent is BaseMethodDeclarationSyntax methodDeclaration)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var parameter = root.FindNode(diagnosticSpan, getInnermostNodeForTie: true) as ParameterSyntax;
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken);
var parameterIndex = methodDeclaration.ParameterList.Parameters.IndexOf(parameter);

if (!bool.Parse(diagnostic.Properties[MethodParameterUnused.IsRemovableKey]))
if (parameterIndex >= 0 && model.GetDeclaredSymbol(methodDeclaration, context.CancellationToken) is { } methodSymbol)
{
return Task.CompletedTask;
}
var callers = await SymbolFinder.FindCallersAsync(
methodSymbol,
context.Document.Project.Solution,
ImmutableHashSet.Create(context.Document),
context.CancellationToken);

context.RegisterCodeFix(
Title,
c =>
{
var newRoot = root.RemoveNode(
parameter,
SyntaxRemoveOptions.KeepLeadingTrivia | SyntaxRemoveOptions.AddElasticMarker);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
},
context.Diagnostics);

return Task.CompletedTask;
arguments = callers
.SelectMany(x => x.Locations)
.Select(x => GetArgumentFromLocation(x, parameterIndex));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you move it to a method you can call it directly from the RemoveNodes invocation.

Comment on lines 649 to 661
// https://github.com/SonarSource/sonar-dotnet/issues/8187
public class Repro_8187_CodeFix
{
void Method()
{
DoSomething(21);
}

void DoSomething(int a) // Fixed
{
Console.WriteLine(a);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test with named arguments, which are out of order. Also some tests with optional arguments.

@sebastien-marichal
Copy link
Contributor Author

As discussed, as arguments can include very complex expressions, I am only removing argument expressions that do no updates whatsoever.

Copy link

sonarcloud bot commented Jul 19, 2024

Copy link

sonarcloud bot commented Jul 19, 2024

Quality Gate Failed Quality Gate failed for 'SonarAnalyzer for .NET'

Failed conditions
87.2% Coverage on New Code (required ≥ 95%)

See analysis details on SonarCloud

Copy link
Contributor

@mary-georgiou-sonarsource mary-georgiou-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Martin mentioned we should try to get the full solution and change all the invocations if possible.

@sebastien-marichal sebastien-marichal deleted the sma/s1172-code-fix branch September 23, 2024 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S1172: code fix does not delete unused parameter in function call
4 participants