Thanks to visit codestin.com
Credit goes to github.com

Skip to content

IDE0059 not working properly inside catch blocks. #81742

@areevesat

Description

@areevesat

Version Used:
.NET 9.0.308

Steps to Reproduce:

  1. Activate analysis rule IDE0059, set to warning.
  2. 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;
}
Image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions