Skip to content

Commit

Permalink
Merge pull request #5 from dedis/1.1.4
Browse files Browse the repository at this point in the history
1.1.4
  • Loading branch information
t1b00 authored Nov 23, 2024
2 parents db7a607 + ef40eeb commit d78d2c4
Show file tree
Hide file tree
Showing 29 changed files with 712 additions and 86 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ inThisBuild(
organizationName := "dedis",
organizationHomepage := Some(url("https://dedis.epfl.ch")),
homepage := Some(url("https://github.com/dedis/scapegoat-scalafix")),
licenses := List("AGPL 3.0" -> url("https://www.gnu.org/licenses/agpl-3.0.en.html")),
licenses := List("GPL 3.0" -> url("https://www.gnu.org/licenses/gpl-3.0.en.html")),
developers := List(Developer("t1b00", "Thibault Czarniak", "[email protected]", url("https://www.linkedin.com/in/thcz/"))),
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision,
scmInfo := Some(ScmInfo(url("https://github.com/dedis/scapegoat-scalafix"), "scm:[email protected]:dedis/scapegoat-scalafix.git")),
version := "1.1.3",
version := "1.1.4",
versionScheme := Some("pvp")
)
)
Expand Down
18 changes: 16 additions & 2 deletions input/src/main/scala/fix/AsInstanceOf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ rule = AsInstanceOf
package fix

