Skip to content

Commit 62b1a2e

Browse files
authored
Merge pull request #1871 from dedis/work-be2-thibault-linter_master
[BE2] Work on more rules for linter
2 parents 0450bf5 + 9c32274 commit 62b1a2e

29 files changed

+874
-37
lines changed

be2-scala/linter/input/src/main/scala/fix/ArraysInFormat.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ object ArraysInFormat {
99
val str = f"Here are my cool elements ${array}" // assert: ArraysInFormat
1010
val str2 = s"Here are my cool elements ${array}" // assert: ArraysInFormat
1111
String.format("Here are my cool elements %d", array) // assert: ArraysInFormat
12+
"Here are my cool elements %d".format(array) // assert: ArraysInFormat
13+
val str3 = "Here are my cool elements %d"
14+
str3.format(array) // assert: ArraysInFormat
1215
String.format("Here are my cool elements %d", array) /* assert: ArraysInFormat
1316
^^^^^
1417
Array passed to format / interpolate string
1518
*/
19+
20+
"Here are my cool elements %d".format(13) // scalafix: ok;
1621
}
1722

1823
}

be2-scala/linter/input/src/main/scala/fix/EmptyInterpolatedString.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ package fix
66
object EmptyInterpolatedString {
77
def test(): Unit = {
88
print(f"Here's my cute interpolation") // assert: EmptyInterpolatedString
9-
print(s"Here's my amazing interpolationg") // assert: EmptyInterpolatedString
10-
print(String.format("I'm hungry!")) // assert: EmptyInterpolatedString
9+
print(s"Here's my amazing interpolation") // assert: EmptyInterpolatedString
10+
String.format("I'm hungry!") // assert: EmptyInterpolatedString
11+
val str = "I'm hungry!"
12+
str.format() // assert: EmptyInterpolatedString
13+
"I'm hungry!".format() // assert: EmptyInterpolatedString
1114
}
1215
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
rule = IllegalFormatString
3+
*/
4+
package fix
5+
6+
object IllegalFormatString {
7+
def test(): Unit = {
8+
val name: String = "John"
9+
val age: Integer = 30
10+
val illegalFormatString1 = "%s is %d years old, %d"
11+
String.format(illegalFormatString1, name, age) // assert: IllegalFormatString
12+
13+
illegalFormatString1.format(name, age) // assert: IllegalFormatString
14+
15+
"%s is %d years old, %d".format(name, age) // assert: IllegalFormatString
16+
17+
"%5.5q".format("sam") // assert: IllegalFormatString
18+
19+
"% s".format("sam") // assert: IllegalFormatString
20+
21+
"%qs".format("sam") // assert: IllegalFormatString
22+
23+
"%.-5s".format("sam") // assert: IllegalFormatString
24+
25+
"%.s".format("sam") // assert: IllegalFormatString
26+
27+
"%<s %s".format("sam", "sam") // assert: IllegalFormatString
28+
29+
"%.2f %s".format(14.5, "sammmmmmmmm") // scalafix: ok;
30+
"%010d".format(0) // scalafix: ok;
31+
32+
"%s is %d years old, %d".format(name, age, 145) // scalafix: ok;
33+
34+
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
rule = IncorrectNumberOfArgsToFormat
3+
*/
4+
package fix
5+
6+
object IncorrectNumberOfArgsToFormat {
7+
def test() = {
8+
val name: String = "John"
9+
val age: Integer = 30
10+
val illegalFormatString1 = "%s is %d years old, %d"
11+
illegalFormatString1.format(name, age, 134, 54, "Olivia") // assert: IncorrectNumberOfArgsToFormat
12+
illegalFormatString1.format(name) // assert: IncorrectNumberOfArgsToFormat
13+
14+
"%s is %d years old, %d".format(name, age, 134, 54, "Olivia") // assert: IncorrectNumberOfArgsToFormat
15+
"%s is %d years old, %d".format(name) // assert: IncorrectNumberOfArgsToFormat
16+
17+
String.format(illegalFormatString1, name) // assert: IncorrectNumberOfArgsToFormat
18+
19+
illegalFormatString1.format(name, age, 134) // scalafix: ok;
20+
"%s is %d years old, %d".format(name, age, 134) // scalafix: ok;
21+
}
22+
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
rule = IncorrectlyNamedExceptions
3+
*/
4+
package fix
5+
6+
// Test values taken from Scapegoat test
7+
object IncorrectlyNamedExceptions {
8+
class NotException // scalafix: ok;
9+
10+
class IsException extends Exception // scalafix: ok;
11+
12+
class IsNotNamedCorrectly extends Exception // assert: IncorrectlyNamedExceptions
13+
14+
class Is extends Exception // assert: IncorrectlyNamedExceptions
15+
16+
class IsChild extends Is // assert: IncorrectlyNamedExceptions
17+
18+
class IsChildException // scalafix: ok;
19+
20+
class IsRuntimeException extends RuntimeException // scalafix: ok;
21+
22+
class IsRuntime extends Exception // assert: IncorrectlyNamedExceptions
23+
24+
}
25+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
rule = LonelySealedTrait
3+
*/
4+
package fix
5+
6+
// Test values taken from Scapegoat test
7+
object LonelySealedTrait {
8+
9+
sealed trait NoImpl // assert: LonelySealedTrait
10+
11+
sealed trait Impl1 // scalafix: ok;
12+
object Implemented extends Impl1
13+
14+
sealed trait Impl2 // scalafix: ok;
15+
case object Implemented2 extends Impl2
16+
17+
sealed trait Impl3 // scalafix: ok;
18+
case object Implemented3 extends Serializable with Impl3
19+
20+
sealed trait Impl4 // scalafix: ok;
21+
case class Implemented4() extends Impl4
22+
23+
sealed trait Impl5 // scalafix: ok;
24+
class Implemented extends Impl5
25+
26+
sealed trait Impl6 // scalafix: ok;
27+
case class Implemented6(name: String) extends Serializable with Impl6
28+
29+
sealed trait Impl7 // scalafix: ok;
30+
class Implemented7(name: String) extends Serializable with Impl7
31+
32+
sealed trait Impl8 // scalafix: ok;
33+
case class Implemented8(name: String) extends Impl8
34+
35+
sealed trait Impl9 // scalafix: ok;
36+
class Implemented9(name: String) extends Impl9
37+
38+
sealed trait Impl10 // scalafix: ok;
39+
sealed trait Impl11 // scalafix: ok;
40+
case class Implemented10(name: String) extends Impl10
41+
case class Implemented11(name: String) extends Impl11
42+
43+
trait AnalyzerFilter {
44+
def name: String
45+
}
46+
47+
trait AnalyzerFilterDefinition {
48+
def filterType: String
49+
}
50+
51+
sealed trait CharFilter extends AnalyzerFilter // scalafix: ok;
52+
53+
sealed trait CharFilterDefinition extends CharFilter with AnalyzerFilterDefinition // scalafix: ok;
54+
55+
case object HtmlStripCharFilter extends CharFilter {
56+
val name = "html_strip"
57+
}
58+
59+
case class MappingCharFilter(name: String, mappings: (String, String)*)
60+
extends CharFilterDefinition {
61+
val filterType = "mapping"
62+
}
63+
64+
case class PatternReplaceCharFilter(name: String, pattern: String, replacement: String)
65+
extends CharFilterDefinition {
66+
val filterType = "pattern_replace"
67+
}
68+
69+
sealed abstract class MultiMode(val elastic: String) // scalafix: ok;
70+
case object MultiMode {
71+
case object Min extends MultiMode("min")
72+
case object Max extends MultiMode("max")
73+
case object Sum extends MultiMode("sum")
74+
case object Avg extends MultiMode("avg")
75+
}
76+
77+
sealed trait A[S] // scalafix: ok;
78+
case object B extends A[String]
79+
case object C extends A[BigDecimal]
80+
81+
sealed abstract class IndexOptionsNoImplementation(val value: String) // assert: LonelySealedTrait
82+
83+
sealed abstract class IndexOptions(val value: String) // scalafix: ok;
84+
object IndexOptions {
85+
case object Docs extends IndexOptions("docs")
86+
case object Freqs extends IndexOptions("freqs")
87+
case object Positions extends IndexOptions("positions")
88+
}
89+
90+
sealed trait D // scalafix: ok;
91+
sealed trait E extends D
92+
sealed trait F extends E
93+
object F1 extends F
94+
95+
sealed class G // scalafix: ok;
96+
sealed class H extends G
97+
sealed class I extends H
98+
object I1 extends I
99+
100+
sealed trait J // scalafix: ok;
101+
sealed trait K extends J
102+
sealed class L extends K
103+
object L1 extends L
104+
105+
106+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
rule = MapGetAndGetOrElse
3+
*/
4+
package fix
5+
6+
object MapGetAndGetOrElse {
7+
def test() = {
8+
val numMap1 = Map(1 -> "one", 2 -> "two")
9+
numMap1.get(1).getOrElse("unknown") // assert: MapGetAndGetOrElse
10+
11+
12+
val numMap2 = scala.collection.mutable.Map("one" -> 1, "two" -> 2)
13+
numMap2.get("one").getOrElse(-1) // assert: MapGetAndGetOrElse
14+
15+
Map("John" -> "Smith", "Peter" -> "Rabbit").get("Sarah").getOrElse("-") // assert: MapGetAndGetOrElse
16+
17+
}
18+
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
rule = NanComparison
3+
*/
4+
package fix
5+
6+
object NanComparison {
7+
8+
def test() = {
9+
val d = 0.5d
10+
d == Double.NaN // assert: NanComparison
11+
Double.NaN == d // assert: NanComparison
12+
d.equals(Double.NaN) // assert: NanComparison
13+
Double.NaN.equals(d) // assert: NanComparison
14+
15+
val f = 0.5f
16+
f == Double.NaN // assert: NanComparison
17+
Double.NaN == f // assert: NanComparison
18+
19+
val g = Double.NaN
20+
f == g // assert: NanComparison
21+
g == f // assert: NanComparison
22+
23+
d == g // assert: NanComparison
24+
g == d // assert: NanComparison
25+
d.equals(g) // assert: NanComparison
26+
g.equals(d) // assert: NanComparison
27+
}
28+
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
rule = OptionGet
3+
*/
4+
package fix
5+
6+
object OptionGet {
7+
def test() = {
8+
val o = Option("olivia")
9+
o.get // assert: OptionGet
10+
11+
Option("layla").get // assert: OptionGet
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
rule = OptionSize
3+
*/
4+
package fix
5+
6+
object OptionSize {
7+
def test() = {
8+
val o = Option("olivia")
9+
o.size // assert: OptionSize
10+
11+
Option("layla").size // assert: OptionSize
12+
}
13+
}

0 commit comments

Comments
 (0)