Skip to content

Commit

Permalink
UnusedSelfType
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jan 12, 2025
1 parent 9ddccf1 commit 9669976
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
19 changes: 19 additions & 0 deletions input/src/main/scala/fix/UnusedSelfTypeTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
rule = UnusedSelfType
*/
package fix

trait UnusedSelfTypeTest {
class X1 { self => // assert: UnusedSelfType
}
trait X2 { self => // assert: UnusedSelfType
}
trait X3 { self =>
def f1 = self
}
trait X4 { self =>
trait B {
val c = self
}
}
}
42 changes: 42 additions & 0 deletions rules/src/main/scala/fix/UnusedSelfType.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fix

import scala.meta.Self
import scala.meta.Template
import scala.meta.Term
import scala.meta.XtensionCollectionLikeUI
import scala.meta.contrib.XtensionTreeOps
import scalafix.Patch
import scalafix.lint.Diagnostic
import scalafix.lint.LintSeverity
import scalafix.v1.SyntacticDocument
import scalafix.v1.SyntacticRule
import scalafix.v1.XtensionSeqPatch

class UnusedSelfType extends SyntacticRule("UnusedSelfType") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect {
case t @ Template.After_4_9_9(
_,
_,
Template.Body(
Some(Self(a, None)),
_
),
_
)
if t.body.stats.forall(s =>
s.collectFirst {
case x: Term.Name if x.value == a.value => ()
}.isEmpty
) =>
Patch.lint(
Diagnostic(
id = "",
message = "unused self type",
position = a.pos,
severity = LintSeverity.Warning
)
)
}.asPatch
}
}

0 comments on commit 9669976

Please sign in to comment.