object AsInstanceOf {
// In the original Scapegoat test code, there are tests with SuppressWarning annotations.
// We chose to not include these as the same could be done for every rule. If a user wants to suppress a rule, they can simply remove it from the configuration.

def test(): Unit = {
val s: Any = "sammy"
println(s.asInstanceOf[String]) // assert: AsInstanceOf
Expand All @@ -18,6 +17,21 @@ object AsInstanceOf {
case _ => println("no match :(") // scalafix: ok;
}

@SuppressWarnings(Array("all"))
def hello: Unit = {
val s: Any = "sammy"
println(s.asInstanceOf[String]) // scalafix: ok;
}

@SuppressWarnings(Array("AsInstanceOf"))
def hello2: Unit = {
val s: Any = "sammy"
println(s.asInstanceOf[String]) // scalafix: ok;
}

@SuppressWarnings(Array("AsInstanceOf"))
val mf = manifest[Class[_]] // scalafix: ok;

sealed trait MyGADT[T]
final case class VariantInt(value: Int) extends MyGADT[Int]
final case class VariantString(value: String) extends MyGADT[String]
Expand Down
43 changes: 43 additions & 0 deletions input/src/main/scala/fix/ExistsSimplifiableToContains.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
rule = ExistsSimplifiableToContains
*/
package fix

object ExistsSimplifiableToContains {
val exists1 = List(1, 2, 3).exists(x => x == 2) // assert: ExistsSimplifiableToContains
val exists12 = List(1, 2, 3).exists(x => 2 == x) // assert: ExistsSimplifiableToContains

val list = List("sam", "spade")
val exists2 = list.exists(_ == "spoof") // assert: ExistsSimplifiableToContains
val exists22 = list.exists("spoof" == _) // assert: ExistsSimplifiableToContains

val exists3 = (1 to 3).exists(_ == 2) // assert: ExistsSimplifiableToContains
val exists32 = (1 to 3).exists(2 == _) // assert: ExistsSimplifiableToContains
val exists33 = (1 until 3).exists(_ == 2) // assert: ExistsSimplifiableToContains
val exists34 = (1 until 3).exists(2 == _) // assert: ExistsSimplifiableToContains

def isItA(strings: String*): Boolean = {
strings.exists { element => // assert: ExistsSimplifiableToContains
element.toLowerCase == "a"
}
}

val l: Iterable[String] = List[String]("a", "b", "c")
print(l.exists(_ == "a")) // scalafix: ok;

def atLeastOneIsAllLowercase(strings: String*): Boolean = {
strings.exists { element => // scalafix: ok;
element == element.toLowerCase
}
}

def containsNoA(strings: String*): Boolean = {
strings.exists { element => // scalafix: ok;
element.replaceAll("a", "").size == element.size
}
}

val map = Map("answer" -> "42")
val isCorrect = map.exists(_._2 == "42") // scalafix: ok;

}
12 changes: 12 additions & 0 deletions input/src/main/scala/fix/FilterDotHead.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rule = FilterDotHead
*/
package fix

object FilterDotHead {
def test(): Unit = {
List(1, 2, 3).filter(_ < 0).head // assert: FilterDotHead
List(1, 2, 3).map(e => e).head // scalafix: ok;
}

}
12 changes: 12 additions & 0 deletions input/src/main/scala/fix/FilterDotHeadOption.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rule = FilterDotHeadOption
*/
package fix

object FilterDotHeadOption {
def test(): Unit = {
List(1, 2, 3).filter(_ < 0).headOption // assert: FilterDotHeadOption
List(1, 2, 3).map(e => e).headOption // scalafix: ok;
}

}
12 changes: 12 additions & 0 deletions input/src/main/scala/fix/FilterDotIsEmpty.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rule = FilterDotIsEmpty
*/
package fix

object FilterDotIsEmpty {
def test(): Unit = {
List(1, 2, 3).filter(_ < 0).isEmpty // assert: FilterDotIsEmpty
List(1, 2, 3).map(e => e).isEmpty // scalafix: ok;
}

}
12 changes: 12 additions & 0 deletions input/src/main/scala/fix/FilterDotSize.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rule = FilterDotSize
*/
package fix

object FilterDotSize {
def test(): Unit = {
List(1, 2, 3).filter(_ < 0).size // assert: FilterDotSize
List(1, 2, 3).map(e => e).size // scalafix: ok;
}

}
13 changes: 13 additions & 0 deletions input/src/main/scala/fix/FilterOptionAndGet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
rule = FilterOptionAndGet
*/
package fix

object FilterOptionAndGet {
def test(): Unit = {
val a = List(None, Some("sam")).filter(_.isDefined).map(_.get) // assert: FilterOptionAndGet
val b = List(None, Some("sam")).filter(_.isDefined).map(_.getOrElse("default")) // scalafix: ok;

}

}
17 changes: 17 additions & 0 deletions input/src/main/scala/fix/FinalModifierOnCaseClass.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
rule = FinalModifierOnCaseClass
*/
package fix

object FinalModifierOnCaseClass {
case class Person(name: String) // assert: FinalModifierOnCaseClass
abstract case class Person2(name: String) // scalafix: ok;
final case class Person3(name: String) // scalafix: ok;

// Comes from an issue on Scapegoat GitHub
sealed abstract case class Nat(toInt: Int) // scalafix: ok;
object Nat {
def fromInt(n: Int): Option[Nat] =
if (n >= 0) Some(new Nat(n) {}) else None
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
rule = FindAndNotEqualsNoneReplaceWithExists
*/
package fix

object FindAndNotEqualsNoneReplaceWithExists {
def test(): Unit = {
List(1, 2, 3).find(_ > 0) != None // assert: FindAndNotEqualsNoneReplaceWithExists
List(1, 2, 3).find(_ > 0) == None // assert: FindAndNotEqualsNoneReplaceWithExists
List(1, 2, 3).find(_ > 0) // scalafix: ok;
}

}
12 changes: 12 additions & 0 deletions input/src/main/scala/fix/FindDotIsDefined.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rule = FindDotIsDefined
*/
package fix

object FindDotIsDefined {
def test(): Unit = {
val a = List(1, 2, 3).find(_ > 4).isDefined // assert: FindDotIsDefined
val b = List(1, 2, 3).find(_ > 4) // scalafix: ok;
}

}
9 changes: 9 additions & 0 deletions input/src/main/scala/fix/InvalidRegexTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
rule = InvalidRegexTest
*/
package fix

object InvalidRegexTest {
val regex = "?".r // assert: InvalidRegexTest
val regex2 = "^[A-Z][A-Za-z0-9]*$".r // scalafix: ok;
}
43 changes: 43 additions & 0 deletions input/src/main/scala/fix/IsInstanceOf.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
rule = IsInstanceOf
*/
package fix

object IsInstanceOf {
def test(): Unit = {
val s: Any = "sammy"
println(s.isInstanceOf[String]) // assert: IsInstanceOf

case class MappingCharFilter(name: String, mappings: (String, String)*) // scalafix: ok;

val pf: PartialFunction[Any, Unit] = {
case s: String => println(s) // scalafix: ok;
case i: Int if i == 4 => println(i) // scalafix: ok;
case _ => println("no match :(") // scalafix: ok;
}

@SuppressWarnings(Array("all"))
def hello: Unit = {
val s: Any = "sammy"
println(s.isInstanceOf[String]) // scalafix: ok;
}

@SuppressWarnings(Array("IsInstanceOf"))
def hello2: Unit = {
val s: Any = "sammy"
println(s.isInstanceOf[String]) // scalafix: ok;
}

sealed trait MyGADT[T]
final case class VariantInt(value: Int) extends MyGADT[Int]
final case class VariantString(value: String) extends MyGADT[String]

def doStuff[T](gadt: MyGADT[T]): T = { // scalafix: ok;
gadt match {
case VariantInt(value) => value
case VariantString(value) => value
}
}
}

}
Loading

0 comments on commit d78d2c4

Please sign in to comment.