-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Open
Labels
Description
Version Used:
.NET 9.0.308
Steps to Reproduce:
- Activate analysis rule IDE0059, set to warning.
- Create code inside a try block with assignment to a variable that is not used later.
In the snippet and screenshot below, the first assignment to violation1 and to num1 are correctly flagged by IDE0059. The second assignment to num1 and violation1, as well as the assignments to violation2 and num2 should be flagged, but are not. The assignment to num2 is flagged by CS0219 (which should also catch violation2, so that one isn't working properly either.)
public async Task<Violation?> TestMethod(Guid violationId, CancellationToken cancellationToken)
{
var violation1 = await this.violationStore.GetById(violationId, cancellationToken);
int num1 = 0;
try
{
violation1 = await this.violationStore.GetById(violationId, cancellationToken);
num1 = 2;
var violation2 = await this.violationStore.GetById(violationId, cancellationToken);
int num2 = 3;
if (violation1 == null)
{
return null;
}
}
catch (Exception)
{
return null;
}
return violation1;
}Diagnostic Id:
IDE0059: Unnecessary assignment of a value
Expected Behavior: Visual Studio should warn that the assigned value is unused, even in a try block.
Actual Behavior: No warning is issued on the code inside the try block.