Skip to content

[SR-5744] Refactoring action to convert if-let to guard-let and vice versa #24566

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

Merged
merged 2 commits into from
Jul 11, 2019
Merged

[SR-5744] Refactoring action to convert if-let to guard-let and vice versa #24566

merged 2 commits into from
Jul 11, 2019

Conversation

Regno
Copy link
Contributor

@Regno Regno commented May 7, 2019

Implement action to convert from:

func foo(idxOpt: Int?) {
  if let idx = idxOpt {
    print(idx)
  }
}

to

func foo(idxOpt: Int?) {
  guard let idx = idxOpt else {
    return
  }
  print(idx)
}

and vice-versa

Resolves new feature request SR-5744.
Refactoring action to convert if-let to guard-let and vice versa

Feedback is very much appreciated!

@Regno
Copy link
Contributor Author

Regno commented May 7, 2019

@swift-ci test

@theblixguy
Copy link
Collaborator

You can't invoke CI yourself, you need commit access for it.

@theblixguy
Copy link
Collaborator

cc @akyrtzi

@akyrtzi akyrtzi requested a review from nkcsgexi May 14, 2019 00:37
Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

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

The implementation looks great overall👍! I have only several nitpicks inline.


if (Info.ContainedNodes.size() == 1) {
if (auto S = Info.ContainedNodes[0].dyn_cast<Stmt*>()) {
If = dyn_cast<IfStmt>(S);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should always use 2-spaces indentation. Could you fix other indentation issues as well in this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


if (CondList.size() != 1)
return true; // abort

Copy link
Contributor

Choose a reason for hiding this comment

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

We can assume performChange() is called only when isApplicable returns true. So it seems the above logics are unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

auto Body = dyn_cast_or_null<BraceStmt>(If->getThenStmt());

if (!Body)
return true; // abort
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you move the ThenStmt check to the isApplicable function too? so we don't need to check it here and surprising the users by aborting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to the isApplicable.


// Get if-let else body.
auto ElseBody = dyn_cast_or_null<BraceStmt>(If->getElseStmt());
if (ElseBody) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These two statements can be merged as if (auto *ElseBody = dyn_cast_or_null<BraceStmt>(If->getElseStmt())) {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

auto E = CondList[0];
auto P = E.getPatternOrNull();
if (!P)
return true; // abort
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, can we move these condition checkings into isApplicable function and only check it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved.

auto ReplaceRange = RangeInfo.ContentRange;
EditConsumer.accept(SM, ReplaceRange, DeclBuffer.str());

return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation issue here and all other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@nkcsgexi
Copy link
Contributor

@swift-ci please smoke test

@nkcsgexi
Copy link
Contributor

@swift-ci please stress test

@Regno
Copy link
Contributor Author

Regno commented Jul 11, 2019

Also I fixed similar issues at this PR: Refactoring action to convert to computed property.

@nkcsgexi
Copy link
Contributor

All tests passed. Congratulations for your first Swift contribution!🚢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants