-
Notifications
You must be signed in to change notification settings - Fork 93
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
mccartney
merged 4 commits into
scapegoat-scala:master
from
jyoo980:yoo/store-before-return
May 27, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/StoreBeforeReturn.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.sksamuel.scapegoat.inspections.unneccesary | ||
|
||
import com.sksamuel.scapegoat._ | ||
|
||
class StoreBeforeReturn | ||
extends Inspection( | ||
text = "Unnecessary store before return.", | ||
defaultLevel = Levels.Info, | ||
description = "Checks for storing a value in a block, and immediately returning the value.", | ||
explanation = | ||
"Storing a value and then immediately returning it is equivalent to returning the raw value itself." | ||
) { | ||
|
||
override def inspector(context: InspectionContext): Inspector = | ||
new Inspector(context) { | ||
override def postTyperTraverser: context.Traverser = | ||
new context.Traverser { | ||
import context.global._ | ||
|
||
private def lastExprName(expr: Tree): Option[String] = | ||
expr match { | ||
case Return(Ident(name)) => Some(name.toString()) | ||
case Ident(name) => Some(name.toString()) | ||
case _ => None | ||
} | ||
|
||
override def inspect(tree: context.global.Tree): Unit = | ||
tree match { | ||
case DefDef(_, _, _, _, _, Block(stmts, lastExprInBody)) => | ||
val maybeLastExprName = lastExprName(lastExprInBody) | ||
stmts.lastOption.foreach { | ||
case defn @ ValDef(_, assignmentName, _, _) | ||
if maybeLastExprName.contains(assignmentName.toString()) => | ||
context.warn(defn.pos, self) | ||
case _ => stmts.foreach(inspect) | ||
} | ||
case _ => continue(tree) | ||
} | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/test/scala/com/sksamuel/scapegoat/inspections/unnecessary/StoreBeforeReturnTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.sksamuel.scapegoat.inspections.unnecessary | ||
|
||
import com.sksamuel.scapegoat.InspectionTest | ||
import com.sksamuel.scapegoat.inspections.unneccesary.StoreBeforeReturn | ||
|
||
class StoreBeforeReturnTest extends InspectionTest { | ||
|
||
override val inspections = Seq(new StoreBeforeReturn) | ||
|
||
"store value before explicit return" - { | ||
"should report warning" in { | ||
val code = | ||
""" | ||
|object Test { | ||
| def hello(): String = { | ||
| var s = "sammy" | ||
| return s | ||
| } | ||
|} | ||
|""".stripMargin | ||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 1 | ||
} | ||
|
||
"should report warning in a nested function" in { | ||
val code = | ||
""" | ||
|object Test { | ||
| def foo(): Int = { | ||
| def bar(): Int = { | ||
| val x = 1 | ||
| return x | ||
| } | ||
| return bar() | ||
| } | ||
|} | ||
|""".stripMargin | ||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 1 | ||
} | ||
} | ||
|
||
"store value before implicit return" - { | ||
"should report warning" in { | ||
val code = | ||
""" | ||
|object Test { | ||
| def hello(): String = { | ||
| var s = "sammy" | ||
| s | ||
| } | ||
|} | ||
|""".stripMargin | ||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 1 | ||
} | ||
|
||
"should report warning in a nested function" in { | ||
val code = | ||
""" | ||
|object Test { | ||
| def foo(): Int = { | ||
| def bar(): Int = { | ||
| val x = 1 | ||
| x | ||
| } | ||
| bar() | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 1 | ||
} | ||
} | ||
"store value and modify before return" - { | ||
"should not report warning" in { | ||
val code = | ||
""" | ||
|object Test { | ||
| def hello(): Int = { | ||
| var x = 1 | ||
| x = x + 1 | ||
| return x | ||
| } | ||
|} | ||
|""".stripMargin | ||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 0 | ||
} | ||
} | ||
|
||
"store value and return for mutually-recusive functions" - { | ||
"should report a warning, and terminate" in { | ||
val code = | ||
""" | ||
|object Test { | ||
| def foo(): Int = { | ||
| var x = bar() | ||
| return x | ||
| } | ||
| def bar(): Int = { | ||
| val x = foo() | ||
| return x | ||
| } | ||
|} | ||
|""".stripMargin | ||
compileCodeSnippet(code) | ||
compiler.scapegoat.feedback.warnings.size shouldBe 2 | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean:
or something else?
Can you write down a UT for this case?
I am still not sure what's the suspected code.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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)