-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2136 from GiganticMinecraft/scalafix_lint
- Loading branch information
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/scalafix/scala/fix/WarnUnoverriddenImplicitToStringCallsOnCaseClass.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package fix | ||
|
||
import scalafix.v1._ | ||
|
||
import scala.meta._ | ||
|
||
/** | ||
* Lints on string interpolation where its variable part contains `case class` without `toString`. | ||
*/ | ||
// noinspection ScalaUnusedSymbol; referred from scalafix implicitly | ||
// NOTE: see AST on https://xuwei-k.github.io/scalameta-ast/ or https://astexplorer.net | ||
class WarnUnoverriddenImplicitToStringCallsOnCaseClass extends SemanticRule("WarnUnoverriddenImplicitToStringCallsOnCaseClass") { | ||
override def fix(implicit doc: SemanticDocument): Patch = { | ||
val s = doc.tree.collect { | ||
// string interpolation in standard library | ||
case Term.Interpolate((prefix, _, args)) => | ||
if (prefix.value != "s") { | ||
return Patch.empty | ||
} | ||
|
||
Patch.fromIterable( | ||
args.collect { arg => | ||
val tp = arg.symbol | ||
val info = tp.info.get | ||
// does `tp` point to some `case class`? | ||
val isCaseClass = info.isCase && info.isClass | ||
|
||
// lazily evaluated since most classes are not `case class` | ||
lazy val isToStringOverriden = info.overriddenSymbols.exists(overridenMethodSym => overridenMethodSym.value == "toString" && { | ||
overridenMethodSym.info.get.signature match { | ||
// def toString[](): <return type> の形の override を見つけたい。 | ||
// もし toString() の return type が String のサブタイプにならないような型であれば | ||
// scalafix が走る前にコンパイルが落ちるので、ここで改めて return type が | ||
// String のサブタイプであるかは考慮する必要はない | ||
case MethodSignature(List(), List(), _) => true | ||
case _ => false | ||
} | ||
}) | ||
|
||
if (!isCaseClass || isToStringOverriden) { | ||
return Patch.empty | ||
} | ||
|
||
Patch.lint(new Diagnostic { | ||
override def message: String = "Case class value shouldn't be interpolated, use `toString` " + | ||
"if you wish to interpolate the String representation into the string" | ||
|
||
// points to arg | ||
override def position: _root_.scala.meta.Position = arg.pos | ||
}) | ||
} | ||
) | ||
case _ => Patch.empty | ||
} | ||
|
||
Patch.fromIterable(s) | ||
} | ||
} |