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

Variable shadowing #266

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ For instructions on suppressing warnings by file, by inspection or by line see [

### Inspections

There are currently 116 inspections. An overview list is given, followed by a more detailed description of each inspection after the list (todo: finish rest of detailed descriptions)
There are currently 117 inspections. An overview list is given, followed by a more detailed description of each inspection after the list (todo: finish rest of detailed descriptions)

| Name | Brief Description | Default Level |
|------|-------------------|---------------|
Expand Down Expand Up @@ -257,6 +257,7 @@ There are currently 116 inspections. An overview list is given, followed by a mo
| UseSqrt | Checks for use of `math.pow` for calculating `math.sqrt` | Info |
| VarClosure | Finds closures that reference var | Warning |
| VarCouldBeVal | Checks for `var`s that could be declared as `val`s | Warning |
| VariableShadowing | Checks for multiple uses of the variable name in nested scopes | Warning |
| WhileTrue | Checks for code that uses a `while(true)` or `do { } while(true)` block. | Warning |
| ZeroNumerator | Checks for dividing by 0 by a number, eg `0 / x` which will always return `0` | Warning |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ object ScapegoatConfig extends App {
new UseLog10,
new UseLog1P,
new UseSqrt,
new VariableShadowing,
new VarClosure,
new VarCouldBeVal,
new WhileTrue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class VariableShadowing extends Inspection("Variable shadowing", Levels.Warning)

override def inspect(tree: Tree): Unit = {
tree match {
case Block(_, _) =>
enter(); continue(tree); exit()
case ClassDef(_, _, _, Template(_, _, body)) =>
enter(); continue(tree); exit()
case ModuleDef(_, _, Template(_, _, body)) =>
Expand All @@ -41,13 +43,12 @@ class VariableShadowing extends Inspection("Variable shadowing", Levels.Warning)
cases.foreach {
case CaseDef(Bind(name, _), _, _) =>
if (isDefined(name.toString)) warn(tree)
continue(tree)
case _ =>
continue(tree)
case _ => // do nothing
}
continue(tree)
case _ => continue(tree)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,56 @@ class VariableShadowingTest
compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}

"when two if branches define the same variable" in {
val code =
"""class Test {
| if (1 > 0) {
| val something = 4
| println(something+1)
| } else {
| val something = 2
| println(something+2)
| }
|}""".stripMargin

compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}

"when two sibling cases define the same local variable" in {
val code =
"""class Test {
| val possibility: Option[Int] = Some(3)
| possibility match {
| case Some(x) =>
| val y = x + 1
| println(y)
| case None =>
| val y = 0
| println(y)
| }
|}""".stripMargin

compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}

"when visiting a match case, especially not visit it twice" in {
val code =
"""class Test {
| val possibility: Option[Int] = Some(3)
| possibility match {
| case Some(x) =>
| val y = x + 1
| println(y)
| case None => println("None")
| }
|}""".stripMargin

compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
}
}
}
}