Add else if support to RCS1211#1155
Conversation
| if (ifStatementStatement is not BlockSyntax ifBlock) | ||
| return CSharpFacts.IsJumpStatement(ifStatementStatement.Kind()); | ||
|
|
||
| if (elseClause.Statement is BlockSyntax elseBlock) |
There was a problem hiding this comment.
The diffs from L64 onwards do not change behaviour. They are just reducing nesting / Moving the checks that require a semantic model later.
|
I'm not sure that this proposal fits into this analyzer. The purpose of this analyzer is simply to remove (or let's say unwrap) last else (in if-else sequence). To be honest, I don't think that this analyzer is that much useful. It should serve more like a refactoring when you want to remove last else (this is why default severity is set to hidden/silent). This proposal does something else: it essentially split if-else sequence into sequence of separate 'if' statements. There is actually a refactoring RR0190 which does very similar or same thing. Regarding the current implementation I see two problems:
public async Task Test_UnnecessaryElseIf()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
int M(bool flag, bool flag2)
{
if (flag)
{
return 1;
}
[|else|] if (flag2)
{
M(flag, flag2);
}
else
{
return 2;
}
return 3;
}
}
", @"
class C
{
int M(bool flag, bool flag2)
{
if (flag)
{
return 1;
}
if (flag2)
{
M(flag, flag2);
}
else
{
return 2;
}
return 3;
}
}
");
}My recommendation would be to keep the analyzer as it is and leave the "split" functionality to the refactoring RR0190. |
|
I agree that the current behaviour of applying it causing the diagnostic to show up in a different place is not ideal and is something that should be fixed... I was going to implement this but then I decided that would be more work and I would do this change piece meal. This sounded like it was a good idea given your comment. The Rider analogue of this analyzer has exactly the behaviour that you think is weird and personally, I like it. But if you don't want to merge it then feel free to close this PR. |
@jamesHargreaves12 Could you send me a link to their docs? I would like to take a closer look at that analyzer. |
|
@jamesHargreaves12 ping |
|
From Rider docs: Inspections: Details: These ReSharper / Rider EditorConfig values may make it easier to find: There's a fix for it: Remove redundant 'else' keyword |
Adds support for the following:
to