Roslyn: IDE0060 (remove unused parameter) should not fire for MSTest ClassInitialize-annotated method

Created on 17 Apr 2019  路  3Comments  路  Source: dotnet/roslyn

Version Used:
Visual Studio Professional 16.0.1

Steps to Reproduce:
```c#
[TestClass]
public class TestClass1
{
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Console.WriteLine("ClassInitialize");
}

[TestMethod]
public void Test1()
{
}

}
```

Expected Behavior:

  • No message/error

Actual Behavior:

  • Get the following message: "IDE0060 Remove unused parameter 'context' if it is not part of a shipped public API"
  • If I remove the context parameter and run the test, I get the following error from MSTest v1: "TestClass1.ClassInitialize has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext"
Area-IDE Bug IDE-CodeStyle Resolution-By Design

Most helpful comment

I believe the correct approach here is to mark the parameter itself as a discard parameter, using special discard names, we now include this in diagnostic's description:

Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.

All 3 comments

Based on the review of #34441, the current recommended resolution for this situation is to add the following statement as the first line of your ClassInitialize method:

// context is required by MSTest but not used in this case
_ = context;

I believe the correct approach here is to mark the parameter itself as a discard parameter, using special discard names, we now include this in diagnostic's description:

Avoid unused paramereters in your code. If the parameter cannot be removed, then change its name so it starts with an underscore and is optionally followed by an integer, such as '_', '_1', '_2', etc. These are treated as special discard symbol names.

There's some nice typos in that rule and its name :p.
Thanks guys! Was wondering how to handle multiple discard parameters, that _1, _2, helps

Was this page helpful?
0 / 5 - 0 ratings