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 unused param (IDE0060) false positive #58480

Merged
merged 2 commits into from
Dec 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,36 @@ public C(int [|i|])
}");
}

[WorkItem(56317, "https://github.com/dotnet/roslyn/issues/56317")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task NotImplementedException_NoDiagnostic4()
{
await TestDiagnosticMissingAsync(
@"using System;

class C
{
private int Goo(int [|i|])
=> throw new NotImplementedException();
}");
}

[WorkItem(56317, "https://github.com/dotnet/roslyn/issues/56317")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task NotImplementedException_NoDiagnostic5()
{
await TestDiagnosticMissingAsync(
@"using System;

class C
{
private int Goo(int [|i|])
{
throw new NotImplementedException();
}
}");
}

[WorkItem(41236, "https://github.com/dotnet/roslyn/issues/41236")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task NotImplementedException_MultipleStatements1()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,18 @@ static bool IsSingleThrowNotImplementedOperation(IOperation firstBlock)
if (firstOp == null)
return false;

// unwrap: { throw new NYI(); }
if (firstOp is IExpressionStatementOperation expressionStatement)
{
// unwrap: { throw new NYI(); }
firstOp = expressionStatement.Operation;
}
else if (firstOp is IReturnOperation returnOperation)
{
// unwrap: 'int M(int p) => throw new NYI();'
// For this case, the throw operation is wrapped within a conversion operation to 'int',
// which in turn is wrapped within a return operation.
firstOp = returnOperation.ReturnedValue.WalkDownConversion();
}

// => throw new NotImplementedOperation(...)
return IsThrowNotImplementedOperation(notImplementedExceptionType, firstOp);
Expand All @@ -177,7 +186,7 @@ static bool IsSingleThrowNotImplementedOperation(IOperation firstBlock)
return firstOp;
}

static bool IsThrowNotImplementedOperation(INamedTypeSymbol notImplementedExceptionType, IOperation operation)
static bool IsThrowNotImplementedOperation(INamedTypeSymbol notImplementedExceptionType, IOperation? operation)
=> operation is IThrowOperation throwOperation &&
UnwrapImplicitConversion(throwOperation.Exception) is IObjectCreationOperation objectCreation &&
notImplementedExceptionType.Equals(objectCreation.Type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,20 @@ public static bool IsNumericLiteral(this IOperation operation)

public static bool IsNullLiteral(this IOperation operand)
=> operand is ILiteralOperation { ConstantValue: { HasValue: true, Value: null } };

/// <summary>
/// Walks down consecutive conversion operations until an operand is reached that isn't a conversion operation.
/// </summary>
/// <param name="operation">The starting operation.</param>
/// <returns>The inner non conversion operation or the starting operation if it wasn't a conversion operation.</returns>
public static IOperation? WalkDownConversion(this IOperation? operation)
{
while (operation is IConversionOperation conversionOperation)
Copy link
Member

Choose a reason for hiding this comment

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

do you only need to do this if it's implicit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Likely for the case in this bug, but probably we want to do it for all conversions for this extension method.

{
operation = conversionOperation.Operand;
}

return operation;
}
}
}