dotnet / dotnet/roslynator

Refactoring request: "Promote variable to parent scope/demote variable to child scope"

Open
#404 0 comments 2 reactions 0 assignees View on GitHub
Area-Refactorings Feature Request
Dominant language
C#
Stars
3.5k
Forks
294
Avg merge
2h 30m
Merged PRs (30d)
4

Description

Essentially you have something like:

```csharp
if (someCondition)
{
var foo = bar();
}
```

and now you need to use `foo` outside of the conditional block, so you rewrite the code as:

```csharp
TypeOfFoo foo;

if (someCondition)
{
foo = bar();
}
```

Note that the RHS of the expression remains inside the conditional block; only the declaration moves. Doing this requires two steps: (1) moving the declaration (2) updating the declaration's type, and a refactoring that could do it in a single step would be most helpful. A complementary refactoring to go the other way (creating an anonymous scope to achieve this) would also probably be useful.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.