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

Adding inspection for unnecessary value store before return #653

Merged
merged 4 commits into from
May 27, 2022

Conversation

jyoo980
Copy link
Contributor

@jyoo980 jyoo980 commented May 21, 2022

  • Now checking for a case where a value is stored in a block, and then
    immediately returned in the next line.
  • Tests for both implicit and explicit scope returns.

  * Now checking for a case where a value is stored in a block, and then
    immediately returned in the next line.
  * Tests for both implicit and explicit scope returns.
case defn @ ValDef(_, assignmentName, _, _)
if maybeLastExprName.contains(assignmentName.toString()) =>
context.warn(defn.pos, self)
case _ => stmts.foreach(inspect)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a suspicion that this will blow up for mutually-recursive functions, but maybe the traversal algorithm already handles this? If not, any suggestions on how to approach this? I guess I could use an accumulator to keep track of visited locations, but that seems naive.

Copy link
Collaborator

Choose a reason for hiding this comment

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

mutually-recursive functions,

Do you mean:


  def a(): Int = {
    val x = b()
    x
  }
  def b(): Int = {
    val x = a()
    x
  }

or something else?

I have a suspicion [...] how to approach this?

Can you write down a UT for this case?
I am still not sure what's the suspected code.

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 a unit test, looks like it isn't a problem (which makes sense, since this is just a syntactic check and the linter doesn't actually execute the code).

Copy link
Collaborator

@mccartney mccartney May 25, 2022

Choose a reason for hiding this comment

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

Yes. We don't follow recursion (unless your inspection code chooses to, but even if it did it could only do within a single class / compilation unit)

README.md Outdated Show resolved Hide resolved
@mccartney mccartney merged commit 2ea5343 into scapegoat-scala:master May 27, 2022
@jyoo980 jyoo980 deleted the yoo/store-before-return branch May 27, 2022 17:19
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