From 400823e1133d870d8163d6ab30570475365e4ae6 Mon Sep 17 00:00:00 2001 From: Meriam Lachkar Date: Thu, 15 Jul 2021 10:29:01 +0200 Subject: [PATCH] Make NoAutoTupling fixes the insertion of unit - refactor tests --- docs/rules/NoAutoTupling.md | 14 ++++------- project/ScalafixBuild.scala | 4 ++-- .../internal/rule/NoAutoTupling.scala | 2 ++ .../{ => noAutoTupling}/NoAutoTupling.scala | 7 ++---- .../test/noAutoTupling}/NoUnitInsertion.scala | 2 +- .../{ => noAutoTupling}/NoAutoTupling.scala | 7 ++---- .../test/noAutoTupling}/NoAutoTupling.scala | 7 ++---- .../test/noAutoTupling}/NoAutoTupling.scala | 7 ++---- .../test/noAutoTupling/NoUnitInsertion.scala | 24 +++++++++++++++++++ 9 files changed, 42 insertions(+), 32 deletions(-) rename scalafix-tests/input/src/main/scala-2/test/{ => noAutoTupling}/NoAutoTupling.scala (91%) rename scalafix-tests/input/src/main/{scala-2.12/test => scala-2/test/noAutoTupling}/NoUnitInsertion.scala (92%) rename scalafix-tests/output/src/main/scala-2.11/test/{ => noAutoTupling}/NoAutoTupling.scala (91%) rename scalafix-tests/output/src/main/{scala-2.13/test => scala-2.12/test/noAutoTupling}/NoAutoTupling.scala (91%) rename scalafix-tests/output/src/main/{scala-2.12/test => scala-2.13/test/noAutoTupling}/NoAutoTupling.scala (91%) create mode 100644 scalafix-tests/output/src/main/scala-2/test/noAutoTupling/NoUnitInsertion.scala diff --git a/docs/rules/NoAutoTupling.md b/docs/rules/NoAutoTupling.md index ffbdb12fd..d981d8179 100644 --- a/docs/rules/NoAutoTupling.md +++ b/docs/rules/NoAutoTupling.md @@ -4,27 +4,23 @@ id: NoAutoTupling title: NoAutoTupling --- -> ⚠️ This rule does not work with Scala 2.13 since `-Yno-adapter-args` has been -> removed. - Adds explicit tuples around argument lists where auto-tupling is occurring. To use this rule: -- enable `-Ywarn-adapted-args` (note, `-Yno-adapted-args` will fail compilation, - which prevents scalafix from running) -- disable `-Xfatal-warnings`. Unfortunately, the Scala compiler does not support - finer grained control over the severity level per message kind. See - [scalameta/scalameta#924](https://github.com/scalameta/scalameta/issues/924) - for a possible workaround in the near future. +- enable `-Ywarn-adapted-args` for Scala 2.11 and 2.12 (note, `-Yno-adapted-args` will fail compilation, + which prevents scalafix from running). For Scala 2.13, use instead `-Xlint:adapted-args`. +- enable also `-deprecation` to get warnings on insertions of `Unit`. ```scala // before def someMethod(t: (Int, String)) = ... someMethod(1, "something") +val c: Option[Unit] = Some() // after def someMethod(t: (Int, String)) = ... someMethod((1, "something")) +val c: Option[Unit] = Some(()) ``` Auto-tupling is a feature that can lead to unexpected results, making code to diff --git a/project/ScalafixBuild.scala b/project/ScalafixBuild.scala index b3277e17e..4e9272971 100644 --- a/project/ScalafixBuild.scala +++ b/project/ScalafixBuild.scala @@ -60,8 +60,8 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys { } val warnAdaptedArgs = Def.setting { if (isScala3.value) Nil - else if (isScala213.value) Seq("-Xlint:adapted-args") - else Seq("-Ywarn-adapted-args") + else if (isScala213.value) Seq("-Xlint:adapted-args", "-deprecation") + else Seq("-Ywarn-adapted-args", "-deprecation") } lazy val scaladocOptions = Seq( "-groups", diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala index c42253041..336273300 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala @@ -25,6 +25,8 @@ class NoAutoTupling extends SemanticRule("NoAutoTupling") { case message if message.message.startsWith( "Adaptation of argument list by inserting ()" + ) || message.message.startsWith( + "adaptation of an empty argument list by inserting ()" ) => message.position }.toSet diff --git a/scalafix-tests/input/src/main/scala-2/test/NoAutoTupling.scala b/scalafix-tests/input/src/main/scala-2/test/noAutoTupling/NoAutoTupling.scala similarity index 91% rename from scalafix-tests/input/src/main/scala-2/test/NoAutoTupling.scala rename to scalafix-tests/input/src/main/scala-2/test/noAutoTupling/NoAutoTupling.scala index 4396c43e4..adbceb44d 100644 --- a/scalafix-tests/input/src/main/scala-2/test/NoAutoTupling.scala +++ b/scalafix-tests/input/src/main/scala-2/test/noAutoTupling/NoAutoTupling.scala @@ -1,9 +1,9 @@ /* rules = NoAutoTupling */ -package test +package test.noAutoTupling -class NoAutoTupling2 { +class NoAutoTupling { def a(x: (Int, Boolean)) = x a(2, true) @@ -48,7 +48,4 @@ class NoAutoTupling2 { Foo(42, "foo", ('z', true)) Foo.apply(42, "foo", ('z', true)) } - - //todo: Fix Adaptation of argument list by inserting () - val c: Option[Unit] = Some() } diff --git a/scalafix-tests/input/src/main/scala-2.12/test/NoUnitInsertion.scala b/scalafix-tests/input/src/main/scala-2/test/noAutoTupling/NoUnitInsertion.scala similarity index 92% rename from scalafix-tests/input/src/main/scala-2.12/test/NoUnitInsertion.scala rename to scalafix-tests/input/src/main/scala-2/test/noAutoTupling/NoUnitInsertion.scala index 812005ce0..353ade261 100644 --- a/scalafix-tests/input/src/main/scala-2.12/test/NoUnitInsertion.scala +++ b/scalafix-tests/input/src/main/scala-2/test/noAutoTupling/NoUnitInsertion.scala @@ -1,7 +1,7 @@ /* rules = NoAutoTupling */ -package test +package test.noAutoTupling class NoUnitInsertion { diff --git a/scalafix-tests/output/src/main/scala-2.11/test/NoAutoTupling.scala b/scalafix-tests/output/src/main/scala-2.11/test/noAutoTupling/NoAutoTupling.scala similarity index 91% rename from scalafix-tests/output/src/main/scala-2.11/test/NoAutoTupling.scala rename to scalafix-tests/output/src/main/scala-2.11/test/noAutoTupling/NoAutoTupling.scala index a37d68ff2..ebd49ac96 100644 --- a/scalafix-tests/output/src/main/scala-2.11/test/NoAutoTupling.scala +++ b/scalafix-tests/output/src/main/scala-2.11/test/noAutoTupling/NoAutoTupling.scala @@ -1,6 +1,6 @@ -package test +package test.noAutoTupling -class NoAutoTupling2 { +class NoAutoTupling { def a(x: (Int, Boolean)) = x a((2, true)) @@ -45,8 +45,5 @@ class NoAutoTupling2 { Foo(42, "foo", ('z', true)) Foo.apply(42, "foo", ('z', true)) } - - //todo: Fix Adaptation of argument list by inserting () - val c: Option[Unit] = Some() } diff --git a/scalafix-tests/output/src/main/scala-2.13/test/NoAutoTupling.scala b/scalafix-tests/output/src/main/scala-2.12/test/noAutoTupling/NoAutoTupling.scala similarity index 91% rename from scalafix-tests/output/src/main/scala-2.13/test/NoAutoTupling.scala rename to scalafix-tests/output/src/main/scala-2.12/test/noAutoTupling/NoAutoTupling.scala index ad0c75e4e..7b76cbeb7 100644 --- a/scalafix-tests/output/src/main/scala-2.13/test/NoAutoTupling.scala +++ b/scalafix-tests/output/src/main/scala-2.12/test/noAutoTupling/NoAutoTupling.scala @@ -1,6 +1,6 @@ -package test +package test.noAutoTupling -class NoAutoTupling2 { +class NoAutoTupling { def a(x: (Int, Boolean)) = x a((2, true)) @@ -45,8 +45,5 @@ class NoAutoTupling2 { Foo(42, "foo", ('z', true)) Foo.apply(42, "foo", ('z', true)) } - - //todo: Fix Adaptation of argument list by inserting () - val c: Option[Unit] = Some() } diff --git a/scalafix-tests/output/src/main/scala-2.12/test/NoAutoTupling.scala b/scalafix-tests/output/src/main/scala-2.13/test/noAutoTupling/NoAutoTupling.scala similarity index 91% rename from scalafix-tests/output/src/main/scala-2.12/test/NoAutoTupling.scala rename to scalafix-tests/output/src/main/scala-2.13/test/noAutoTupling/NoAutoTupling.scala index ad0c75e4e..7b76cbeb7 100644 --- a/scalafix-tests/output/src/main/scala-2.12/test/NoAutoTupling.scala +++ b/scalafix-tests/output/src/main/scala-2.13/test/noAutoTupling/NoAutoTupling.scala @@ -1,6 +1,6 @@ -package test +package test.noAutoTupling -class NoAutoTupling2 { +class NoAutoTupling { def a(x: (Int, Boolean)) = x a((2, true)) @@ -45,8 +45,5 @@ class NoAutoTupling2 { Foo(42, "foo", ('z', true)) Foo.apply(42, "foo", ('z', true)) } - - //todo: Fix Adaptation of argument list by inserting () - val c: Option[Unit] = Some() } diff --git a/scalafix-tests/output/src/main/scala-2/test/noAutoTupling/NoUnitInsertion.scala b/scalafix-tests/output/src/main/scala-2/test/noAutoTupling/NoUnitInsertion.scala new file mode 100644 index 000000000..f772c5bb7 --- /dev/null +++ b/scalafix-tests/output/src/main/scala-2/test/noAutoTupling/NoUnitInsertion.scala @@ -0,0 +1,24 @@ +package test.noAutoTupling + +class NoUnitInsertion { + + val x: Option[Unit] = Option(()) + + def a(u: Unit): Unit = u + a(()) + + def b(x: Int)(u: Unit): Unit = (x, u) + b(2)(()) + + val c: Unit => Unit = + u => u + c(()) + + case class Foo(u: Unit) + Foo(()) + Foo.apply(()) + + case class Bar(i: Int)(u: Unit) + Bar.apply(2)(()) + +} \ No newline at end of file