dotnet / dotnet/roslynator

RFE: RR0150, RR0151 (Replace while with ...): Handle 'continue'

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

Description

Roslynator Refactorings 2017 up to and including 2.1.1

When refactoring, and flipping conditional code around, you can sometimes end up with something like

```
int i = 0;
while (i cond1 smth)
{
if (cond2) { /* ... */ }
++i;
continue; // used to be inside "if (!cond2)
}
```

It would be nice if these refactorings noticed the `continue` is a NOP, allowing these two refactorings to run, and remove the (now spurious) `continue`.

Coming to think of it, that could/should probably be turned into an RR of itself - removal of pointless/"dead" code.

Additionally, it could be nice when converting to `for` to allow (though probably not enforce) the loop condition-variable initialization to be placed as such, converting the previous into

```
for (int i = 0; i cond1 smth; ++i)
{
if (cond2) { /* ... */ }
}
```

or perhaps that could be turned into an RR or two of itself, moving the for-loop variable initialization in/out of the for-initialization-expression. (just a brainstorming idea)

While I'm not the least familiar with the Roslynator code, a cursory glance suggest https://github.com/JosefPihrt/Roslynator/blob/d402258408dc8e17d737417e1c268f91be7b5d49/src/Refactorings/CSharp/Refactorings/ReplaceWhileWithForRefactoring.cs#L81-L82

could be the source of not allowing the refactoring? Of course, that's just the detection part...

Contributor guide

Open the contributing guide

Research direction

Start with Refactorings/CSharp/Refactorings/ReplaceWhileWithForRefactoring.cs around lines 81-82, then trace the detection and transformation for RR0150 and RR0151. Determine how a loop containing continue should be handled and whether moving initialization into the for expression belongs in this change; done means the requested refactoring behavior is defined and implemented without the spurious continue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.