Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactor action: Remove redundant else statement #68529

Draft
wants to merge 49 commits into
base: main
Choose a base branch
from

Conversation

kandicst
Copy link
Contributor

Fixes #60385

@kandicst kandicst requested a review from a team as a code owner June 10, 2023 00:06
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 10, 2023
@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Jun 10, 2023
@kandicst kandicst changed the title Code refactor: Remove redundant else statement Code refactor action: Remove redundant else statement Jun 10, 2023

return elseClause;
}
private static bool IsJumpStatement(StatementSyntax? statement)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: make local functino above.

@@ -257,6 +257,11 @@ private static Option2<CodeStyleOption2<ExpressionBodyPreference>> CreatePreferE
"csharp_style_prefer_primary_constructors",
CSharpIdeCodeStyleOptions.Default.PreferPrimaryConstructors);

public static readonly Option2<CodeStyleOption2<bool>> PreferRemoveRedundantElseStatement = CreateOption(
CSharpCodeStyleOptionGroups.CodeBlockPreferences,
"csharp_style_prefer_remove_redundant_else_statement",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be dotnet_... since it applies to VB as well. csharp_style is only for constructs that are c# specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making that change only would fail this check since Option language is C# by default

Debug.Assert(LanguageName is null == (Definition.ConfigName.StartsWith("dotnet_", StringComparison.Ordinal) ||

Should this option then go in some other file or just create it here with language as null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct. it goes into another file with teh rest of the dotnet style options.

@CyrusNajmabadi
Copy link
Member

This is a great start. Thanks for the contribution!

@CyrusNajmabadi
Copy link
Member

Fyi. I'm currently in vacation for another week. But I am very enthusiastic about this pr. I will try to get some reviews in early, but can't promise because of commitments.

RemoveRedundantElseStatementCodeFixProvider>;

[Trait(Traits.Feature, Traits.Features.CodeActionsRemoveRedundantElseStatement)]
public class RemoveRedundantElseStatementTests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some fix-all tests? That should have if-statements within the an outer if's 'else' block. both should have redundant else blocks.

Note: because of fix all, it's possible one fix will change things such that there might be an issue with the inner local variables now being a collision problem when fixing the outer. i can suggest ways to address that if that occurs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the fix-all, but now sure how to reproduce the inner/outer collision case you described

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inner else should have a variable that can either move up two levels, or only one.

@CyrusNajmabadi
Copy link
Member

@kandicst Ok. Done with cleanup/rewriting. Tehre are still some pieces of feedback i've left in the PR that would be good to take care of. Thanks!

/// (return, break, continue or throw) in order to preserve the program's correctness.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class RemoveRedundantElseStatementDiagnosticAnalyzer : AbstractBuiltInCodeStyleDiagnosticAnalyzer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 I worry about adding this as an analyzer and not a refactoring. There have been many cases where I included an explicit else for clarity, and I wouldn't want an analyzer undoing that work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both can work here. It's fine as an analyzer (it's off by default).

When off, it would make sense to have as a refactoring too. I can make such a change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C# Refactor actions missing for removing redundant else statements
4 participants