Skip to content

Add a rule that prevents assignment from appearing in expression contexts. #442

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 1 commit into from
Nov 2, 2022

Conversation

allevato
Copy link
Member

@allevato allevato commented Nov 1, 2022

The main goal of this rule is to prevent "clever" code like this:

func foo() {
  guard someCondition else { return someProperty = someValue }
  // ...
}

That assignment can be easily misread as the Boolean == instead, and most readers wouldn't expect side effects to appear in a return statement like that. Just write it as two separate statements.

@allevato
Copy link
Member Author

allevato commented Nov 1, 2022

Creating this as a draft right now since it depends on the test fix in #441 to pass.

cc @dylansturg

XCTAssertDiagnosed(.moveAssignmentToOwnStatement, line: 2, column: 10)
}

func testReturnStatementWithAssignmentHasCommentMoved() {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about if they're doing something silly like opening a multiline comment on the line with the return? I think the trailing trivia will get moved so that the return is sitting inside of the comment?

func foo() {
  return a = b /* blargh
    continues here */
}

Copy link
Member Author

Choose a reason for hiding this comment

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

The trivia will never break within a block comment, so the entire comment (including its line break) will be a single trivia piece. We're also still using the hack workaround I wrote to preserve the legacy trivia behavior, so that comment would be in the leading trivia of the } token.

I did tweak this logic though to make sure it handles more cases around weird comments and added tests for those, PTAL.

…exts.

The main goal of this rule is to prevent "clever" code like this:

```
func foo() {
  guard someCondition else { return someProperty = someValue }
  // ...
}
```

That assignment can be easily misread as the Boolean `==` instead, and
most readers wouldn't expect side effects to appear in a `return`
statement like that. Just write it as two separate statements.
@allevato allevato force-pushed the no-assignment-expr-rule branch from 8563dbe to 08be967 Compare November 2, 2022 16:06
@allevato allevato marked this pull request as ready for review November 2, 2022 16:06
@allevato allevato merged commit 11d16d0 into swiftlang:main Nov 2, 2022
@allevato allevato deleted the no-assignment-expr-rule branch November 2, 2022 19:36
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.

2 participants