File tree 2 files changed +28
-0
lines changed
rules/src/main/scala/org/typelevel/fix
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ rule = TypelevelAs
3
3
*/
4
4
package fix
5
5
6
+ import cats .syntax .functor .toFunctorOps
7
+
6
8
object AsTests {
7
9
8
10
def listMapLit = {
@@ -17,11 +19,18 @@ object AsTests {
17
19
.map(_ => ()) can be replaced by .void */
18
20
}
19
21
22
+ def listAsUnit = {
23
+ List (1 , 2 , 3 ).as(()) /* assert: TypelevelAs.as
24
+ ^^^^^^^^^^^^^^^^^^^^
25
+ .as(()) can be replaced by .void */
26
+ }
27
+
20
28
def shouldBeIgnored = {
21
29
def f = " a"
22
30
List (1 , 2 , 3 ).map(_ => f)
23
31
List (1 , 2 , 3 ).map(i => i)
24
32
List (1 , 2 , 3 ).map(println(_))
33
+ List (1 , 2 , 3 ).as(1 )
25
34
}
26
35
27
36
}
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ class As extends SemanticRule("TypelevelAs") {
26
26
case tree @ AnonymousMap (Lit .Unit ()) => Patch .lint(VoidDiagnostic (tree))
27
27
// fa.map(_ => 1)
28
28
case tree @ AnonymousMap (_ : Lit ) => Patch .lint(AsDiagnostic (tree))
29
+ // fa.as(())
30
+ case tree @ As (Lit .Unit ()) => Patch .lint(AsUnitDiagnostic (tree))
29
31
}.asPatch
30
32
31
33
}
@@ -41,6 +43,17 @@ object AnonymousMap {
41
43
}
42
44
}
43
45
46
+ object As {
47
+ def unapply (term : Term ): Option [Term ] = term match {
48
+ case Term .Apply .Initial (
49
+ Term .Select (_, Term .Name (" as" )),
50
+ List (value)
51
+ ) =>
52
+ Some (value)
53
+ case _ => None
54
+ }
55
+ }
56
+
44
57
final case class AsDiagnostic (t : Tree ) extends Diagnostic {
45
58
override def message : String = " .map(_ => f) can be replaced by .as(f)"
46
59
override def position : Position = t.pos
@@ -52,3 +65,9 @@ final case class VoidDiagnostic(t: Tree) extends Diagnostic {
52
65
override def position : Position = t.pos
53
66
override def categoryID : String = " as"
54
67
}
68
+
69
+ final case class AsUnitDiagnostic (t : Tree ) extends Diagnostic {
70
+ override def message : String = " .as(()) can be replaced by .void"
71
+ override def position : Position = t.pos
72
+ override def categoryID : String = " as"
73
+ }
You can’t perform that action at this time.
0 commit comments